rienafairefr / pynYNAB

a python client for the new YNAB
MIT License
138 stars 12 forks source link

init_session() needs to be called no matter if the connection object … #50

Closed chbndrhnns closed 6 years ago

chbndrhnns commented 6 years ago

…is given to the factory or if it is created there.

Otherwise something like this fails:

from pynYNAB.ClientFactory import nYnabClientFactory
from pynYNAB.connection import nYnabConnection
from  pynYNAB.schema.budget import Payee
class Arg(object): pass
args = Arg()
args.budgetname = 'a'
args.email = 'b'
args.password = 'c'
import logging
logging.basicConfig(level='DEBUG')
log = logging.getLogger(__name__)

connection = nYnabConnection(args.email, args.password)
client = nYnabClientFactory().create_client(budgetname=args.budgetname,
                                                      nynabconnection=connection,
                                                      sync=True,
                                                      logger=log)
coveralls commented 6 years ago

Coverage Status

Coverage increased (+0.07%) to 83.371% when pulling 89d08f93678205e719b1ca1b9bb94d1de18b7062 on chbndrhnns:master into 072110a59ff468b5c30c92b99549c4f2c7d59cb3 on rienafairefr:master.

rienafairefr commented 6 years ago

I see the problem. I think I delegated that responsibility to the caller. Presumably, if you're passing a connection object you know you should initialize it at some point. Otherwise, just omit te connection object, pass email and password and the connection will be created and initialized for you inside the nYnabClientFactory.

If you're taking responsibility for creating the connection, you're taking the responsibility for initializing it, I think that's alright ? init_session starts the session with nynab server, with a certain expiration period for the tokens etc. the caller (your code or whatever) should decide when to init I think, not me.

A middle ground would be to fail gracefully in case the connection has not been initialized before trying to sync or whatever

chbndrhnns commented 6 years ago

Failing gracefully is ok for me. I just tried https://github.com/scottrobertson/fintech-to-ynab and noticed the init did not take place there which is why it failed starting up.

rienafairefr commented 6 years ago

Does logging an error and raising a nYnabConnectionError exception count as failing gracefully for your use case ? at least it's a bit more verbose than just "unknown api error invalid_token" :-)

chbndrhnns commented 6 years ago

Definitely!