diff --git a/pyakeneo/auth.py b/pyakeneo/auth.py index fb8ae18..e3bd8a7 100644 --- a/pyakeneo/auth.py +++ b/pyakeneo/auth.py @@ -24,11 +24,20 @@ def __init__(self, base_url, client_id, secret, username, password): self._token = None self._refresh_token = None self._expiry_date = None + self._session = requests.Session() @property def authorization(self): return "Bearer {0}".format(self._token) + @property + def session(self): + return self._session + + @session.setter + def session(self, session): + self._session = session + def _request_a_token(self, grant_type="password"): """Requests a token. Throws in case of error""" authorization = "Basic {0}".format( @@ -58,7 +67,7 @@ def _request_a_token(self, grant_type="password"): ) url = urljoin(self._base_url, self.TOKEN_PATH) - r = requests.post(url, data=data, headers=headers) + r = self._session.post(url, data=data, headers=headers) r.raise_for_status() try: diff --git a/pyakeneo/client.py b/pyakeneo/client.py index 7e60672..47456df 100644 --- a/pyakeneo/client.py +++ b/pyakeneo/client.py @@ -14,44 +14,34 @@ class Client: BASIC_API_PATH = "/api/rest/v1/" def __init__( - self, - base_url, - client_id=None, - secret=None, - username=None, - password=None, - session=None, - auth=None, + self, + base_url: str, + client_id: str = None, + secret: str = None, + username: str = None, + password: str = None, + session: requests.Session = None, ): - """Expect credential - 1) as auth, or - 2) as client_id+secret+username+password, or - 3) as session having an authentication.""" - provided_auth = False - if not auth: - if client_id or secret or username or password: - if client_id and secret and username and password: - provided_auth = True - auth = Auth(base_url, client_id, secret, username, password) - elif session: - provided_auth = True - else: - provided_auth = True - if not provided_auth: + if not session and not (client_id and secret and username and password): + # No credentials provided neither via client_id+secret+username+password nor via session raise ValueError( - "Expect credential 1) as auth, or " - + "2) as client_id+secret+username+password, or " - + "3) as session having an authentication." + "Expect credentials via " + + "1) as client_id+secret+username+password, or " + + "2) as session having an authentication." ) + if not session: session = requests.Session() - self._init(base_url, session, auth) + session.auth = self._make_auth(base_url, client_id, secret, username, password) + + self._init(base_url, session) + + def _make_auth(self, base_url, client_id, secret, username, password) -> Auth: + return Auth(base_url, client_id, secret, username, password) - def _init(self, base_url, session, auth): + def _init(self, base_url, session): self._base_url = base_url self._session = session - if auth: - self._session.auth = auth self._session.headers.update({"Content-Type": "application/json"}) self._resources = { "association_types": AssociationTypesPool(