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

Allow specifying the User-Agent when creating a GalaxyClient #318

Merged
merged 2 commits into from
Jul 9, 2024
Merged
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
3 changes: 2 additions & 1 deletion bioblend/galaxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def __init__(
password: Optional[str] = None,
*,
verify: bool = True,
user_agent: Optional[str] = None,
) -> None:
"""
A base representation of a connection to a Galaxy instance, identified
Expand Down Expand Up @@ -83,7 +84,7 @@ def __init__(
:param verify: Whether to verify the server's TLS certificate
:type verify: bool
"""
super().__init__(url, key=key, email=email, password=password, verify=verify)
super().__init__(url, key=key, email=email, password=password, verify=verify, user_agent=user_agent)
self.libraries = libraries.LibraryClient(self)
self.histories = histories.HistoryClient(self)
self.workflows = workflows.WorkflowClient(self)
Expand Down
5 changes: 4 additions & 1 deletion bioblend/galaxy/objects/galaxy_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ def __init__(
password: Optional[str] = None,
*,
verify: bool = True,
user_agent: Optional[str] = None,
) -> None:
self.gi = bioblend.galaxy.GalaxyInstance(url, key=api_key, email=email, password=password, verify=verify)
self.gi = bioblend.galaxy.GalaxyInstance(
url, key=api_key, email=email, password=password, verify=verify, user_agent=user_agent
)
self.log = bioblend.log
self.datasets = client.ObjDatasetClient(self)
self.dataset_collections = client.ObjDatasetCollectionClient(self)
Expand Down
15 changes: 10 additions & 5 deletions bioblend/galaxyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import logging
from typing import (
Any,
Dict,
Optional,
Union,
)

import requests
Expand Down Expand Up @@ -40,6 +42,7 @@ def __init__(
*,
verify: bool = True,
timeout: Optional[float] = None,
user_agent: Optional[str] = None,
) -> None:
"""
:param verify: Whether to verify the server's TLS certificate
Expand Down Expand Up @@ -78,7 +81,9 @@ def __init__(
self._key = None
self.email = email
self.password = password
self.json_headers: dict = {"Content-Type": "application/json"}
self.json_headers: Dict[str, Union[str, bytes, None]] = {"Content-Type": "application/json"}
if user_agent:
self.json_headers["User-Agent"] = user_agent
# json_headers needs to be set before key can be defined, otherwise authentication with email/password causes an error
self.json_headers["x-api-key"] = self.key
# Number of attempts before giving up on a GET request.
Expand Down Expand Up @@ -330,15 +335,14 @@ def get_tus_uploader(
"""
headers = {"x-api-key": self.key}
client = tusclient.client.TusClient(self.url + url, headers=headers)
if storage:
storage = tusclient.storage.filestorage.FileStorage(storage)
url_storage = tusclient.storage.filestorage.FileStorage(storage) if storage else None
try:
return client.uploader(
file_path=path,
chunk_size=chunk_size,
metadata=metadata,
store_url=storage is not None,
url_storage=storage,
store_url=url_storage is not None,
url_storage=url_storage,
)
except tusclient.exceptions.TusCommunicationError as exc:
raise ConnectionError(
Expand Down Expand Up @@ -374,6 +378,7 @@ def key(self) -> Optional[str]:


def _tus_uploader_session_id(self: tusclient.uploader.Uploader) -> str:
assert self.url
return self.url.rsplit("/", 1)[1]


Expand Down
3 changes: 2 additions & 1 deletion bioblend/toolshed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(
password: Optional[str] = None,
*,
verify: bool = True,
user_agent: Optional[str] = None,
) -> None:
"""
A base representation of a connection to a ToolShed instance, identified
Expand Down Expand Up @@ -61,7 +62,7 @@ def __init__(
:param verify: Whether to verify the server's TLS certificate
:type verify: bool
"""
super().__init__(url, key=key, email=email, password=password, verify=verify)
super().__init__(url, key=key, email=email, password=password, verify=verify, user_agent=user_agent)
self.categories = categories.ToolShedCategoryClient(self)
self.repositories = repositories.ToolShedRepositoryClient(self)
self.tools = tools.ToolShedToolClient(self)