-
Notifications
You must be signed in to change notification settings - Fork 113
Reduce requests #823
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
Closed
Closed
Reduce requests #823
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
|---|---|---|
|
|
@@ -126,7 +126,7 @@ def _request_with_auth(response, | |
| timeout=None, | ||
| verify=None, | ||
| **kwargs): | ||
| """Try request (3 times) with credentials if 401 returned from server | ||
| """Request with credentials | ||
|
|
||
| :param response: requests.response | ||
| :type response: requests.Response | ||
|
|
@@ -146,40 +146,32 @@ def _request_with_auth(response, | |
| :rtype: requests.Response | ||
| """ | ||
|
|
||
| i = 0 | ||
| while i < 3 and response.status_code == 401: | ||
| parsed_url = urlparse(url) | ||
| hostname = parsed_url.hostname | ||
| auth_scheme, realm = get_auth_scheme(response) | ||
| creds = (hostname, auth_scheme, realm) | ||
|
|
||
| with lock: | ||
| if creds not in AUTH_CREDS: | ||
| auth = _get_http_auth(response, parsed_url, auth_scheme) | ||
| else: | ||
| auth = AUTH_CREDS[creds] | ||
|
|
||
| # try request again, with auth | ||
| response = _request(method, url, is_success, timeout, auth, | ||
| verify, **kwargs) | ||
|
|
||
| # only store credentials if they're valid | ||
| with lock: | ||
| if creds not in AUTH_CREDS and response.status_code == 200: | ||
| AUTH_CREDS[creds] = auth | ||
| # acs invalid token | ||
| elif response.status_code == 401 and \ | ||
| auth_scheme in ["acsjwt", "oauthjwt"]: | ||
|
|
||
| if config.get_config_val("core.dcos_acs_token") is not None: | ||
| msg = ("Your core.dcos_acs_token is invalid. " | ||
| "Please run: `dcos auth login`") | ||
| raise DCOSException(msg) | ||
|
|
||
| i += 1 | ||
| parsed_url = urlparse(url) | ||
| hostname = parsed_url.hostname | ||
| auth_scheme, realm = get_auth_scheme(response) | ||
| creds = (hostname, auth_scheme, realm) | ||
|
|
||
| if response.status_code == 401: | ||
| raise DCOSAuthenticationException(response) | ||
| with lock: | ||
| if creds not in AUTH_CREDS: | ||
| auth = _get_http_auth(response, parsed_url, auth_scheme) | ||
| else: | ||
| auth = AUTH_CREDS[creds] | ||
|
|
||
| response = _request(method, url, is_success, timeout, auth, | ||
| verify, **kwargs) | ||
|
|
||
| # only store credentials if they're valid | ||
| with lock: | ||
| if creds not in AUTH_CREDS and response.status_code == 200: | ||
| AUTH_CREDS[creds] = auth | ||
| # acs invalid token | ||
| elif response.status_code == 401 and \ | ||
| auth_scheme in ["acsjwt", "oauthjwt"]: | ||
|
|
||
| if config.get_config_val("core.dcos_acs_token") is not None: | ||
| msg = ("Your core.dcos_acs_token is invalid. " | ||
| "Please run: `dcos auth login`") | ||
| raise DCOSException(msg) | ||
|
|
||
| return response | ||
|
|
||
|
|
@@ -190,8 +182,14 @@ def request(method, | |
| timeout=None, | ||
| verify=None, | ||
| **kwargs): | ||
| """Sends an HTTP request. If the server responds with a 401, ask the | ||
| user for their credentials, and try request again (up to 3 times). | ||
| """Sends an HTTP request. We first send a HEAD request for the | ||
| supplied URL so that we can determine the type of authentication | ||
| required (if any). If authentication is required then we again | ||
| use a HEAD request asking the user for their credentials, and | ||
| try request again (up to 3 times). Once authenticated, we issue | ||
| the request passed in. We are careful to execute the request | ||
| passed in just once given that it may be stateful e.g. any files | ||
| object containing a stream may only be evaluated once. | ||
|
|
||
| :param method: method for the new Request object | ||
| :type method: str | ||
|
|
@@ -218,12 +216,26 @@ def request(method, | |
| if verify is not None: | ||
| silence_requests_warnings() | ||
|
|
||
| response = _request(method, url, is_success, timeout, | ||
| verify=verify, **kwargs) | ||
| dcos_url = config.get_config_val("core.dcos_url") | ||
| auth_url = urllib.parse.urljoin(dcos_url, 'exhibitor/') | ||
| response = _request( | ||
| 'GET', auth_url, is_success, timeout, verify=verify, **kwargs) | ||
|
|
||
| if response.status_code == 401: | ||
| i = 0 | ||
| while i < 3 and response.status_code == 401: | ||
| auth_response = _request_with_auth( | ||
| response, 'GET', auth_url, is_success, timeout, | ||
|
Author
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. Also, |
||
| verify=verify, **kwargs) | ||
| response.status_code = auth_response.status_code | ||
| i += 1 | ||
|
|
||
| response = _request_with_auth(response, method, url, is_success, | ||
| timeout, verify, **kwargs) | ||
| timeout, verify=verify, **kwargs) | ||
| if response.status_code == 401: | ||
| raise DCOSAuthenticationException(response) | ||
| else: | ||
| response = _request(method, url, is_success, timeout, verify, **kwargs) | ||
|
|
||
| if is_success(response.status_code): | ||
| return response | ||
|
|
||
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.
I'd still use a
HEADhere - it is cheaper given the lack of content in reply. You'll still get the headers you need, and your service can potentially optimise in terms of handlingHEAD.