Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ jobs:
with:
python-version: '3.9'

- name: Install Poetry
- name: Poetry Setup
run: |
pip install --upgrade pip
curl -sSL https://install.python-poetry.org | python3 -
poetry config virtualenvs.create false
poetry install

- name: Increment version
- name: Generate and push Tag
run: |
git config --global user.name 'GitHub Actions'
git config --global user.email '[email protected]'
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "api-to-dataframe"
version = "0.1.0"
version = "1.0.0"
description = "A package to convert API responses to pandas dataframe"
authors = ["IvanildoBarauna <[email protected]>"]
readme = "README.md"
Expand Down
3 changes: 2 additions & 1 deletion src/api_to_dataframe/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from api_to_dataframe.main import ClientBuilder
from api_to_dataframe.controller.client_builder import ClientBuilder
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
9 changes: 9 additions & 0 deletions src/api_to_dataframe/common/utils/retry_strategies.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import Enum


class RetryStrategies(Enum):
NoRetryStrategy = 0
LinearStrategy = 1
ExponentialStrategy = 2
CustomStrategy = 3

20 changes: 20 additions & 0 deletions src/api_to_dataframe/controller/client_builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies
from api_to_dataframe.models.get_data import GetData


class ClientBuilder:
def __init__(self, endpoint: str, retry_strategy: RetryStrategies = RetryStrategies.NoRetryStrategy):
if endpoint == "":
raise ValueError("::: endpoint param is mandatory :::")
else:
self.endpoint = endpoint
self.retry_strategy = retry_strategy

def get_api_data(self):
response = GetData.get_response(self.endpoint, self.retry_strategy)
return response

@staticmethod
def api_to_dataframe(response: dict):
df = GetData.to_dataframe(response)
return df
22 changes: 0 additions & 22 deletions src/api_to_dataframe/main.py

This file was deleted.

24 changes: 24 additions & 0 deletions src/api_to_dataframe/models/get_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import requests
import pandas as pd

from api_to_dataframe.common.utils.retry_strategies import RetryStrategies


class GetData:
@staticmethod
def get_response(endpoint: str, RetryStrategies: RetryStrategies):
response = requests.get(endpoint)

if response.ok:
return response.json()
else:
raise ConnectionError(response.status_code)

@staticmethod
def to_dataframe(response):
df = pd.DataFrame(response)
if df.empty:
raise ValueError("::: DataFrame is empty :::")
else:
return df

15 changes: 15 additions & 0 deletions src/api_to_dataframe/models/retainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from api_to_dataframe.common.utils.retry_strategies import RetryStrategies


class Retainer:
@staticmethod
def strategy(retry_strategy: RetryStrategies = RetryStrategies.NoRetryStrategy):
if retry_strategy == RetryStrategies.NoRetryStrategy:
print("::: NoRetryStrategy :::")
elif retry_strategy == RetryStrategies.LinearStrategy:
print("::: LinearStrategy :::")
elif retry_strategy == RetryStrategies.ExponentialStrategy:
print("::: ExponentialStrategy :::")
elif retry_strategy == RetryStrategies.CustomStrategy:
print("::: CustomStrategy :::")

Empty file.
19 changes: 12 additions & 7 deletions tests/test_run.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
import pytest
import pandas as pd
import api_to_dataframe as api_to_dataframe
from api_to_dataframe import ClientBuilder


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


@pytest.fixture()
def response_setup():
new_client = ClientBuilder(endpoint="https://economia.awesomeapi.com.br/last/USD-BRL")
return new_client.get_api_data()


def test_constructor_without_param():
with pytest.raises(ValueError):
new_client = api_to_dataframe.ClientBuilder(endpoint="")
new_client = ClientBuilder(endpoint="")


def test_constructor_with_param(setup):
Expand All @@ -22,11 +28,10 @@ def test_constructor_with_param(setup):

def test_response_to_json(setup):
new_client = setup
response = new_client._response_to_json()
response = new_client.get_api_data()
assert isinstance(response, dict)


def test_to_dataframe(setup):
new_client = setup
df = new_client.to_dataframe()
def test_to_dataframe(response_setup):
df = ClientBuilder.api_to_dataframe(response_setup)
assert isinstance(df, pd.DataFrame)