-
Notifications
You must be signed in to change notification settings - Fork 33
IDEV-2020 handle large response feeds improved #148
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3c2d37b
Revert "IDEV-2020: Handle partial response from RTUF endpoints."
jbabac 0c7b051
IDEV-2020: Create FeedsResults class for results generator for feeds …
jbabac 18ffd9c
IDEV-2020: Use the FeedsResults class.
jbabac f3a15c6
IDEV-2020: Adjust cli implementation
jbabac 23f3fbf
IDEV-2020: Adjust tests.
jbabac 9bb7e8c
IDEV-2020: Use typing.Generator.
jbabac 3953216
IDEV-2020: Fix type hint to FeedsResults. It should be the .response(…
jbabac 6a8a76a
IDEV-2020: Refactor checking limit exceeded and remove feeds product …
jbabac ee1577b
IDEV-2020: Override self.data() method to isolate handling of FeedsRe…
jbabac 711418d
IDEV-2020: Raise the exception at the end.
jbabac 0bca37b
IDEV-2020: Make "Limit Exceeded" the default reason.
jbabac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -53,8 +53,6 @@ def __init__( | |
| self._response = None | ||
| self._items_list = None | ||
| self._data = None | ||
| self._limit_exceeded = None | ||
| self._limit_exceeded_message = None | ||
|
|
||
| def _wait_time(self): | ||
| if not self.api.rate_limit or not self.product in self.api.limits: | ||
|
|
@@ -77,29 +75,6 @@ def _wait_time(self): | |
|
|
||
| return wait_for | ||
|
|
||
| def _get_feeds_results_generator(self, parameters, headers): | ||
| with Client(verify=self.api.verify_ssl, proxy=self.api.proxy_url, timeout=None) as session: | ||
| status_code = None | ||
| while status_code != 200: | ||
| resp_data = session.get(url=self.url, params=parameters, headers=headers, **self.api.extra_request_params) | ||
| status_code = resp_data.status_code | ||
| self.setStatus(status_code, resp_data) | ||
|
|
||
| # Check limit exceeded here | ||
| if "response" in resp_data.text and "limit_exceeded" in resp_data.text: | ||
| self._limit_exceeded = True | ||
| self._limit_exceeded_message = "limit exceeded" | ||
|
|
||
| yield resp_data | ||
|
|
||
| if self._limit_exceeded: | ||
| raise ServiceException(503, "Limit Exceeded{}".format(self._limit_exceeded_message)) | ||
|
|
||
| if not self.kwargs.get("sessionID"): | ||
| # we'll only do iterative request for queries that has sessionID. | ||
| # Otherwise, we will have an infinite request if sessionID was not provided but the required data asked is more than the maximum (1 hour of data) | ||
| break | ||
|
|
||
| def _get_session_params(self): | ||
| parameters = deepcopy(self.kwargs) | ||
| parameters.pop("output_format", None) | ||
|
|
@@ -118,12 +93,6 @@ def _get_session_params(self): | |
| return {"parameters": parameters, "headers": headers} | ||
|
|
||
| def _make_request(self): | ||
| if self.product in FEEDS_PRODUCTS_LIST: | ||
| session_params = self._get_session_params() | ||
| parameters = session_params.get("parameters") | ||
| headers = session_params.get("headers") | ||
|
|
||
| return self._get_feeds_results_generator(parameters=parameters, headers=headers) | ||
|
|
||
| with Client(verify=self.api.verify_ssl, proxy=self.api.proxy_url, timeout=None) as session: | ||
| if self.product in [ | ||
|
|
@@ -138,15 +107,19 @@ def _make_request(self): | |
| patch_data = self.kwargs.copy() | ||
| patch_data.update(self.api.extra_request_params) | ||
| return session.patch(url=self.url, json=patch_data) | ||
| elif self.product in FEEDS_PRODUCTS_LIST: | ||
| session_params = self._get_session_params() | ||
| parameters = session_params.get("parameters") | ||
| headers = session_params.get("headers") | ||
| return session.get(url=self.url, params=parameters, headers=headers, **self.api.extra_request_params) | ||
| else: | ||
| return session.get(url=self.url, params=self.kwargs, **self.api.extra_request_params) | ||
|
|
||
| def _get_results(self): | ||
| wait_for = self._wait_time() | ||
| if self.api.rate_limit and (wait_for is None or self.product == "account-information"): | ||
| data = self._make_request() | ||
| status_code = data.status_code if self.product not in FEEDS_PRODUCTS_LIST else 200 | ||
| if status_code == 503: # pragma: no cover | ||
| if data.status_code == 503: # pragma: no cover | ||
| sleeptime = 60 | ||
| log.info( | ||
| "503 encountered for [%s] - sleeping [%s] seconds before retrying request.", | ||
|
|
@@ -166,40 +139,27 @@ def _get_results(self): | |
| def data(self): | ||
| if self._data is None: | ||
| results = self._get_results() | ||
| status_code = results.status_code if self.product not in FEEDS_PRODUCTS_LIST else 200 | ||
| self.setStatus(status_code, results) | ||
| if ( | ||
| self.kwargs.get("format", "json") == "json" | ||
| and self.product not in FEEDS_PRODUCTS_LIST # Special handling of feeds products' data to preserve the result in jsonline format | ||
| ): | ||
| self.setStatus(results.status_code, results) | ||
| if self.kwargs.get("format", "json") == "json": | ||
| self._data = results.json() | ||
| elif self.product in FEEDS_PRODUCTS_LIST: | ||
| self._data = results # Uses generator to handle large data results from feeds endpoint | ||
| else: | ||
| self._data = results.text | ||
| limit_exceeded, message = self.check_limit_exceeded() | ||
|
|
||
| if limit_exceeded: | ||
| self._limit_exceeded = True | ||
| self._limit_exceeded_message = message | ||
| self.check_limit_exceeded() | ||
|
|
||
| if self._limit_exceeded is True: | ||
| raise ServiceException(503, "Limit Exceeded{}".format(self._limit_exceeded_message)) | ||
| else: | ||
| return self._data | ||
| return self._data | ||
|
|
||
| def check_limit_exceeded(self): | ||
| if self.product in FEEDS_PRODUCTS_LIST: | ||
| # bypass here as this is handled in generator already | ||
| return False, "" | ||
|
|
||
| if self.kwargs.get("format", "json") == "json" and self.product not in FEEDS_PRODUCTS_LIST: | ||
| if "response" in self._data and "limit_exceeded" in self._data["response"] and self._data["response"]["limit_exceeded"] is True: | ||
| return True, self._data["response"]["message"] | ||
| # TODO: handle html, xml response errors better. | ||
| limit_exceeded, reason = False, "" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a pedantic change: can we put the Overall, LGTM ✅ . Nice!! |
||
| if isinstance(self._data, dict) and ( | ||
| "response" in self._data and "limit_exceeded" in self._data["response"] and self._data["response"]["limit_exceeded"] is True | ||
| ): | ||
| limit_exceeded, reason = True, self._data["response"]["message"] | ||
| elif "response" in self._data and "limit_exceeded" in self._data: | ||
| return True, "limit exceeded" | ||
| return False, "" | ||
| limit_exceeded = True | ||
|
|
||
| if limit_exceeded: | ||
| raise ServiceException(503, f"Limit Exceeded {reason}") | ||
|
|
||
| @property | ||
| def status(self): | ||
|
|
@@ -249,7 +209,7 @@ def response(self): | |
| return self._response | ||
|
|
||
| def items(self): | ||
| return self.response().items() if isinstance(self.response(), dict) else self.response() | ||
| return self.response().items() | ||
|
|
||
| def emails(self): | ||
| """Find and returns all emails mentioned in the response""" | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice!