From c147faea17d20bc5c0900ab7a59b5b1923e03c6e Mon Sep 17 00:00:00 2001 From: William Smith Date: Sat, 2 Sep 2023 18:50:20 +0100 Subject: [PATCH] Add handling for all requests --- bioblend/galaxy/__init__.py | 3 +- bioblend/galaxy/objects/galaxy_instance.py | 3 +- bioblend/galaxyclient.py | 161 ++++++++++++++------- 3 files changed, 116 insertions(+), 51 deletions(-) diff --git a/bioblend/galaxy/__init__.py b/bioblend/galaxy/__init__.py index ea2cd47cb..795f821bc 100644 --- a/bioblend/galaxy/__init__.py +++ b/bioblend/galaxy/__init__.py @@ -108,7 +108,8 @@ def __init__( self.tool_data = tool_data.ToolDataClient(self) self.folders = folders.FoldersClient(self) self.tool_dependencies = tool_dependencies.ToolDependenciesClient(self) - self.session = session + if session is not None: + self.session = session def __repr__(self) -> str: """ diff --git a/bioblend/galaxy/objects/galaxy_instance.py b/bioblend/galaxy/objects/galaxy_instance.py index 00102aacf..08ed84025 100644 --- a/bioblend/galaxy/objects/galaxy_instance.py +++ b/bioblend/galaxy/objects/galaxy_instance.py @@ -70,7 +70,8 @@ def __init__( self.invocations = client.ObjInvocationClient(self) self.tools = client.ObjToolClient(self) self.jobs = client.ObjJobClient(self) - self.session = session + if session is not None: + self.session = session def _wait_datasets( self, datasets: Iterable[wrappers.Dataset], polling_interval: float, break_on_error: bool = True diff --git a/bioblend/galaxyclient.py b/bioblend/galaxyclient.py index 41569f175..4bff70997 100644 --- a/bioblend/galaxyclient.py +++ b/bioblend/galaxyclient.py @@ -55,11 +55,18 @@ def __init__( for scheme in ("https://", "http://"): log.warning(f"Missing scheme in url, trying with {scheme}") with contextlib.suppress(requests.RequestException): - r = requests.get( - scheme + url, - timeout=self.timeout, - verify=self.verify, - ) + if session is not None: + r = session.get( + scheme + url, + timeout=self.timeout, + verify=self.verify, + ) + else: + r = requests.get( + scheme + url, + timeout=self.timeout, + verify=self.verify, + ) r.raise_for_status() found_scheme = scheme break @@ -84,6 +91,8 @@ def __init__( self._max_get_attempts = 1 # Delay in seconds between subsequent retries. self._get_retry_delay = 10.0 + # Add session to the client if provided + self.session = session @property def max_get_attempts(self) -> int: @@ -134,7 +143,10 @@ def make_get_request(self, url: str, **kwargs: Any) -> requests.Response: headers = self.json_headers kwargs.setdefault("timeout", self.timeout) kwargs.setdefault("verify", self.verify) - r = self.session.get(url, headers=headers, **kwargs) + if self.session is not None: + r = self.session.get(url, headers=headers, **kwargs) + else: + r = requests.get(url, headers=headers, **kwargs) return r def make_post_request( @@ -174,16 +186,26 @@ def my_dumps(d: dict) -> dict: data = json.dumps(payload) if payload is not None else None headers = self.json_headers post_params = params - - r = requests.post( - url, - params=post_params, - data=data, - headers=headers, - timeout=self.timeout, - allow_redirects=False, - verify=self.verify, - ) + if self.session is not None: + r = self.session.post( + url, + params=post_params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) + else: + r = requests.post( + url, + params=post_params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) if r.status_code == 200: try: return r.json() @@ -215,15 +237,26 @@ def make_delete_request( """ data = json.dumps(payload) if payload is not None else None headers = self.json_headers - r = requests.delete( - url, - params=params, - data=data, - headers=headers, - timeout=self.timeout, - allow_redirects=False, - verify=self.verify, - ) + if self.session is not None: + r = self.session.delete( + url, + params=params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) + else: + r = requests.delete( + url, + params=params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) return r def make_put_request(self, url: str, payload: Optional[dict] = None, params: Optional[dict] = None) -> Any: @@ -237,15 +270,26 @@ def make_put_request(self, url: str, payload: Optional[dict] = None, params: Opt """ data = json.dumps(payload) if payload is not None else None headers = self.json_headers - r = requests.put( - url, - params=params, - data=data, - headers=headers, - timeout=self.timeout, - allow_redirects=False, - verify=self.verify, - ) + if self.session is not None: + r = self.session.put( + url, + params=params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) + else: + r = requests.put( + url, + params=params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) if r.status_code == 200: try: return r.json() @@ -273,15 +317,26 @@ def make_patch_request(self, url: str, payload: Optional[dict] = None, params: O """ data = json.dumps(payload) if payload is not None else None headers = self.json_headers - r = requests.patch( - url, - params=params, - data=data, - headers=headers, - timeout=self.timeout, - allow_redirects=False, - verify=self.verify, - ) + if self.session is not None: + r = self.session.patch( + url, + params=params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) + else: + r = requests.patch( + url, + params=params, + data=data, + headers=headers, + timeout=self.timeout, + allow_redirects=False, + verify=self.verify, + ) if r.status_code == 200: try: return r.json() @@ -356,12 +411,20 @@ def key(self) -> Optional[str]: auth_url = f"{self.url}/authenticate/baseauth" # Use lower level method instead of make_get_request() because we # need the additional Authorization header. - r = requests.get( - auth_url, - headers=headers, - timeout=self.timeout, - verify=self.verify, - ) + if self.session is not None: + r = self.session.get( + auth_url, + headers=headers, + timeout=self.timeout, + verify=self.verify, + ) + else: + r = requests.get( + auth_url, + headers=headers, + timeout=self.timeout, + verify=self.verify, + ) if r.status_code != 200: raise Exception("Failed to authenticate user.") response = r.json()