Skip to content

Commit

Permalink
Merge pull request #4 from KaveTech/feat/allow-custom-sessions
Browse files Browse the repository at this point in the history
Allow to assign custom sessions in the client and its auth
  • Loading branch information
amatmv authored Aug 7, 2023
2 parents c848803 + 8e015e6 commit 74c5651
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 31 deletions.
11 changes: 10 additions & 1 deletion pyakeneo/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
50 changes: 20 additions & 30 deletions pyakeneo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 74c5651

Please sign in to comment.