Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to assign custom sessions in the client and its auth #4

Merged
merged 3 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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