Skip to content

Commit

Permalink
Replace urllib.parse.urljoin() with f-strings
Browse files Browse the repository at this point in the history
because the former generate the wrong URL when passed absolute paths
for Galaxy instances served at a subdirectory, e.g.:

```python
urljoin("https://my.galaxy/subdir", "/dataset/dataset_id/display")
```

returns "https://my.galaxy/dataset/dataset_id/display" instead of
"https://my.galaxy/subdir/dataset/dataset_id/display".
  • Loading branch information
nsoranzo committed Jun 15, 2023
1 parent 2f7288d commit 5c5189f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
8 changes: 8 additions & 0 deletions bioblend/_tests/TestGalaxyInstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def setUp(self):
# "connect" to a fake Galaxy instance
self.gi = GalaxyInstance("http://localhost:56789", key="whatever")

def test_url_attribute(self):
assert self.gi.base_url == "http://localhost:56789"
assert self.gi.url == "http://localhost:56789/api"
# Test instance served at a subdirectory
gi = GalaxyInstance("http://localhost:56789/galaxy/", key="whatever")
assert gi.base_url == "http://localhost:56789/galaxy"
assert gi.url == "http://localhost:56789/galaxy/api"

def test_set_max_get_attempts(self):
self.gi.max_get_attempts = 3
assert 3 == self.gi.max_get_attempts
Expand Down
3 changes: 1 addition & 2 deletions bioblend/galaxy/datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
TYPE_CHECKING,
Union,
)
from urllib.parse import urljoin

from requests import Response
from typing_extensions import Literal
Expand Down Expand Up @@ -86,7 +85,7 @@ def _initiate_download(
# does not work when using REMOTE_USER with access disabled to
# everything but /api without auth
download_url = dataset["download_url"] + "?to_ext=" + file_ext
url = urljoin(self.gi.base_url, download_url)
url = f"{self.gi.base_url}{download_url}"

r = self.gi.make_get_request(url, stream=stream_content)
r.raise_for_status()
Expand Down
3 changes: 1 addition & 2 deletions bioblend/galaxy/histories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
Pattern,
Union,
)
from urllib.parse import urljoin

from typing_extensions import Literal

Expand Down Expand Up @@ -870,7 +869,7 @@ def open_history(self, history_id: str) -> None:
any such tab is recommended.
"""

url = urljoin(self.gi.base_url, f"history/switch_to_history?hist_id={history_id}")
url = f"{self.gi.base_url}/history/switch_to_history?hist_id={history_id}"
webbrowser.open_new_tab(url)

def get_extra_files(self, history_id: str, dataset_id: str) -> List[str]:
Expand Down
5 changes: 2 additions & 3 deletions bioblend/galaxyclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
Any,
Optional,
)
from urllib.parse import urljoin

import requests
import tusclient.client
Expand Down Expand Up @@ -66,9 +65,9 @@ def __init__(
else:
raise ValueError(f"Missing scheme in url {url}")
url = found_scheme + url
self.base_url = url.rstrip("/")
# All of Galaxy's and ToolShed's API's are rooted at <url>/api so make that the url
self.base_url = url
self.url = urljoin(url, "api")
self.url = f"{self.base_url}/api"
# If key has been supplied, use it; otherwise just set email and
# password and grab user's key before first request.
if key:
Expand Down
5 changes: 2 additions & 3 deletions docs/examples/objects/w5_galaxy_api.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import json
import os
import sys
from urllib.parse import urljoin

# This example, provided for comparison with w5_metagenomics.py,
# contains the code required to run the metagenomics workflow
# *without* BioBlend.

URL = os.getenv("GALAXY_URL", "https://orione.crs4.it")
API_URL = urljoin(URL, "api")
URL = os.getenv("GALAXY_URL", "https://orione.crs4.it").rstrip("/")
API_URL = f"{URL}/api"
API_KEY = os.getenv("GALAXY_API_KEY", "YOUR_API_KEY")
if API_KEY == "YOUR_API_KEY":
sys.exit("API_KEY not set, see the README.txt file")
Expand Down

0 comments on commit 5c5189f

Please sign in to comment.