Skip to content

Commit

Permalink
Add handling for all requests
Browse files Browse the repository at this point in the history
  • Loading branch information
williamjsmith15 committed Sep 19, 2023
1 parent d34286f commit c147fae
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 51 deletions.
3 changes: 2 additions & 1 deletion bioblend/galaxy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
"""
Expand Down
3 changes: 2 additions & 1 deletion bioblend/galaxy/objects/galaxy_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
161 changes: 112 additions & 49 deletions bioblend/galaxyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c147fae

Please sign in to comment.