diff --git a/bioblend/galaxy/__init__.py b/bioblend/galaxy/__init__.py index 8c5a652fb..e23d8d930 100644 --- a/bioblend/galaxy/__init__.py +++ b/bioblend/galaxy/__init__.py @@ -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 @@ -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) diff --git a/bioblend/galaxy/objects/galaxy_instance.py b/bioblend/galaxy/objects/galaxy_instance.py index 470d7a11c..2da9648bc 100644 --- a/bioblend/galaxy/objects/galaxy_instance.py +++ b/bioblend/galaxy/objects/galaxy_instance.py @@ -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) diff --git a/bioblend/galaxyclient.py b/bioblend/galaxyclient.py index 559c48cb5..97b5c0aa5 100644 --- a/bioblend/galaxyclient.py +++ b/bioblend/galaxyclient.py @@ -12,7 +12,9 @@ import logging from typing import ( Any, + Dict, Optional, + Union, ) import requests @@ -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 @@ -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. @@ -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( @@ -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] diff --git a/bioblend/toolshed/__init__.py b/bioblend/toolshed/__init__.py index 4408864c4..4644babde 100644 --- a/bioblend/toolshed/__init__.py +++ b/bioblend/toolshed/__init__.py @@ -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 @@ -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)