-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from inloco/refactor/replaces-requests-in-api-…
…and-token_manager refactor(api, token_manager): creates BaseRequest calls and uses on existing routes
- Loading branch information
Showing
7 changed files
with
340 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ | |
'feedback_events', | ||
'json_util', | ||
'models', | ||
'token_manager'] | ||
'token_manager', | ||
'base_request'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import json | ||
import platform | ||
import sys | ||
from typing import Final, Any, Union, Optional | ||
|
||
import requests | ||
|
||
from incognia.exceptions import IncogniaHTTPError | ||
|
||
_LIBRARY_VERSION: Final[str] = sys.modules['incognia'].__version__ | ||
_OS_NAME: Final[str] = platform.system() | ||
_OS_VERSION: Final[str] = platform.release() | ||
_OS_ARCH: Final[str] = platform.architecture()[0] | ||
_LANGUAGE_VERSION: Final[str] = platform.python_version() | ||
|
||
USER_AGENT_HEADER: Final[dict] = { | ||
'User-Agent': f'incognia-python/{_LIBRARY_VERSION}' | ||
f' ({_OS_NAME} {_OS_VERSION} {_OS_ARCH})' | ||
f' Python/{_LANGUAGE_VERSION}' | ||
} | ||
|
||
JSON_CONTENT_HEADER: Final[dict] = { | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
|
||
class BaseRequest: | ||
def __init__(self, timeout: float = 5.0): | ||
self.__timeout: float = timeout | ||
|
||
def timeout(self) -> float: | ||
return self.__timeout | ||
|
||
def post(self, url: Union[str, bytes], headers: Any = None, data: Any = None, | ||
params: Any = None, | ||
auth: Optional[Any] = None) -> Optional[dict]: | ||
headers = headers or {} | ||
headers.update(USER_AGENT_HEADER) | ||
|
||
try: | ||
response = requests.post(url=url, headers=headers, data=data, params=params, | ||
timeout=self.__timeout, | ||
auth=auth) | ||
response.raise_for_status() | ||
return json.loads(response.content.decode('utf-8')) or None | ||
|
||
except requests.HTTPError as e: | ||
raise IncogniaHTTPError(e) from None | ||
|
||
def get(self, url: Union[str, bytes], headers: Any = None, data: Any = None) -> Optional[dict]: | ||
headers = headers or {} | ||
headers.update(USER_AGENT_HEADER) | ||
|
||
try: | ||
response = requests.get(url=url, headers=headers, data=data, timeout=self.__timeout) | ||
response.raise_for_status() | ||
return json.loads(response.content.decode('utf-8')) or None | ||
|
||
except requests.HTTPError as e: | ||
raise IncogniaHTTPError(e) from None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.