Skip to content

Commit

Permalink
Add calc prazo method to correios client
Browse files Browse the repository at this point in the history
  • Loading branch information
caiocarrara committed Jul 28, 2017
1 parent 21b5592 commit e554442
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 1 deletion.
15 changes: 14 additions & 1 deletion correios/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
from decimal import Decimal
from typing import Union, Sequence, List, Dict, Optional

from suds.cache import NoCache

from correios import xml_utils, DATADIR
from correios.exceptions import PostingListSerializerError, TrackingCodesLimitExceededError
from correios.models.data import EXTRA_SERVICE_MP, EXTRA_SERVICE_AR
Expand Down Expand Up @@ -332,7 +334,7 @@ def __init__(self, username, password, timeout=8, environment="production"):
self.websro_client = SoapClient(self.websro_url, timeout=self.timeout)
self.websro = self.websro_client.service

self.freight_client = SoapClient(self.freight_url, timeout=self.timeout)
self.freight_client = SoapClient(self.freight_url, timeout=self.timeout, cache=NoCache())
self.freight = self.freight_client.service

self.model_builder = ModelBuilder()
Expand Down Expand Up @@ -457,3 +459,14 @@ def calculate_freights(self,
"S" if EXTRA_SERVICE_AR in extra_services else "N",
)
return self.model_builder.build_freights_list(response)

def calculate_delivery_time(self,
service: Union[Service, int],
from_zip: Union[ZipCode, int, str],
to_zip: Union[ZipCode, int, str]):
service = Service.get(service)
from_zip = ZipCode.create(from_zip)
to_zip = ZipCode.create(to_zip)

response = self.freight.CalcPrazo(str(service), str(from_zip), str(to_zip))
return response.Servicos.cServico[0].PrazoEntrega
32 changes: 32 additions & 0 deletions tests/fixtures/cassettes/test_calculate_delivery_time.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
interactions:
- request:
body: <?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://tempuri.org/"><SOAP-ENV:Header/><ns0:Body><ns1:CalcPrazo><ns1:nCdServico>04162</ns1:nCdServico><ns1:sCepOrigem>07192100</ns1:sCepOrigem><ns1:sCepDestino>80030001</ns1:sCepDestino></ns1:CalcPrazo></ns0:Body></SOAP-ENV:Envelope>
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['468']
Content-Type: [text/xml;charset=UTF-8]
SOAPAction:
- !!binary |
Imh0dHA6Ly90ZW1wdXJpLm9yZy9DYWxjUHJhem8i
User-Agent: [python-requests/2.13.0]
method: POST
uri: http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx
response:
body: {string: '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CalcPrazoResponse
xmlns="http://tempuri.org/"><CalcPrazoResult><Servicos><cServico><Codigo>4162</Codigo><PrazoEntrega>1</PrazoEntrega><EntregaDomiciliar>S</EntregaDomiciliar><EntregaSabado>S</EntregaSabado><Erro
/><MsgErro /><obsFim /><DataMaxEntrega>31/07/2017</DataMaxEntrega></cServico></Servicos></CalcPrazoResult></CalcPrazoResponse></soap:Body></soap:Envelope>'}
headers:
Cache-Control: ['private, max-age=0']
Content-Length: ['585']
Content-Type: [text/xml; charset=utf-8]
Date: ['Fri, 28 Jul 2017 12:58:41 GMT']
Server: [Microsoft-IIS/7.5]
X-AspNet-Version: [4.0.30319]
X-Powered-By: [ASP.NET]
status: {code: 200, message: OK}
version: 1
34 changes: 34 additions & 0 deletions tests/fixtures/cassettes/test_calculate_delivery_time_except.yaml
Original file line number Diff line number Diff line change
@@ -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/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://tempuri.org/"><SOAP-ENV:Header/><ns0:Body><ns1:CalcPrazo><ns1:nCdServico>04669</ns1:nCdServico><ns1:sCepOrigem>01311300</ns1:sCepOrigem><ns1:sCepDestino>01311300</ns1:sCepDestino></ns1:CalcPrazo></ns0:Body></SOAP-ENV:Envelope>
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
Content-Length: ['468']
Content-Type: [text/xml;charset=UTF-8]
SOAPAction:
- !!binary |
Imh0dHA6Ly90ZW1wdXJpLm9yZy9DYWxjUHJhem8i
User-Agent: [python-requests/2.13.0]
method: POST
uri: http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx
response:
body: {string: "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"\
http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\
\ xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><CalcPrazoResponse\
\ xmlns=\"http://tempuri.org/\"><CalcPrazoResult><Servicos><cServico><Codigo>4669</Codigo><PrazoEntrega>0</PrazoEntrega><EntregaDomiciliar\
\ /><EntregaSabado /><Erro>008</Erro><MsgErro>Servi\xE7o indispon\xEDvel para\
\ o trecho informado.</MsgErro><obsFim /><DataMaxEntrega /></cServico></Servicos></CalcPrazoResult></CalcPrazoResponse></soap:Body></soap:Envelope>"}
headers:
Cache-Control: ['private, max-age=0']
Content-Length: ['589']
Content-Type: [text/xml; charset=utf-8]
Date: ['Fri, 28 Jul 2017 13:09:43 GMT']
Server: [Microsoft-IIS/7.5]
X-AspNet-Version: [4.0.30319]
X-Powered-By: [ASP.NET]
status: {code: 200, message: OK}
version: 1
16 changes: 16 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,19 @@ def test_calculate_freight_with_error(client, posting_card, package: Package):
assert len(freights) == 1
assert freights[0].error_code == -4
assert freights[0].error_message == "Peso excedido."


@pytest.mark.skipif(not correios, reason="API Client support disabled")
@vcr.use_cassette
def test_calculate_delivery_time(client):
expected_delivery_time = 1
delivery_time = client.calculate_delivery_time(Service.get(SERVICE_SEDEX), '07192100', '80030001')
assert expected_delivery_time == int(delivery_time)


@pytest.mark.skipif(not correios, reason="API Client support disabled")
@vcr.use_cassette
def test_calculate_delivery_time_service_not_allowed_for_path(client):
expected_delivery_time = 0
delivery_time = client.calculate_delivery_time(Service.get(SERVICE_PAC), '01311300', '01311300')
assert expected_delivery_time == int(delivery_time)

0 comments on commit e554442

Please sign in to comment.