Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #36 from rafalkrupinski/feature/request_opts
Browse files Browse the repository at this point in the history
Make API accept default options for requests.sessions.Session.request().
  • Loading branch information
thicccat688 authored Jan 9, 2024
2 parents 31e4eee + 03a0fcd commit f93f547
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 3 additions & 1 deletion peerberrypy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(
password: Optional[str] = None,
tfa_secret: Optional[str] = None,
access_token: Optional[str] = None,
request_opts: Optional[dict] = None,
):
"""
Peerberry API wrapper with all relevant Peerberry functionalities.
Expand All @@ -33,14 +34,15 @@ def __init__(
:param tfa_secret: Base32 secret used for two-factor authentication
:param access_token: Access token used to authenticate to the API (Optional; Only pass the JWT for it to work!)
(Only mandatory if account has two-factor authentication enabled)
:param request_opts: Optional[dict] - Additional options for :any:`requests.sessions.Session.request()`.
"""

self.email = email
self._password = password
self._tfa_secret = tfa_secret

# Initialize HTTP session & authenticate to API
self._session = RequestHandler()
self._session = RequestHandler(request_opts or {})
self.access_token = access_token

if not access_token:
Expand Down
8 changes: 6 additions & 2 deletions peerberrypy/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class RequestHandler:
def __init__(self):
def __init__(self, request_params: dict):
""" Request handler for internal use with Peerberry's specifications. """
self.__session = cloudscraper.create_scraper(
browser={
Expand All @@ -14,6 +14,7 @@ def __init__(self):
'desktop': True,
}
)
self._request_params = request_params

def request(
self,
Expand All @@ -29,10 +30,13 @@ def request(
if output_type not in output_types:
raise ValueError(f'Output type must be one of the following: {", ".join(output_types)}')

requests_params = self._request_params.copy()
requests_params.update(kwargs)

response = self.__session.request(
method=method,
url=url,
**kwargs,
**requests_params,
)

if response.status_code >= 400:
Expand Down

0 comments on commit f93f547

Please sign in to comment.