diff --git a/.idea/api-to-dataframe.iml b/.idea/api-to-dataframe.iml index 17714ac..0de63f1 100644 --- a/.idea/api-to-dataframe.iml +++ b/.idea/api-to-dataframe.iml @@ -2,9 +2,10 @@ + - + diff --git a/pyproject.toml b/pyproject.toml index 2e2ac28..06d0e55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,13 +1,13 @@ [tool.poetry] name = "api-to-dataframe" -version = "0.0.11" +version = "0.1.0" description = "A package to convert API responses to pandas dataframe" authors = ["IvanildoBarauna "] readme = "README.md" license = "MIT" packages = [{ include = "api_to_dataframe", from = "src" }] classifiers=[ - "Development Status :: 1 - Planning", + "Development Status :: 2 - Pre-Alpha", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 3", diff --git a/src/api_to_dataframe/__init__.py b/src/api_to_dataframe/__init__.py index d425652..bf0ed9e 100644 --- a/src/api_to_dataframe/__init__.py +++ b/src/api_to_dataframe/__init__.py @@ -1 +1 @@ -from . import run as ApiToDataframe \ No newline at end of file +from api_to_dataframe.main import ClientBuilder diff --git a/src/api_to_dataframe/main.py b/src/api_to_dataframe/main.py new file mode 100644 index 0000000..0bd441d --- /dev/null +++ b/src/api_to_dataframe/main.py @@ -0,0 +1,22 @@ +import requests +import pandas as pd + + +class ClientBuilder: + def __init__(self, endpoint: str): + if endpoint == "": + raise ValueError("::: endpoint param is mandatory :::") + else: + self.endpoint = endpoint + + def _response_to_json(self): + response = requests.get(self.endpoint) + + if response.ok: + return response.json() + else: + raise ConnectionError(response.status_code) + + def to_dataframe(self): + df = pd.DataFrame([self._response_to_json()]) + return df diff --git a/src/api_to_dataframe/run.py b/src/api_to_dataframe/run.py deleted file mode 100644 index d21d925..0000000 --- a/src/api_to_dataframe/run.py +++ /dev/null @@ -1,6 +0,0 @@ -class ApiToDataframe: - def __init__(self, name: str): - self.name = name - - def say_hello(self): - print(f"Hello {self.name}") diff --git a/tests/test_run.py b/tests/test_run.py index 213ad10..612e84a 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -1,5 +1,32 @@ import pytest -from src.api_to_dataframe import run +import pandas as pd +import api_to_dataframe as api_to_dataframe -def test_main(): - assert True \ No newline at end of file + +@pytest.fixture() +def setup(): + new_client = api_to_dataframe.ClientBuilder(endpoint="https://economia.awesomeapi.com.br/last/USD-BRL") + return new_client + + +def test_constructor_without_param(): + with pytest.raises(ValueError): + new_client = api_to_dataframe.ClientBuilder(endpoint="") + + +def test_constructor_with_param(setup): + expected_result = "https://economia.awesomeapi.com.br/last/USD-BRL" + new_client = setup + assert new_client.endpoint == expected_result + + +def test_response_to_json(setup): + new_client = setup + response = new_client._response_to_json() + assert isinstance(response, dict) + + +def test_to_dataframe(setup): + new_client = setup + df = new_client.to_dataframe() + assert isinstance(df, pd.DataFrame)