Skip to content

Commit 04e3034

Browse files
feat: Added timeout param
1 parent d064a9c commit 04e3034

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "api-to-dataframe"
3-
version = "1.0.3"
3+
version = "1.1.0"
44
description = "A package to convert API responses to pandas dataframe"
55
authors = ["IvanildoBarauna <[email protected]>"]
66
readme = "README.md"

src/api_to_dataframe/controller/client_builder.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,17 @@
33

44

55
class ClientBuilder:
6-
"""
7-
Builder for creating clients that interact with an API endpoint and return data.
8-
9-
Attributes:
10-
endpoint (str): The API endpoint to be accessed.
11-
retry_strategy (RetryStrategies): The retry strategy for the request. Default is NoRetryStrategy.
12-
"""
13-
14-
def __init__(self, endpoint: str, retry_strategy: RetryStrategies = RetryStrategies.NoRetryStrategy):
6+
def __init__(self,
7+
endpoint: str,
8+
retry_strategy: RetryStrategies = RetryStrategies.NoRetryStrategy,
9+
timeout: int = 5):
1510
"""
1611
Initializes an instance of ClientBuilder.
1712
1813
Args:
1914
endpoint (str): The API endpoint to be accessed.
2015
retry_strategy (RetryStrategies, optional): The retry strategy for the request. Default is NoRetryStrategy.
16+
timeout (int, optional): The timeout for the request. Default is 5 seconds.
2117
2218
Raises:
2319
ValueError: If the endpoint is empty.
@@ -27,6 +23,7 @@ def __init__(self, endpoint: str, retry_strategy: RetryStrategies = RetryStrateg
2723
else:
2824
self.endpoint = endpoint
2925
self.retry_strategy = retry_strategy
26+
self.timeout = timeout
3027

3128
def get_api_data(self):
3229
"""
@@ -35,7 +32,7 @@ def get_api_data(self):
3532
Returns:
3633
dict: The response from the API.
3734
"""
38-
response = GetData.get_response(self.endpoint, self.retry_strategy)
35+
response = GetData.get_response(self.endpoint, self.retry_strategy, self.timeout)
3936
return response
4037

4138
@staticmethod

src/api_to_dataframe/models/get_data.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
import requests
2+
from requests.exceptions import HTTPError, Timeout, RequestException
23
import pandas as pd
34

45
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
56

67

78
class GetData:
89
@staticmethod
9-
def get_response(endpoint: str, RetryStrategies: RetryStrategies):
10-
response = requests.get(endpoint)
11-
12-
if response.ok:
13-
return response.json()
10+
def get_response(endpoint: str, retry_strategies: RetryStrategies, timeout: int):
11+
try:
12+
response = requests.get(endpoint, timeout=timeout)
13+
response.raise_for_status()
14+
except HTTPError as http_err:
15+
print(f'HTTP error occurred: {http_err}')
16+
except Timeout as timeout_err:
17+
print(f'Timeout error occurred: {timeout_err}')
18+
except RequestException as req_err:
19+
print(f'Error occurred: {req_err}')
1420
else:
15-
raise ConnectionError(response.status_code)
21+
return response
1622

1723
@staticmethod
1824
def to_dataframe(response):
19-
df = pd.DataFrame(response)
25+
try:
26+
df = pd.DataFrame(response.json())
27+
except Exception as err:
28+
raise TypeError(f"Invalid response for transform in dataframe: {err}")
29+
2030
if df.empty:
2131
raise ValueError("::: DataFrame is empty :::")
2232
else:
2333
return df
24-

tests/test_run.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import pytest
22
import pandas as pd
3+
import requests
4+
35
from api_to_dataframe import ClientBuilder
46

57

@@ -29,7 +31,7 @@ def test_constructor_with_param(setup):
2931
def test_response_to_json(setup):
3032
new_client = setup
3133
response = new_client.get_api_data()
32-
assert isinstance(response, dict)
34+
assert isinstance(response, requests.Response)
3335

3436

3537
def test_to_dataframe(response_setup):

0 commit comments

Comments
 (0)