Created
January 29, 2020 13:30
-
-
Save arkanister/8875fcb6db3c3620c4deb35dc2e28e4d to your computer and use it in GitHub Desktop.
Consulta de CEP utilizando a api do correios.
This file contains hidden or 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
| """ | |
| Cep query by brazilian correios web services. | |
| requirements: | |
| - zeep==3.4.0 | |
| """ | |
| from zeep import Client, exceptions | |
| client = Client('https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl') | |
| def get_address_by_cep(cep): | |
| """ | |
| Fetch brazilian correios api to get the complete address by cep. If any error occur, | |
| `None` will be retrieve by function execution. | |
| The response usually is returned in the following schema: | |
| { | |
| "type": "object", | |
| "properties": { | |
| "cep": {"type": "string", "pattern": "^([0-9]{8})$"}, | |
| "end": {"type": "string"}, | |
| "complemento2": {"type": ["string", "null"]}, | |
| "bairro": {"type": "string"}, | |
| "cidade": {"type": "string"}, | |
| "uf": {"type": "string", "pattern": "^([a-z]{2})$"}, | |
| "unidadesPostagem": {"type": "array"} | |
| }, | |
| "requiredProperties": ["cep", "end", "complemento2", "bairro", "cidade", "uf", "unidadesPostagem"] | |
| } | |
| Args: | |
| cep (str, required): Cep to perform the search on brazilian correios api. Both | |
| (00000000) and (000000-000) formats will be considered valid. | |
| Returns: | |
| dict | |
| """ | |
| cep = ''.join([c for c in str(cep) if c.isdigit()]) | |
| if not cep or len(cep) < 8: | |
| return None | |
| try: | |
| response = client.service.consultaCEP(cep) | |
| except exceptions.Fault: | |
| return None | |
| else: | |
| return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment