-
Notifications
You must be signed in to change notification settings - Fork 0
Added API Token object and logging in as a different user #2
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
base: main
Are you sure you want to change the base?
Changes from all commits
8222d78
9252dd3
f8587ff
af976ae
0985402
f9efc61
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -60,6 +60,16 @@ def __init__(self): | |||||||||||||||||||||||||||||||||||||||
| self.username = self._cfg['username'] | ||||||||||||||||||||||||||||||||||||||||
| self.password = self._cfg['password'] | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| @classmethod | ||||||||||||||||||||||||||||||||||||||||
| def with_credentials(cls, uri, username, password): | ||||||||||||||||||||||||||||||||||||||||
| """Create a config with explicit credentials instead of reading from a config file.""" | ||||||||||||||||||||||||||||||||||||||||
| config = cls.__new__(cls) | ||||||||||||||||||||||||||||||||||||||||
| config._hashtopolis_uri = uri | ||||||||||||||||||||||||||||||||||||||||
| config._api_endpoint = uri + '/api/v2' | ||||||||||||||||||||||||||||||||||||||||
| config.username = username | ||||||||||||||||||||||||||||||||||||||||
| config.password = password | ||||||||||||||||||||||||||||||||||||||||
| return config | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| class HashtopolisResponseError(HashtopolisError): | ||||||||||||||||||||||||||||||||||||||||
| pass | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -106,22 +116,26 @@ def __init__(self, model_uri, config): | |||||||||||||||||||||||||||||||||||||||
| self._hashtopolis_uri = config._hashtopolis_uri | ||||||||||||||||||||||||||||||||||||||||
| self.config = config | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def authenticate(self): | ||||||||||||||||||||||||||||||||||||||||
| if self._api_endpoint not in HashtopolisConnector.token: | ||||||||||||||||||||||||||||||||||||||||
| # Request access TOKEN, used throughout the test | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| logger.info("Start authentication") | ||||||||||||||||||||||||||||||||||||||||
| def authenticate(self, auth=None): | ||||||||||||||||||||||||||||||||||||||||
| if auth is not None: | ||||||||||||||||||||||||||||||||||||||||
| logger.info("Start authentication with provided credentials") | ||||||||||||||||||||||||||||||||||||||||
| auth_uri = self._api_endpoint + '/auth/token' | ||||||||||||||||||||||||||||||||||||||||
| auth = (self.config.username, self.config.password) | ||||||||||||||||||||||||||||||||||||||||
| r = requests.post(auth_uri, auth=auth) | ||||||||||||||||||||||||||||||||||||||||
| self.validate_status_code(r, [201], "Authentication failed") | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| r_json = self.resp_to_json(r) | ||||||||||||||||||||||||||||||||||||||||
| HashtopolisConnector.token[self._api_endpoint] = r_json['token'] | ||||||||||||||||||||||||||||||||||||||||
| HashtopolisConnector.token_expires[self._api_endpoint] = r_json['token'] | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| self._token = HashtopolisConnector.token[self._api_endpoint] | ||||||||||||||||||||||||||||||||||||||||
| self._token_expires = HashtopolisConnector.token_expires[self._api_endpoint] | ||||||||||||||||||||||||||||||||||||||||
| self._token = r_json['token'] | ||||||||||||||||||||||||||||||||||||||||
| self._token_expires = r_json['token'] | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+119
to
+127
|
||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||
| if self._api_endpoint not in HashtopolisConnector.token: | ||||||||||||||||||||||||||||||||||||||||
| logger.info("Start authentication") | ||||||||||||||||||||||||||||||||||||||||
| auth_uri = self._api_endpoint + '/auth/token' | ||||||||||||||||||||||||||||||||||||||||
| r = requests.post(auth_uri, auth=(self.config.username, self.config.password)) | ||||||||||||||||||||||||||||||||||||||||
| self.validate_status_code(r, [201], "Authentication failed") | ||||||||||||||||||||||||||||||||||||||||
| r_json = self.resp_to_json(r) | ||||||||||||||||||||||||||||||||||||||||
| HashtopolisConnector.token[self._api_endpoint] = r_json['token'] | ||||||||||||||||||||||||||||||||||||||||
| HashtopolisConnector.token_expires[self._api_endpoint] = r_json['token'] | ||||||||||||||||||||||||||||||||||||||||
| self._token = HashtopolisConnector.token[self._api_endpoint] | ||||||||||||||||||||||||||||||||||||||||
| self._token_expires = HashtopolisConnector.token_expires[self._api_endpoint] | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| self._headers = { | ||||||||||||||||||||||||||||||||||||||||
| 'Authorization': 'Bearer ' + self._token | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -215,8 +229,8 @@ def get_single_page(self, page, filter): | |||||||||||||||||||||||||||||||||||||||
| return response["data"] | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # todo refactor start_offset into page variable | ||||||||||||||||||||||||||||||||||||||||
| def filter(self, include, ordering, filter, start_offset): | ||||||||||||||||||||||||||||||||||||||||
| self.authenticate() | ||||||||||||||||||||||||||||||||||||||||
| def filter(self, include, ordering, filter, start_offset, auth=None): | ||||||||||||||||||||||||||||||||||||||||
| self.authenticate(auth=auth) | ||||||||||||||||||||||||||||||||||||||||
| headers = self._headers | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+232
to
234
|
||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| after_dict = {"primary": {"id": start_offset}} | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -394,12 +408,13 @@ def count(self, filter): | |||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| # Build Django ORM style django.query interface | ||||||||||||||||||||||||||||||||||||||||
| class QuerySet(): | ||||||||||||||||||||||||||||||||||||||||
| def __init__(self, cls, include=None, ordering=None, filters=None, pages=None): | ||||||||||||||||||||||||||||||||||||||||
| def __init__(self, cls, include=None, ordering=None, filters=None, pages=None, auth=None): | ||||||||||||||||||||||||||||||||||||||||
| self.cls = cls | ||||||||||||||||||||||||||||||||||||||||
| self.include = include | ||||||||||||||||||||||||||||||||||||||||
| self.ordering = ordering | ||||||||||||||||||||||||||||||||||||||||
| self.filters = filters | ||||||||||||||||||||||||||||||||||||||||
| self.pages = pages | ||||||||||||||||||||||||||||||||||||||||
| self.auth = auth | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def __iter__(self): | ||||||||||||||||||||||||||||||||||||||||
| yield from self.__getitem__(slice(None, None, 1)) | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -431,7 +446,7 @@ def filter_(self, start, stop, step): | |||||||||||||||||||||||||||||||||||||||
| filters['id'] = filters['pk'] | ||||||||||||||||||||||||||||||||||||||||
| del filters['pk'] | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| filter_generator = self.cls.get_conn().filter(self.include, self.ordering, filters, start_offset=cursor) | ||||||||||||||||||||||||||||||||||||||||
| filter_generator = self.cls.get_conn().filter(self.include, self.ordering, filters, start_offset=cursor, auth=self.auth) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| while index < stop: | ||||||||||||||||||||||||||||||||||||||||
| # Fetch new entries in chunks default to server | ||||||||||||||||||||||||||||||||||||||||
|
|
@@ -469,6 +484,10 @@ def page(self, **pages): | |||||||||||||||||||||||||||||||||||||||
| def all(self): | ||||||||||||||||||||||||||||||||||||||||
| # yield from self | ||||||||||||||||||||||||||||||||||||||||
| return self | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| def authenticate(self, auth): | ||||||||||||||||||||||||||||||||||||||||
| self.auth = auth | ||||||||||||||||||||||||||||||||||||||||
| return self | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+488
to
491
|
||||||||||||||||||||||||||||||||||||||||
| def authenticate(self, auth): | |
| self.auth = auth | |
| return self | |
| def with_auth(self, auth): | |
| """Set authentication credentials for subsequent requests. | |
| This only stores the value on the QuerySet so it can be passed to | |
| later HTTP requests. It does not perform authentication immediately. | |
| Args: | |
| auth: Authentication object understood by requests, typically a | |
| ``(username, password)`` tuple. | |
| """ | |
| self.auth = auth | |
| return self | |
| def authenticate(self, auth): | |
| """Backward-compatible alias for :meth:`with_auth`.""" | |
| return self.with_auth(auth) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Helperis now imported in the "base stuff" import list, but it is also imported again later in the "action utilities" import list. Consider removing one of these imports to avoid redundancy and potential confusion during maintenance.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As it's mentioned by Copilot, the helper class is already imported below.