-
Notifications
You must be signed in to change notification settings - Fork 0
Send a custom user agent #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: master
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -64,6 +64,7 @@ class Download(_helpers.RequestsMixin, _download.Download): | |
| end (int): The last byte in a range to be downloaded. If not | ||
| provided, but ``start`` is provided, will download from the | ||
| ``start`` to the end of the media. | ||
| user_agent (Optional[str]) : Custom user agent to be sent | ||
| headers (Optional[Mapping[str, str]]): Extra headers that should | ||
| be sent with the request, e.g. headers for encrypted data. | ||
|
|
||
|
|
@@ -156,6 +157,8 @@ def consume(self, transport): | |
| finished. | ||
| """ | ||
| method, url, payload, headers = self._prepare_request() | ||
| if self.user_agent is not None: | ||
| headers[u'User-Agent'] = self.user_agent | ||
|
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. Not sure which way is better, to have the
Owner
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. We should not set user_agent as None if api caller doesnt provide user-agent. In that case, user_agent would be 'python-requests/2.18.' which is set by the underlying requests lib. |
||
| # NOTE: We assume "payload is None" but pass it along anyway. | ||
| request_kwargs = { | ||
| u'data': payload, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,10 +132,11 @@ def test__write_to_stream_with_hash_check_fail(self): | |
| decode_unicode=False) | ||
|
|
||
| def _consume_helper( | ||
| self, stream=None, end=65536, headers=None, chunks=(), | ||
| response_headers=None): | ||
| self, stream=None, end=65536, user_agent=None, headers=None, | ||
| chunks=(), response_headers=None): | ||
|
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. Same idea as in the first comment.
Owner
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. Same as comment in first. |
||
| download = download_mod.Download( | ||
| EXAMPLE_URL, stream=stream, end=end, headers=headers) | ||
| EXAMPLE_URL, stream=stream, end=end, user_agent=user_agent, | ||
| headers=headers) | ||
|
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. Same thing...
Owner
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. Same as above. |
||
| transport = mock.Mock(spec=[u'request']) | ||
| transport.request.return_value = _mock_response( | ||
| chunks=chunks, headers=response_headers) | ||
|
|
@@ -234,6 +235,14 @@ def test_consume_with_headers(self): | |
| # Make sure the headers have been modified. | ||
| assert headers == {u'range': range_bytes} | ||
|
|
||
| def test_consume_with_user_agent(self): | ||
| headers = {} | ||
| end = 16383 | ||
| user_agent = "Custom-User-Agent-1.0" | ||
| range_bytes = u'bytes={:d}-{:d}'.format(0, end) | ||
| self._consume_helper(end=end, user_agent=user_agent, headers=headers) | ||
| assert headers == {u'range': range_bytes, u'User-Agent': user_agent} | ||
|
|
||
|
|
||
| class TestChunkedDownload(object): | ||
|
|
||
|
|
||
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.
Is there any particular reason to put
user_agentbeforeheaders?Uh oh!
There was an error while loading. Please reload this page.
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.
Nothing as such, i thought of having str argument(user_agent) before a dict(headers) looks good.