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 5 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 @@ -138,6 +138,7 @@
heartbeat,
listify,
StructuredExecutionTimer,
user_agent,
)
from galaxy.util.task import IntervalTask
from galaxy.util.tool_shed import tool_shed_registry
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 @@ -29,6 +29,7 @@
from galaxy.util import (
in_directory,
safe_makedirs,
user_agent,
)
from galaxy.util.bunch import Bunch
from galaxy.util.compression_utils import CompressedFile
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


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("galaxy")
Copy link
Member

@hexylena hexylena Mar 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could also include the galaxy version here as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could also include the galaxy version here as well

Good idea, I added the galaxy version to the User-Agent.

Now, the primary outstanding TODO is to figure out where to import the user_agent package to get full coverage. I probably should defer to y'all on that one, there's probably a best place that those with deeper knowledge of the codebase would know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @hexylena, I'm checking in on this PR. I think it's mostly done, except that someone with deep knowledge of the codebase (probably a regular contributor such as yourself) needs to determine where to import the user_agent package so that the user agent is properly set at all entry points. Please let me know if there's any way I can help.

Loading