Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def default_location():
If no location specified in cli, find user's location
Make a GET request to the API endpoint
"""
response = requests.get("https://ipinfo.io/json", timeout=10)
try:
response = requests.get("https://ipinfo.io/json", timeout=10)
response.raise_for_status()
Comment thread
christe6-osu marked this conversation as resolved.
Outdated
except requests.exceptions.Timeout:
return "No data"
Comment thread
christe6-osu marked this conversation as resolved.

if response.status_code == HTTPStatus.OK:
data = response.json()
Expand Down
6 changes: 6 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import pytest
from openmeteo_requests.Client import OpenMeteoRequestsError
from requests.exceptions import Timeout

from src.api import (
default_location,
Expand All @@ -33,6 +34,7 @@
["43.03", "-72.001", "New York"],
),
(HTTPStatus.BAD_REQUEST, {}, "No data"),
(Timeout, {}, "No data"),
],
)
def test_default_location_mocked(
Expand All @@ -46,6 +48,10 @@ def test_default_location_mocked(
# Mock the 'requests.get' method
mock_requests = mocker.patch("requests.get", return_value=mock_response)

# Check for Timeout and set side effect
if status_code == Timeout:
mock_requests.side_effect = Timeout("Test Timeout")
Comment thread
christe6-osu marked this conversation as resolved.
Outdated

# Act: Call the function
result = default_location()

Expand Down