Skip to content

Commit

Permalink
fix: rename requests.py into requests_utils.py (#5099)
Browse files Browse the repository at this point in the history
* requests.py -> requests_utils.py

* fix tests

* reimport requrests

* fix more tests

* review feedback
  • Loading branch information
ZanSara authored Jun 12, 2023
1 parent 72fe43a commit 49e037a
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion haystack/nodes/prompt/invocation_layer/anthropic_claude.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
AnthropicTokenStreamingHandler,
DefaultTokenStreamingHandler,
)
from haystack.utils.requests import request_with_retry
from haystack.utils import request_with_retry
from haystack.environment import HAYSTACK_REMOTE_API_MAX_RETRIES, HAYSTACK_REMOTE_API_TIMEOUT_SEC

ANTHROPIC_TIMEOUT = float(os.environ.get(HAYSTACK_REMOTE_API_TIMEOUT_SEC, 30))
Expand Down
2 changes: 1 addition & 1 deletion haystack/nodes/prompt/invocation_layer/cohere.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
DefaultTokenStreamingHandler,
)
from haystack.nodes.prompt.invocation_layer.handlers import DefaultPromptHandler
from haystack.utils.requests import request_with_retry
from haystack.utils.requests_utils import request_with_retry

logger = logging.getLogger(__name__)
TIMEOUT = float(os.environ.get(HAYSTACK_REMOTE_API_TIMEOUT_SEC, 30))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
DefaultTokenStreamingHandler,
)
from haystack.nodes.prompt.invocation_layer.handlers import DefaultPromptHandler
from haystack.utils.requests import request_with_retry
from haystack.utils.requests_utils import request_with_retry

logger = logging.getLogger(__name__)
HF_TIMEOUT = float(os.environ.get(HAYSTACK_REMOTE_API_TIMEOUT_SEC, 30))
Expand Down
2 changes: 1 addition & 1 deletion haystack/preview/components/audio/whisper_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path
from dataclasses import dataclass

from haystack.utils.requests import request_with_retry
from haystack.utils.requests_utils import request_with_retry
from haystack.preview import component, ComponentInput, ComponentOutput, Document

logger = logging.getLogger(__name__)
Expand Down
1 change: 1 addition & 0 deletions haystack/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from haystack.utils.reflection import args_to_kwargs
from haystack.utils.requests_utils import request_with_retry
from haystack.utils.preprocessing import convert_files_to_docs, tika_convert_files_to_docs
from haystack.utils.import_utils import fetch_archive_from_http
from haystack.utils.cleaning import clean_wiki_text
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions test/preview/components/audio/test_whisper_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_run_with_path(self, preview_samples_path):
mock_response.content = '{"text": "test transcription", "other_metadata": ["other", "meta", "data"]}'
comp = RemoteWhisperTranscriber(api_key="whatever")

with patch("haystack.utils.requests.requests") as mocked_requests:
with patch("haystack.utils.requests_utils.requests") as mocked_requests:
mocked_requests.request.return_value = mock_response

result = comp.run(
Expand All @@ -67,7 +67,7 @@ def test_run_with_str(self, preview_samples_path):
mock_response.content = '{"text": "test transcription", "other_metadata": ["other", "meta", "data"]}'
comp = RemoteWhisperTranscriber(api_key="whatever")

with patch("haystack.utils.requests.requests") as mocked_requests:
with patch("haystack.utils.requests_utils.requests") as mocked_requests:
mocked_requests.request.return_value = mock_response

result = comp.run(
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_transcribe_with_stream(self, preview_samples_path):
mock_response.content = '{"text": "test transcription", "other_metadata": ["other", "meta", "data"]}'
comp = RemoteWhisperTranscriber(api_key="whatever")

with patch("haystack.utils.requests.requests") as mocked_requests:
with patch("haystack.utils.requests_utils.requests") as mocked_requests:
mocked_requests.request.return_value = mock_response

with open(preview_samples_path / "audio" / "this is the content of the document.wav", "rb") as audio_stream:
Expand All @@ -113,7 +113,7 @@ def test_api_transcription(self, preview_samples_path):
mock_response.content = '{"text": "test transcription", "other_metadata": ["other", "meta", "data"]}'
comp = RemoteWhisperTranscriber(api_key="whatever")

with patch("haystack.utils.requests.requests") as mocked_requests:
with patch("haystack.utils.requests_utils.requests") as mocked_requests:
mocked_requests.request.return_value = mock_response

comp.run(
Expand All @@ -138,7 +138,7 @@ def test_api_translation(self, preview_samples_path):
mock_response.content = '{"text": "test transcription", "other_metadata": ["other", "meta", "data"]}'
comp = RemoteWhisperTranscriber(api_key="whatever")

with patch("haystack.utils.requests.requests") as mocked_requests:
with patch("haystack.utils.requests_utils.requests") as mocked_requests:
mocked_requests.request.return_value = mock_response

comp.run(
Expand Down
12 changes: 6 additions & 6 deletions test/utils/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import pytest
import requests

from haystack.utils.requests import request_with_retry
from haystack.utils.requests_utils import request_with_retry


@pytest.mark.unit
@patch("haystack.utils.requests.requests.request")
@patch("haystack.utils.requests_utils.requests.request")
def test_request_with_retry_defaults_successfully(mock_request):
# Make requests with default retry configuration
request_with_retry(method="GET", url="https://example.com")
Expand All @@ -17,7 +17,7 @@ def test_request_with_retry_defaults_successfully(mock_request):


@pytest.mark.unit
@patch("haystack.utils.requests.requests.request")
@patch("haystack.utils.requests_utils.requests.request")
def test_request_with_retry_custom_timeout(mock_request):
# Make requests with default retry configuration
request_with_retry(method="GET", url="https://example.com", timeout=5)
Expand All @@ -27,7 +27,7 @@ def test_request_with_retry_custom_timeout(mock_request):


@pytest.mark.unit
@patch("haystack.utils.requests.requests.request")
@patch("haystack.utils.requests_utils.requests.request")
def test_request_with_retry_failing_request_and_expected_status_code(mock_request):
# Create fake failed response with status code that triggers retry
fake_response = requests.Response()
Expand All @@ -43,7 +43,7 @@ def test_request_with_retry_failing_request_and_expected_status_code(mock_reques


@pytest.mark.unit
@patch("haystack.utils.requests.requests.request")
@patch("haystack.utils.requests_utils.requests.request")
def test_request_with_retry_failing_request_and_ignored_status_code(mock_request):
# Create fake failed response with status code that doesn't trigger retry
fake_response = requests.Response()
Expand All @@ -59,7 +59,7 @@ def test_request_with_retry_failing_request_and_ignored_status_code(mock_request


@pytest.mark.unit
@patch("haystack.utils.requests.requests.request")
@patch("haystack.utils.requests_utils.requests.request")
def test_request_with_retry_timed_out_request(mock_request: Mock):
# Make request fail cause of a timeout
mock_request.side_effect = TimeoutError()
Expand Down

0 comments on commit 49e037a

Please sign in to comment.