-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add galaxy to user agent #17578
Add galaxy to user agent #17578
Changes from all commits
10c31d8
17f4228
1d58a38
5b168ed
8c06c8e
b4c8344
5aac7f1
8c63ec9
c32f538
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import urllib | ||
|
||
import requests | ||
|
||
from galaxy.version import VERSION | ||
|
||
|
||
def __append_word_to_user_agent(word): | ||
|
||
# set requests User-Agent | ||
old_default_user_agent = requests.utils.default_user_agent | ||
|
||
def new_default_user_agent(*args): | ||
return f"{old_default_user_agent(*args)} {word}" | ||
|
||
requests.utils.default_user_agent = new_default_user_agent | ||
|
||
# set urllib User-Agent | ||
old_build_opener = urllib.request.build_opener | ||
|
||
def modify_user_agent_header(header): | ||
if header[0].lower() == "user-agent": | ||
return (header[0], f"{header[1]} {word}") | ||
return header | ||
|
||
def new_build_opener(*handlers): | ||
opener = old_build_opener(*handlers) | ||
opener.addheaders = [modify_user_agent_header(header) for header in opener.addheaders] | ||
return opener | ||
|
||
urllib.request.build_opener = new_build_opener | ||
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 the comment above, w.r.t. the standard library. |
||
|
||
|
||
__append_word_to_user_agent(f"galaxy/{VERSION}") | ||
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. This will be triggered by just importing the module, which may be a surprising side effect in many contexts. It may be better to have a function that can be imported and then called explicitly. That would also simplify testing of this module. |
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.
We'll have to find another way to do this, we shouldn't do monkey patching on a third party library