Skip to content

Commit 43651d0

Browse files
Merge pull request #29 from IvanildoBarauna/feature-SplitResponsabilites
feat: change entrypoint of lib + split responsabilites in components
2 parents 8d5c213 + 8e72c3b commit 43651d0

File tree

10 files changed

+85
-33
lines changed

10 files changed

+85
-33
lines changed

.github/workflows/CI.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ jobs:
5656
with:
5757
python-version: '3.9'
5858

59-
- name: Install Poetry
59+
- name: Poetry Setup
6060
run: |
6161
pip install --upgrade pip
6262
curl -sSL https://install.python-poetry.org | python3 -
6363
poetry config virtualenvs.create false
6464
poetry install
6565
66-
- name: Increment version
66+
- name: Generate and push Tag
6767
run: |
6868
git config --global user.name 'GitHub Actions'
6969
git config --global user.email 'actions@github.com'

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 = "0.1.0"
3+
version = "1.0.0"
44
description = "A package to convert API responses to pandas dataframe"
55
authors = ["IvanildoBarauna <ivanildo.jnr@outlook.com>"]
66
readme = "README.md"

src/api_to_dataframe/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
from api_to_dataframe.main import ClientBuilder
1+
from api_to_dataframe.controller.client_builder import ClientBuilder
2+
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from enum import Enum
2+
3+
4+
class RetryStrategies(Enum):
5+
NoRetryStrategy = 0
6+
LinearStrategy = 1
7+
ExponentialStrategy = 2
8+
CustomStrategy = 3
9+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
2+
from api_to_dataframe.models.get_data import GetData
3+
4+
5+
class ClientBuilder:
6+
def __init__(self, endpoint: str, retry_strategy: RetryStrategies = RetryStrategies.NoRetryStrategy):
7+
if endpoint == "":
8+
raise ValueError("::: endpoint param is mandatory :::")
9+
else:
10+
self.endpoint = endpoint
11+
self.retry_strategy = retry_strategy
12+
13+
def get_api_data(self):
14+
response = GetData.get_response(self.endpoint, self.retry_strategy)
15+
return response
16+
17+
@staticmethod
18+
def api_to_dataframe(response: dict):
19+
df = GetData.to_dataframe(response)
20+
return df

src/api_to_dataframe/main.py

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
2+
3+
4+
class Retainer:
5+
@staticmethod
6+
def strategy(retry_strategy: RetryStrategies = RetryStrategies.NoRetryStrategy):
7+
if retry_strategy == RetryStrategies.NoRetryStrategy:
8+
print("::: NoRetryStrategy :::")
9+
elif retry_strategy == RetryStrategies.LinearStrategy:
10+
print("::: LinearStrategy :::")
11+
elif retry_strategy == RetryStrategies.ExponentialStrategy:
12+
print("::: ExponentialStrategy :::")
13+
elif retry_strategy == RetryStrategies.CustomStrategy:
14+
print("::: CustomStrategy :::")
15+

src/api_to_dataframe/views/dataframe_metrics.py

Whitespace-only changes.

tests/test_run.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
import pytest
22
import pandas as pd
3-
import api_to_dataframe as api_to_dataframe
3+
from api_to_dataframe import ClientBuilder
44

55

66
@pytest.fixture()
77
def setup():
8-
new_client = api_to_dataframe.ClientBuilder(endpoint="https://economia.awesomeapi.com.br/last/USD-BRL")
8+
new_client = ClientBuilder(endpoint="https://economia.awesomeapi.com.br/last/USD-BRL")
99
return new_client
1010

1111

12+
@pytest.fixture()
13+
def response_setup():
14+
new_client = ClientBuilder(endpoint="https://economia.awesomeapi.com.br/last/USD-BRL")
15+
return new_client.get_api_data()
16+
17+
1218
def test_constructor_without_param():
1319
with pytest.raises(ValueError):
14-
new_client = api_to_dataframe.ClientBuilder(endpoint="")
20+
new_client = ClientBuilder(endpoint="")
1521

1622

1723
def test_constructor_with_param(setup):
@@ -22,11 +28,10 @@ def test_constructor_with_param(setup):
2228

2329
def test_response_to_json(setup):
2430
new_client = setup
25-
response = new_client._response_to_json()
31+
response = new_client.get_api_data()
2632
assert isinstance(response, dict)
2733

2834

29-
def test_to_dataframe(setup):
30-
new_client = setup
31-
df = new_client.to_dataframe()
35+
def test_to_dataframe(response_setup):
36+
df = ClientBuilder.api_to_dataframe(response_setup)
3237
assert isinstance(df, pd.DataFrame)

0 commit comments

Comments
 (0)