From 8cdca0f343e27b065d9c49f6b09b1be7aad0e246 Mon Sep 17 00:00:00 2001 From: Nicola Soranzo Date: Wed, 22 May 2024 09:16:28 +0100 Subject: [PATCH] Revert some requests import changes introduced in https://github.com/galaxyproject/galaxy/pull/18003 . In particular, when using requests in the tests to connect to Galaxy or the ToolShed it's not needed to use the modified headers. Also: - Move some imports after conditional imports. - Fix type annotations in `lib/tool_shed/test/base/populators.py` - Fix typo bug in `test/unit/util/test_get_url.py` --- lib/galaxy/app.py | 1 - lib/galaxy/tools/data_fetch.py | 1 - lib/galaxy/tools/error_reports/plugins/gitlab.py | 8 +++++--- lib/galaxy/util/__init__.py | 3 +-- lib/galaxy_test/api/test_drs.py | 3 ++- lib/galaxy_test/api/test_jobs.py | 2 +- lib/galaxy_test/base/api.py | 2 +- lib/galaxy_test/base/populators.py | 2 +- lib/galaxy_test/selenium/framework.py | 2 +- lib/tool_shed/test/base/api_util.py | 2 +- lib/tool_shed/test/base/populators.py | 6 +++--- lib/tool_shed/test/base/twilltestcase.py | 2 +- scripts/api/fetch_to_library.py | 3 +-- scripts/api/search.py | 2 +- scripts/api/upload_to_history.py | 9 +-------- scripts/resumable_upload.py | 3 +-- scripts/tool_shed/api/common.py | 2 +- test/integration/test_interactivetools_api.py | 2 +- test/integration/test_job_files.py | 2 +- test/unit/tool_shed/test_shed_index.py | 2 +- test/unit/util/test_get_url.py | 8 +++----- test/unit/webapps/test_client_disconnect.py | 6 ++---- 22 files changed, 30 insertions(+), 43 deletions(-) diff --git a/lib/galaxy/app.py b/lib/galaxy/app.py index eec2318de4e9..1d65289f499a 100644 --- a/lib/galaxy/app.py +++ b/lib/galaxy/app.py @@ -152,7 +152,6 @@ build_tours_registry, ToursRegistry, ) -from galaxy.util import user_agent # noqa: F401 from galaxy.util import ( ExecutionTimer, heartbeat, diff --git a/lib/galaxy/tools/data_fetch.py b/lib/galaxy/tools/data_fetch.py index 44cc055a03c9..a6786c725dc9 100644 --- a/lib/galaxy/tools/data_fetch.py +++ b/lib/galaxy/tools/data_fetch.py @@ -26,7 +26,6 @@ stream_to_file, stream_url_to_file, ) -from galaxy.util import user_agent # noqa: F401 from galaxy.util import ( in_directory, safe_makedirs, diff --git a/lib/galaxy/tools/error_reports/plugins/gitlab.py b/lib/galaxy/tools/error_reports/plugins/gitlab.py index ec74da3b0664..4192bc0c5e9b 100644 --- a/lib/galaxy/tools/error_reports/plugins/gitlab.py +++ b/lib/galaxy/tools/error_reports/plugins/gitlab.py @@ -4,13 +4,15 @@ import os import urllib.parse -from galaxy.util import requests - try: import gitlab except ImportError: gitlab = None -from galaxy.util import string_as_bool + +from galaxy.util import ( + requests, + string_as_bool, +) from .base_git import BaseGitPlugin log = logging.getLogger(__name__) diff --git a/lib/galaxy/util/__init__.py b/lib/galaxy/util/__init__.py index 96e7edff7c23..ca844c2358fc 100644 --- a/lib/galaxy/util/__init__.py +++ b/lib/galaxy/util/__init__.py @@ -67,8 +67,6 @@ Self, ) -from galaxy.util import requests - try: import grp except ImportError: @@ -125,6 +123,7 @@ def XML(text: Union[str, bytes]) -> Element: XML, ) +from . import requests from .custom_logging import get_logger from .inflection import Inflector from .path import ( # noqa: F401 diff --git a/lib/galaxy_test/api/test_drs.py b/lib/galaxy_test/api/test_drs.py index 095839959540..038a1c25c7e8 100644 --- a/lib/galaxy_test/api/test_drs.py +++ b/lib/galaxy_test/api/test_drs.py @@ -9,6 +9,8 @@ urlparse, ) +import requests + from galaxy.files import ( ConfiguredFileSources, DictFileSourcesUserContext, @@ -18,7 +20,6 @@ fetch_drs_to_file, RetryOptions, ) -from galaxy.util import requests from galaxy.util.config_parsers import parse_allowlist_ips from galaxy_test.base.populators import DatasetPopulator from ._framework import ApiTestCase diff --git a/lib/galaxy_test/api/test_jobs.py b/lib/galaxy_test/api/test_jobs.py index 73214d422b85..09193ea2065e 100644 --- a/lib/galaxy_test/api/test_jobs.py +++ b/lib/galaxy_test/api/test_jobs.py @@ -7,9 +7,9 @@ from unittest import SkipTest import pytest +import requests from dateutil.parser import isoparse -from galaxy.util import requests from galaxy_test.api.test_tools import TestsTools from galaxy_test.base.api_asserts import assert_status_code_is_ok from galaxy_test.base.populators import ( diff --git a/lib/galaxy_test/base/api.py b/lib/galaxy_test/base/api.py index a25411873f08..63e33dd5e90a 100644 --- a/lib/galaxy_test/base/api.py +++ b/lib/galaxy_test/base/api.py @@ -11,9 +11,9 @@ ) import pytest +import requests from typing_extensions import Protocol -from galaxy.util import requests from galaxy.util.properties import get_from_env from .api_asserts import ( assert_error_code_is, diff --git a/lib/galaxy_test/base/populators.py b/lib/galaxy_test/base/populators.py index 58b0039e45b5..c1c2791933b0 100644 --- a/lib/galaxy_test/base/populators.py +++ b/lib/galaxy_test/base/populators.py @@ -69,6 +69,7 @@ from uuid import UUID import cwltest.compare +import requests import yaml from bioblend.galaxyclient import GalaxyClient from gxformat2 import ( @@ -98,7 +99,6 @@ from galaxy.util import ( DEFAULT_SOCKET_TIMEOUT, galaxy_root_path, - requests, UNKNOWN, ) from galaxy.util.resources import resource_string diff --git a/lib/galaxy_test/selenium/framework.py b/lib/galaxy_test/selenium/framework.py index e551cc6badb1..310c88c88053 100644 --- a/lib/galaxy_test/selenium/framework.py +++ b/lib/galaxy_test/selenium/framework.py @@ -18,6 +18,7 @@ TYPE_CHECKING, ) +import requests import yaml from gxformat2 import ( convert_and_import_workflow, @@ -38,7 +39,6 @@ asbool, classproperty, DEFAULT_SOCKET_TIMEOUT, - requests, ) from galaxy.util.unittest_utils import skip_if_github_down from galaxy_test.base import populators diff --git a/lib/tool_shed/test/base/api_util.py b/lib/tool_shed/test/base/api_util.py index 436fc4839c43..e90a495aa69b 100644 --- a/lib/tool_shed/test/base/api_util.py +++ b/lib/tool_shed/test/base/api_util.py @@ -9,9 +9,9 @@ ) from urllib.parse import urljoin +import requests from typing_extensions import Literal -from galaxy.util import requests from galaxy_test.base.api_asserts import ( assert_has_keys, assert_status_code_is, diff --git a/lib/tool_shed/test/base/populators.py b/lib/tool_shed/test/base/populators.py index 5b7c9c18c3d5..01cc8eea6937 100644 --- a/lib/tool_shed/test/base/populators.py +++ b/lib/tool_shed/test/base/populators.py @@ -8,9 +8,9 @@ Union, ) +import requests from typing_extensions import Protocol -from galaxy.util import requests from galaxy.util.resources import ( as_file, resource_path, @@ -189,7 +189,7 @@ def get_install_info(self, repository_metadata: RepositoryMetadata) -> InstallIn api_asserts.assert_status_code_is_ok(revisions_response) return from_legacy_install_info(revisions_response.json()) - def update_column_maker_repo(self, repository: HasRepositoryId) -> requests.Response: + def update_column_maker_repo(self, repository: HasRepositoryId) -> RepositoryUpdate: response = self.upload_revision( repository, COLUMN_MAKER_1_1_1_PATH, @@ -211,7 +211,7 @@ def upload_revision_raw( def upload_revision( self, repository: HasRepositoryId, path: Traversable, commit_message: str = DEFAULT_COMMIT_MESSAGE - ): + ) -> RepositoryUpdate: response = self.upload_revision_raw(repository, path, commit_message=commit_message) if response.status_code != 200: response_json = None diff --git a/lib/tool_shed/test/base/twilltestcase.py b/lib/tool_shed/test/base/twilltestcase.py index b6bbe720f5c7..87eaf2c3ab8d 100644 --- a/lib/tool_shed/test/base/twilltestcase.py +++ b/lib/tool_shed/test/base/twilltestcase.py @@ -25,6 +25,7 @@ ) import pytest +import requests from mercurial import ( commands, hg, @@ -52,7 +53,6 @@ from galaxy.tool_shed.util.repository_util import check_for_updates from galaxy.util import ( DEFAULT_SOCKET_TIMEOUT, - requests, smart_str, ) from galaxy_test.base.api_asserts import assert_status_code_is_ok diff --git a/scripts/api/fetch_to_library.py b/scripts/api/fetch_to_library.py index 89b6b3e43866..c291762edfa8 100644 --- a/scripts/api/fetch_to_library.py +++ b/scripts/api/fetch_to_library.py @@ -1,10 +1,9 @@ import argparse import json +import requests import yaml -from galaxy.util import requests - def main(): parser = argparse.ArgumentParser(description="Upload a directory into a data library") diff --git a/scripts/api/search.py b/scripts/api/search.py index 0f9f77c702b6..14e30f1866a5 100644 --- a/scripts/api/search.py +++ b/scripts/api/search.py @@ -5,7 +5,7 @@ import json import sys -from galaxy.util import requests +import requests class RemoteGalaxy: diff --git a/scripts/api/upload_to_history.py b/scripts/api/upload_to_history.py index 54c2e2e4ce28..17a8f7ef7642 100755 --- a/scripts/api/upload_to_history.py +++ b/scripts/api/upload_to_history.py @@ -7,14 +7,7 @@ import os import sys -try: - from galaxy.util import requests -except ImportError: - print( - "Could not import the requests module. See http://docs.python-requests.org/en/latest/" - " or install with 'pip install requests'" - ) - raise +import requests def upload_file(base_url, api_key, history_id, filepath, **kwargs): diff --git a/scripts/resumable_upload.py b/scripts/resumable_upload.py index 18b58c1ef7f9..32fd67ff2fa8 100644 --- a/scripts/resumable_upload.py +++ b/scripts/resumable_upload.py @@ -2,11 +2,10 @@ import os import click +import requests from tusclient import client from tusclient.storage import filestorage -from galaxy.util import requests - UPLOAD_ENDPOINT = "/api/upload/resumable_upload" SUBMISSION_ENDPOINT = "/api/tools/fetch" CHUNK_SIZE = 10**7 diff --git a/scripts/tool_shed/api/common.py b/scripts/tool_shed/api/common.py index a6ffe359f732..fb1ec34bba2f 100644 --- a/scripts/tool_shed/api/common.py +++ b/scripts/tool_shed/api/common.py @@ -1,4 +1,4 @@ -from galaxy.util import requests +import requests def delete(api_key, url, data, return_formatted=True): diff --git a/test/integration/test_interactivetools_api.py b/test/integration/test_interactivetools_api.py index a6b00f5da2b5..7058070be0bc 100644 --- a/test/integration/test_interactivetools_api.py +++ b/test/integration/test_interactivetools_api.py @@ -10,8 +10,8 @@ ) import pytest +import requests -from galaxy.util import requests from galaxy_test.base import api_asserts from galaxy_test.base.populators import ( DatasetPopulator, diff --git a/test/integration/test_job_files.py b/test/integration/test_job_files.py index fb21fbd98ede..36babdcebf6e 100644 --- a/test/integration/test_job_files.py +++ b/test/integration/test_job_files.py @@ -20,6 +20,7 @@ import tempfile from typing import Dict +import requests from sqlalchemy import select from tusclient import client @@ -28,7 +29,6 @@ ensure_object_added_to_session, transaction, ) -from galaxy.util import requests from galaxy_test.base import api_asserts from galaxy_test.base.populators import DatasetPopulator from galaxy_test.driver import integration_util diff --git a/test/unit/tool_shed/test_shed_index.py b/test/unit/tool_shed/test_shed_index.py index d3fbfe7a8d4e..ea5d5ef1bba4 100644 --- a/test/unit/tool_shed/test_shed_index.py +++ b/test/unit/tool_shed/test_shed_index.py @@ -6,9 +6,9 @@ from io import BytesIO import pytest +import requests from whoosh import index -from galaxy.util import requests from tool_shed.util.shed_index import build_index URL = "https://github.com/mvdbeek/toolshed-test-data/blob/master/toolshed_community_files.tgz?raw=true" diff --git a/test/unit/util/test_get_url.py b/test/unit/util/test_get_url.py index f1cb2bd3c049..e410633bd3f2 100644 --- a/test/unit/util/test_get_url.py +++ b/test/unit/util/test_get_url.py @@ -1,11 +1,9 @@ import pytest +import requests import responses from werkzeug.wrappers.response import Response -from galaxy.util import ( - requests, - url_get, -) +from galaxy.util import url_get @responses.activate @@ -29,7 +27,7 @@ def test_get_url_retry_after(httpserver): attempts = [] def retry_handler(request): - attempts.append(requests) + attempts.append(request) if len(attempts) < 4: return Response("try again later", status=429, content_type="text/plain") else: diff --git a/test/unit/webapps/test_client_disconnect.py b/test/unit/webapps/test_client_disconnect.py index 89adb590136a..0381d40cebc0 100644 --- a/test/unit/webapps/test_client_disconnect.py +++ b/test/unit/webapps/test_client_disconnect.py @@ -5,16 +5,14 @@ from typing import Optional import pytest +import requests import uvicorn from fastapi import status from fastapi.applications import FastAPI from requests import ReadTimeout from starlette.middleware.base import BaseHTTPMiddleware -from galaxy.util import ( - requests, - sockets, -) +from galaxy.util import sockets error_encountered: Optional[str] = None