-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from lucasrcezimbra/develop
0.0.4
- Loading branch information
Showing
14 changed files
with
340 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
# Changelog | ||
|
||
|
||
## (unreleased) | ||
## 0.0.4 (2022-10-23) | ||
- Add High Level API | ||
- Fix Read the Docs | ||
- Fix Coveralls | ||
|
||
* First release on PyPI. | ||
|
||
## 0.0.3 (2022-10-15) | ||
- Add Inter.get_balance | ||
- Improve docs | ||
|
||
|
||
## 0.0.2 (2022-10-15) | ||
- Fix build | ||
|
||
|
||
## 0.0.1 (2022-10-15) | ||
- First PyPI version | ||
* authentication and get_statements |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
inter | ||
API | ||
===== | ||
|
||
.. toctree:: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
from inter.inter import URL, Inter # noqa | ||
from inter._client import Client # noqa | ||
from inter._inter import Inter, Operation # noqa | ||
|
||
__author__ = """Lucas Rangel Cezimbra""" | ||
__email__ = 'lucas@cezimbra.tec.br' | ||
__version__ = '0.0.3' | ||
__version__ = '0.0.4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from datetime import date, datetime | ||
from decimal import Decimal | ||
|
||
from attrs import define | ||
|
||
from inter._client import Client | ||
|
||
|
||
class Inter: | ||
def __init__(self, client): | ||
self._client = client | ||
|
||
@classmethod | ||
def from_credentials(cls, client_id, client_secret, cert_path, key_path): | ||
return cls(client=Client(client_id, client_secret, cert_path, key_path)) | ||
|
||
def get_balance(self, date=None): | ||
""" | ||
Retorna o saldo disponível da conta em determinado dia. | ||
:param date: data do saldo | ||
:type date: :class:`datetime.date` | ||
:return: saldo disponível | ||
:rtype: :class:`decimal.Decimal` | ||
""" | ||
return Decimal(str(self._client.get_balance(date)['disponivel'])) | ||
|
||
def get_statement(self, start_date, end_date): | ||
""" | ||
Busca extrato da conta referente as datas recebidas. | ||
:param start_date: data inicial do extrato | ||
:type start_date: :class:`datetime.date` | ||
:param end_date: data final do extrato | ||
:type end_date: :class:`datetime.date` | ||
:return: lista de operações do periodo | ||
:rtype: List[:class:`Operation`] | ||
""" | ||
data = self._client.get_statements(start_date, end_date)['transacoes'] | ||
return [Operation.from_data(d) for d in data] | ||
|
||
|
||
@define | ||
class Operation: | ||
PAYMENT = 'PAGAMENTO' | ||
PIX = 'PIX' | ||
|
||
CREDIT = 'C' | ||
DEBIT = 'D' | ||
|
||
date: date | ||
"Data da operação" | ||
|
||
description: str | ||
"Descrição da Operação" | ||
|
||
title: str | ||
"Título da Operação" | ||
|
||
type: str | ||
"Tipo da operação. Exemplo: :attr:`PAYMENT`, :attr:`PIX`, etc." | ||
|
||
value: Decimal | ||
"Valor da operação. Positivo se for crédito, negativo se for débito" | ||
|
||
@classmethod | ||
def from_data(cls, data): | ||
""" | ||
Transforma dados retornados da API em um objeto :class:`Operation`. | ||
:param data: dicionário com dados de uma operação | ||
:type data: `dict` | ||
:return: Operação | ||
:rtype: :class:`Operation` | ||
""" | ||
value = Decimal(str(data['valor'])) | ||
value = (value * -1) if data['tipoOperacao'] == cls.DEBIT else value | ||
|
||
return cls( | ||
date=datetime.strptime(data['dataEntrada'], '%Y-%m-%d').date(), | ||
description=data['descricao'], | ||
title=data['titulo'], | ||
type=data['tipoTransacao'], | ||
value=value, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
from uuid import uuid4 | ||
|
||
import pytest | ||
import responses | ||
from responses import matchers | ||
|
||
from inter import Client | ||
from inter._client import URL | ||
|
||
|
||
@pytest.fixture | ||
def client(faker): | ||
client_id, client_secret = faker.pystr(), faker.pystr() | ||
cert_path, key_path = faker.file_path(), faker.file_path() | ||
return Client(client_id, client_secret, cert_path, key_path) | ||
|
||
|
||
def test_init(faker): | ||
client_id, client_secret = faker.pystr(), faker.pystr() | ||
cert_path, key_path = faker.file_path(), faker.file_path() | ||
|
||
client = Client(client_id, client_secret, cert_path, key_path) | ||
|
||
assert client.client_id == client_id | ||
assert client.client_secret == client_secret | ||
assert client.cert_path == cert_path | ||
assert client.key_path == key_path | ||
|
||
|
||
@responses.activate | ||
def test_token(client): | ||
token = str(uuid4()) | ||
|
||
responses.post( | ||
URL.AUTH, | ||
json={ | ||
"access_token": token, | ||
"token_type": "Bearer", | ||
"expires_in": 3600, | ||
"scope": "extrato.read", | ||
}, | ||
status=200, | ||
match=[ | ||
matchers.urlencoded_params_matcher( | ||
{ | ||
'client_id': client.client_id, | ||
'client_secret': client.client_secret, | ||
'scope': 'extrato.read', | ||
'grant_type': 'client_credentials', | ||
} | ||
), | ||
matchers.request_kwargs_matcher({'cert': (client.cert_path, client.key_path)}) | ||
], | ||
) | ||
|
||
assert client.token == token | ||
|
||
|
||
def test_cached_token(client): | ||
token = str(uuid4()) | ||
|
||
client._token = token | ||
|
||
assert client.token == token | ||
|
||
|
||
def test_headers(client): | ||
token = str(uuid4()) | ||
|
||
client._token = token | ||
|
||
assert client.headers == {"Authorization": f"Bearer {client.token}"} | ||
|
||
|
||
@responses.activate | ||
def test_get_statements(faker, client, statements_data): | ||
client._token = uuid4() | ||
start, end = faker.past_date(), faker.past_date() | ||
|
||
responses.get( | ||
URL.STATEMENTS, | ||
json=statements_data, | ||
status=200, | ||
match=[ | ||
matchers.query_param_matcher( | ||
{ | ||
'dataInicio': start.strftime('%Y-%m-%d'), | ||
'dataFim': end.strftime('%Y-%m-%d'), | ||
} | ||
), | ||
matchers.header_matcher(client.headers), | ||
matchers.request_kwargs_matcher({'cert': (client.cert_path, client.key_path)}) | ||
], | ||
) | ||
|
||
assert client.get_statements(start, end) == statements_data | ||
|
||
|
||
@responses.activate | ||
def test_get_balance(faker, client, balance_data): | ||
client._token = uuid4() | ||
|
||
responses.get( | ||
URL.BALANCE, | ||
json=balance_data, | ||
status=200, | ||
match=[ | ||
matchers.header_matcher(client.headers), | ||
matchers.request_kwargs_matcher({'cert': (client.cert_path, client.key_path)}) | ||
], | ||
) | ||
|
||
assert client.get_balance() == balance_data | ||
|
||
|
||
@responses.activate | ||
def test_get_balance_with_date(faker, client, balance_data): | ||
client._token = uuid4() | ||
date = faker.past_date() | ||
|
||
responses.get( | ||
URL.BALANCE, | ||
json=balance_data, | ||
status=200, | ||
match=[ | ||
matchers.query_param_matcher({'dataSaldo': date.strftime('%Y-%m-%d')}), | ||
matchers.header_matcher(client.headers), | ||
matchers.request_kwargs_matcher({'cert': (client.cert_path, client.key_path)}) | ||
], | ||
) | ||
|
||
assert client.get_balance(date) == balance_data |
Oops, something went wrong.