api-to-dataframe 1.0.2__tar.gz → 1.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: api-to-dataframe
3
- Version: 1.0.2
3
+ Version: 1.1.0
4
4
  Summary: A package to convert API responses to pandas dataframe
5
5
  Home-page: https://github.com/IvanildoBarauna/api-to-dataframe
6
6
  License: MIT
@@ -76,10 +76,19 @@ poetry add api-to-dataframe
76
76
  ## Importing library
77
77
  from api_to_dataframe import ClientBuilder, RetryStrategies
78
78
 
79
- # Create a client for the API without retry strategy
80
- client = ClientBuilder(endpoint="https://api.example.com", retry_strategy=RetryStrategies.NoRetryStrategy)
81
- # or with LinearStrategy (In development, actually don't nothing)
82
- client = ClientBuilder(endpoint="https://api.example.com", retry_strategy=RetryStrategies.LinearStrategy)
79
+ # Create a client for simple ingest data from API (timeout 5 seconds)
80
+ client = ClientBuilder(endpoint="https://api.example.com")
81
+
82
+ # if you can define timeout, use: (default is 5 seconds), with LinearStrategy (In development, actually don't nothing) and set headers:
83
+ headers = {
84
+ "application_name": "api_to_dataframe"
85
+ }
86
+ client = ClientBuilder(endpoint="https://api.example.com"
87
+ ,retry_strategy=RetryStrategies.LinearStrategy
88
+ ,timeout=10
89
+ ,headers=headers)
90
+
91
+ ### timeout, retry_strategy and headers are opcionals parameters
83
92
 
84
93
  # Get data from the API
85
94
  data = client.get_api_data()
@@ -51,10 +51,19 @@ poetry add api-to-dataframe
51
51
  ## Importing library
52
52
  from api_to_dataframe import ClientBuilder, RetryStrategies
53
53
 
54
- # Create a client for the API without retry strategy
55
- client = ClientBuilder(endpoint="https://api.example.com", retry_strategy=RetryStrategies.NoRetryStrategy)
56
- # or with LinearStrategy (In development, actually don't nothing)
57
- client = ClientBuilder(endpoint="https://api.example.com", retry_strategy=RetryStrategies.LinearStrategy)
54
+ # Create a client for simple ingest data from API (timeout 5 seconds)
55
+ client = ClientBuilder(endpoint="https://api.example.com")
56
+
57
+ # if you can define timeout, use: (default is 5 seconds), with LinearStrategy (In development, actually don't nothing) and set headers:
58
+ headers = {
59
+ "application_name": "api_to_dataframe"
60
+ }
61
+ client = ClientBuilder(endpoint="https://api.example.com"
62
+ ,retry_strategy=RetryStrategies.LinearStrategy
63
+ ,timeout=10
64
+ ,headers=headers)
65
+
66
+ ### timeout, retry_strategy and headers are opcionals parameters
58
67
 
59
68
  # Get data from the API
60
69
  data = client.get_api_data()
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "api-to-dataframe"
3
- version = "1.0.2"
3
+ version = "1.1.0"
4
4
  description = "A package to convert API responses to pandas dataframe"
5
5
  authors = ["IvanildoBarauna <ivanildo.jnr@outlook.com>"]
6
6
  readme = "README.md"
@@ -22,24 +22,27 @@ Homepage = "https://github.com/IvanildoBarauna/api-to-dataframe"
22
22
  Documentation = "https://github.com/IvanildoBarauna/api-to-dataframe#readme"
23
23
  Issues = "https://github.com/IvanildoBarauna/api-to-dataframe/issues"
24
24
 
25
- [tool.setuptools.packages.find]
26
- where = ["src"]
27
- include = ["api_to_dataframe*"]
25
+ [tool.poetry.dependencies]
26
+ python = "^3.9"
27
+ pandas = "^2.2.2"
28
+ requests = "^2.32.3"
28
29
 
29
30
  [tool.poetry.group.dev.dependencies]
30
31
  poetry-dynamic-versioning = "^1.3.0"
31
32
  pytest = "^8.2.2"
32
33
  coverage = "^7.5.3"
34
+ responses = "^0.25.3"
33
35
 
