From ffdb0cec429fb4f3ac243d36438899ca8e35b821 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Thu, 15 Jun 2023 18:53:48 +0100 Subject: [PATCH] Replace ``urllib.parse.urljoin()`` with f-strings 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". --- bioblend/_tests/TestGalaxyInstance.py | 8 ++++++++ bioblend/galaxy/datasets/__init__.py | 3 +-- bioblend/galaxy/histories/__init__.py | 3 +-- bioblend/galaxyclient.py | 5 ++--- docs/examples/objects/w5_galaxy_api.py | 5 ++--- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bioblend/_tests/TestGalaxyInstance.py b/bioblend/_tests/TestGalaxyInstance.py index 2c7f30004..54b67d343 100644 --- a/bioblend/_tests/TestGalaxyInstance.py +++ b/bioblend/_tests/TestGalaxyInstance.py @@ -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 diff --git a/bioblend/galaxy/datasets/__init__.py b/bioblend/galaxy/datasets/__init__.py index 3fef003de..f33e2582a 100644 --- a/bioblend/galaxy/datasets/__init__.py +++ b/bioblend/galaxy/datasets/__init__.py @@ -16,7 +16,6 @@ TYPE_CHECKING, Union, ) -from urllib.parse import urljoin from requests import Response from typing_extensions import Literal @@ -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() diff --git a/bioblend/galaxy/histories/__init__.py b/bioblend/galaxy/histories/__init__.py index b12737b45..72181a780 100644 --- a/bioblend/galaxy/histories/__init__.py +++ b/bioblend/galaxy/histories/__init__.py @@ -17,7 +17,6 @@ Pattern, Union, ) -from urllib.parse import urljoin from typing_extensions import Literal @@ -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]: diff --git a/bioblend/galaxyclient.py b/bioblend/galaxyclient.py index a99f70c69..3a6c168e0 100644 --- a/bioblend/galaxyclient.py +++ b/bioblend/galaxyclient.py @@ -13,7 +13,6 @@ Any, Optional, ) -from urllib.parse import urljoin import requests import tusclient.client @@ -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 /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: diff --git a/docs/examples/objects/w5_galaxy_api.py b/docs/examples/objects/w5_galaxy_api.py index a74534f21..d5f19897c 100644 --- a/docs/examples/objects/w5_galaxy_api.py +++ b/docs/examples/objects/w5_galaxy_api.py @@ -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")