Skip to content
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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/galaxy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
build_tours_registry,
ToursRegistry,
)
from galaxy.util import user_agent # noqa: F401
from galaxy.util import (
ExecutionTimer,
heartbeat,
Expand Down
1 change: 1 addition & 0 deletions lib/galaxy/tools/data_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
stream_to_file,
stream_url_to_file,
)
from galaxy.util import user_agent # noqa: F401
from galaxy.util import (
in_directory,
safe_makedirs,
Expand Down
34 changes: 34 additions & 0 deletions lib/galaxy/util/user_agent.py
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
Copy link
Member

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


# 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
Copy link
Member

Choose a reason for hiding this comment

The 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}")
Copy link
Member

Choose a reason for hiding this comment

The 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.

Loading