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

Handle errors in _call method #114

Merged
merged 4 commits into from Feb 21, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -20,6 +20,7 @@ htmlcov
.tox
.cache
.mypy_cache
.pytest_cache

pip-log.txt

Expand Down
11 changes: 9 additions & 2 deletions correios/client.py
Expand Up @@ -17,7 +17,9 @@
from pathlib import Path
from typing import List, Optional, Sequence, Union

from .exceptions import TrackingCodesLimitExceededError
from zeep.exceptions import Fault

from .exceptions import AuthenticationError, ClientError, TrackingCodesLimitExceededError
from .models.address import ZipAddress, ZipCode
from .models.builders import ModelBuilder
from .models.data import EXTRA_SERVICE_AR, EXTRA_SERVICE_MP
Expand Down Expand Up @@ -105,7 +107,12 @@ def _auth_call(self, method_name, *args, **kwargs):

def _call(self, method_name, *args, **kwargs):
method = getattr(self.sigep, method_name)
return method(*args, **kwargs) # TODO: handle errors
try:
return method(*args, **kwargs)
except Fault as exc:
if 'autenticacao' in str(exc):
raise AuthenticationError
raise ClientError(str(exc))

def get_user(self, contract_number: Union[int, str], posting_card_number: Union[int, str]) -> User:
contract_number = str(contract_number)
Expand Down
4 changes: 4 additions & 0 deletions correios/exceptions.py
Expand Up @@ -109,6 +109,10 @@ class TrackingCodesLimitExceededError(ClientError):
pass


class AuthenticationError(ClientError):
pass


class RendererError(BaseCorreiosError):
pass

Expand Down
34 changes: 34 additions & 0 deletions tests/fixtures/cassettes/test_client_authentication_error.yaml
@@ -0,0 +1,34 @@
interactions:
- request:
body: '<?xml version=''1.0'' encoding=''utf-8''?>

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Body><ns0:buscaCliente
xmlns:ns0="http://cliente.bean.master.sigep.bsb.correios.com.br/"><idContrato>9911222777</idContrato><idCartaoPostagem>0056789123</idCartaoPostagem><usuario>sigep</usuario><senha>XXXXXX</senha></ns0:buscaCliente></soap-env:Body></soap-env:Envelope>'
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['398']
Content-Type: [text/xml; charset=utf-8]
SOAPAction: ['""']
User-Agent: [Zeep/2.5.0 (www.python-zeep.org)]
method: POST
uri: https://apphom.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente
response:
body:
string: !!binary |
H4sIAAAAAAAAA4VQQW7DIBD8Cs0DWKlHiyClUvqBvmCNNzYSZhGLo+T3wS5t3UOUE6PZGXZnjDCm
7hyvFDiRus0hSrdyx8NUSuoAxE00o+g6WnnNeYQVADUTHKzZfvng4d7gJy6hWHNZH8cD2Y39onyl
bOCP/lZIyT6O9qRwKRSLd+iQ1UBK/EhJXTBMvLw1XxObgQr6YE2U9+60851vjlLxHFuYOv/N4oKv
OtI9YdQ1VKGstx26l147zpk8SwWz7nPN9fKip8utgZ8DYd8I7JqCf93bB60F4aOLAQAA
headers:
Connection: [close]
Content-Encoding: [gzip]
Content-Length: ['222']
Content-Type: [text/xml;charset=UTF-8]
Date: ['Wed, 21 Feb 2018 15:04:47 GMT']
Set-Cookie: [JSESSIONID=SQB6OIJ9jhTriesu6w-D1LO0.87ce37da-a934-3ec7-b3ca-869e2306cbff;
Path=/SigepMasterJPA; Secure]
Vary: ['Accept-Encoding,User-Agent']
status: {code: 500, message: Internal Server Error}
version: 1
9 changes: 8 additions & 1 deletion tests/test_client.py
Expand Up @@ -17,7 +17,7 @@

import pytest

from correios.exceptions import PostingListSerializerError, TrackingCodesLimitExceededError
from correios.exceptions import AuthenticationError, PostingListSerializerError, TrackingCodesLimitExceededError
from correios.models.address import ZipCode
from correios.models.builders import ModelBuilder
from correios.models.data import (
Expand Down Expand Up @@ -61,6 +61,13 @@ def test_basic_client():
assert client.password == "XXXXXX"


@pytest.mark.skipif(not correios, reason="API Client support disabled")
@vcr.use_cassette
def test_client_authentication_error(client):
with pytest.raises(AuthenticationError):
client.get_user(contract_number="9911222777", posting_card_number="0056789123")


@pytest.mark.skipif(not correios, reason="API Client support disabled")
@vcr.use_cassette
def test_get_user(client):
Expand Down