Skip to content

Commit

Permalink
When login is failed, display message and error status code. For exam…
Browse files Browse the repository at this point in the history
…ple: Too many requests (429) or Unauthorized (401) (#17)
  • Loading branch information
leogregianin authored and andreroggeri committed Apr 17, 2018
1 parent 95a5893 commit c02d738
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ print(sum([t['amount'] for t in card_statements]))
# Lista de dicionários contendo todas as faturas do seu cartão de crédito
bills = nu.get_bills()
# Retorna um dicionário contendo os detalhes de uma fatura retornada por
get_bills()
# Retorna um dicionário contendo os detalhes de uma fatura retornada por get_bills()
bill_details = nu.get_bill_details(bills[1])
```

Expand All @@ -47,7 +46,7 @@ nu = Nubank('123456789', 'senha')
account_statements = nu.get_account_statements()
# Soma de todas as transações na NuConta
# Observacão: As transações de saída não possuem o valor negativo, então deve-se olhar a propiedade "__typename".
# Observacão: As transações de saída não possuem o valor negativo, então deve-se olhar a propriedade "__typename".
# TransferInEvent = Entrada
# TransferOutEvent = Saída
print(sum([t['amount'] for t in account_statements]))
Expand Down
3 changes: 2 additions & 1 deletion pynubank/nubank.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def authenticate(self, cpf, password):
}
request = requests.post(Nubank.TOKEN_URL, json=body, headers=self.headers)
if request.status_code != 200:
raise NuException('Authentication failed. Check your credentials!')
message = '{} ({})'.format(json.loads(request.content)['error'], request.status_code)
raise NuException('Authentication failed. {}'.format(message))

data = json.loads(request.content.decode('utf-8'))
self.headers['Authorization'] = 'Bearer {}'.format(data['access_token'])
Expand Down
2 changes: 2 additions & 0 deletions tests/test_nubank_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ def create_fake_response(dict_response, status_code=200):
def test_authentication_failure_raise_exception(monkeypatch, http_status):
response = Response()
response.status_code = http_status
response._content = b'{"error":"Error"}'

monkeypatch.setattr('requests.post', MagicMock(return_value=response))
with pytest.raises(NuException):
Expand Down Expand Up @@ -587,6 +588,7 @@ def test_grapql_query_raises_exeption(monkeypatch, authentication_return, http_s

response = Response()
response.status_code = http_status
response._content = b'{"error":"Error"}'

monkeypatch.setattr('requests.post', MagicMock(return_value=response))
with pytest.raises(NuException):
Expand Down

0 comments on commit c02d738

Please sign in to comment.