34
- [tool.poetry.dependencies]
35
- python = "^3.9"
36
- pandas = "^2.2.2"
37
- requests = "^2.32.3"
38
36
 
39
37
  [build-system]
40
38
  requires = ["poetry-core"]
41
39
  build-backend = "poetry.core.masonry.api"
42
40
 
41
+ [tool.setuptools.packages.find]
42
+ where = ["src/"]
43
+ include = ["api_to_dataframe*"]
44
+
45
+
43
46
  [tool.poetry-dynamic-versioning]
44
47
  enable = true
45
48
  versioning = "semantic"
@@ -1,4 +1,2 @@
1
1
  from .controller.client_builder import ClientBuilder
2
2
  from .common.utils.retry_strategies import RetryStrategies
3
-
4
- __all__ = ['ClientBuilder', 'RetryStrategies']
@@ -3,21 +3,18 @@ from api_to_dataframe.models.get_data import GetData
3
3
 
4
4
 
5
5
  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
+ headers: dict = {},
9
+ retry_strategy: RetryStrategies = RetryStrategies.NoRetryStrategy,
10
+ timeout: int = 5):
15
11
  """
16
12
  Initializes an instance of ClientBuilder.
17
13
 
18
14
  Args:
19
15
  endpoint (str): The API endpoint to be accessed.
20
16
  retry_strategy (RetryStrategies, optional): The retry strategy for the request. Default is NoRetryStrategy.
17
+ timeout (int, optional): The timeout for the request. Default is 5 seconds.
21
18
 
22
19
  Raises:
23
20
  ValueError: If the endpoint is empty.
@@ -27,6 +24,8 @@ class ClientBuilder:
27
24
  else:
28
25
  self.endpoint = endpoint
29
26
  self.retry_strategy = retry_strategy
27
+ self.timeout = timeout
28
+ self.headers = headers
30
29
 
31
30
  def get_api_data(self):
32
31
  """
@@ -35,7 +34,7 @@ class ClientBuilder:
35
34
  Returns:
36
35
  dict: The response from the API.
37
36
  """
38
- response = GetData.get_response(self.endpoint, self.retry_strategy)
37
+ response = GetData.get_response(self.endpoint, self.headers, self.retry_strategy, self.timeout)
39
38
  return response
40
39
 
41
40
  @staticmethod
@@ -0,0 +1,36 @@
1
+ import requests
2
+ from requests.exceptions import HTTPError, Timeout, RequestException
3
+ import pandas as pd
4
+
5
+ from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
6
+
7
+
8
+ class GetData:
9
+ @staticmethod
10
+ def get_response(endpoint: str,
11
+ headers: dict,
12
+ retry_strategies: RetryStrategies,
13
+ timeout: int):
14
+ try:
15
+ response = requests.get(endpoint, timeout=timeout, headers=headers)
16
+ response.raise_for_status()
17
+ except HTTPError as http_err:
18
+ print(f'HTTP error occurred: {http_err}')
19
+ raise http_err
20
+ except Timeout as timeout_err:
21
+ print(f'Timeout error occurred: {timeout_err}')
22
+ raise timeout_err
23
+ else:
24
+ return response
25
+
26
+ @staticmethod
27
+ def to_dataframe(response):
28
+ try:
29
+ df = pd.DataFrame(response.json())
30
+ except Exception as err:
31
+ raise TypeError(f"Invalid response for transform in dataframe: {err}")
32
+
33
+ if df.empty:
34
+ raise ValueError("::: DataFrame is empty :::")
35
+ else:
36
+ return df
@@ -1,24 +0,0 @@
1
- import requests
2
- import pandas as pd
3
-
4
- from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
5
-
6
-
7
- class GetData:
8
- @staticmethod
9
- def get_response(endpoint: str, RetryStrategies: RetryStrategies):
10
- response = requests.get(endpoint)
11
-
12
- if response.ok:
13
- return response.json()
14
- else:
15
- raise ConnectionError(response.status_code)
16
-
17
- @staticmethod
18
- def to_dataframe(response):
19
- df = pd.DataFrame(response)
20
- if df.empty:
21
- raise ValueError("::: DataFrame is empty :::")
22
- else:
23
- return df
24
-