Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version handling #58

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 13 additions & 3 deletions fintoc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@


class Client:

"""Encapsulates the client behaviour and methods."""

def __init__(self, base_url, api_key, user_agent, params={}):
def __init__(self, base_url, api_key, api_version, user_agent, params={}):
self.base_url = base_url
self.api_key = api_key
self.user_agent = user_agent
self.params = params
self.__client = None
self.api_version = api_version

@property
def _client(self):
Expand All @@ -33,7 +33,15 @@ def _client(self):
@property
def headers(self):
"""Return the appropriate headers for every request."""
return {"Authorization": self.api_key, "User-Agent": self.user_agent}
headers = {
"Authorization": self.api_key,
"User-Agent": self.user_agent,
}

if self.api_version is not None:
headers["Fintoc-Version"] = self.api_version

return headers

def request(self, path, paginated=False, method="get", params=None, json=None):
"""
Expand All @@ -52,6 +60,7 @@ def extend(
self,
base_url=None,
api_key=None,
api_version=None,
user_agent=None,
params=None,
):
Expand All @@ -62,6 +71,7 @@ def extend(
return Client(
base_url=base_url or self.base_url,
api_key=api_key or self.api_key,
api_version=api_version or self.api_version,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encuentro que la API version debería siempre extenderse de la del parent. Así que acá sacaría el parámetro api_version del método extend y diría solamente api_version=self.api_version

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buenaa! ahi subí unas mejoras 🙂

user_agent=user_agent or self.user_agent,
params={**self.params, **params} if params else self.params,
)
4 changes: 2 additions & 2 deletions fintoc/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@


class Fintoc:

"""Encapsulates the core object's behaviour and methods."""

def __init__(self, api_key):
def __init__(self, api_key, api_version=None):
self._client = Client(
base_url=f"{API_BASE_URL}/{API_VERSION}",
api_key=api_key,
api_version=api_version,
user_agent=f"fintoc-python/{__version__}",
)
self.charges = ChargesManager("/charges", self._client)
Expand Down
2 changes: 2 additions & 0 deletions tests/managers/test_links_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down
6 changes: 6 additions & 0 deletions tests/mixins/test_manager_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down Expand Up @@ -95,11 +97,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down Expand Up @@ -148,11 +152,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down
6 changes: 6 additions & 0 deletions tests/mixins/test_resource_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down Expand Up @@ -108,11 +110,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down Expand Up @@ -159,11 +163,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class TestAccountResource:
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down
2 changes: 2 additions & 0 deletions tests/resources/test_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ class TestLinkResource:
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down
28 changes: 24 additions & 4 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ def setup_method(self):
self.api_key = "super_secret_api_key"
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.api_version = None

def create_client(self, params=False):
def create_client(self, params=False, api_version=None):
if not params:
return Client(self.base_url, self.api_key, self.user_agent)
return Client(self.base_url, self.api_key, self.user_agent, params=self.params)
return Client(self.base_url, self.api_key, api_version, self.user_agent)

return Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)

def test_client_creation_without_params(self):
client = self.create_client()
Expand All @@ -34,7 +42,17 @@ def test_client_creation_with_params(self):
assert client.user_agent == self.user_agent
assert client.params == self.params

def test_client_headers(self):
def test_client_headers_with_api_version(self):
client = self.create_client(api_version="2023-01-01")
assert isinstance(client.headers, dict)
assert len(client.headers.keys()) == 3
assert "Authorization" in client.headers
assert "User-Agent" in client.headers
assert client.headers["Authorization"] == self.api_key
assert client.headers["User-Agent"] == self.user_agent
assert client.headers["Fintoc-Version"] == "2023-01-01"

def test_client_headers_without_api_version(self):
client = self.create_client()
assert isinstance(client.headers, dict)
assert len(client.headers.keys()) == 2
Expand Down Expand Up @@ -87,11 +105,13 @@ def patch_http_client(self, patch_http_client):
def setup_method(self):
self.base_url = "https://test.com"
self.api_key = "super_secret_api_key"
self.api_version = None
self.user_agent = "fintoc-python/test"
self.params = {"first_param": "first_value", "second_param": "second_value"}
self.client = Client(
self.base_url,
self.api_key,
self.api_version,
self.user_agent,
params=self.params,
)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ class TestCoreFintocObject:
def test_object_creations(self):
# pylint: disable=protected-access
api_key = "super_secret_api_key"
fintoc = Fintoc(api_key)
api_version = "2023-01-01"
fintoc = Fintoc(api_key, api_version)
assert isinstance(fintoc._client, Client)
assert isinstance(fintoc.links, ManagerMixin)
assert isinstance(fintoc.webhook_endpoints, ManagerMixin)
Loading