Skip to main content

Cliente Python assíncrono para o serviço SOAP oficial do CNES.

Project description

3s-cnes-client

Cliente Python moderno, tipado e assíncrono para o serviço SOAP oficial do CNES (Cadastro Nacional de Estabelecimentos de Saúde).

A biblioteca encapsula SOAP/XML e entrega modelos Pydantic serializáveis para JSON. O usuário informa um CNES e recebe dados normalizados do estabelecimento.

Instalação

pip install 3s-cnes-client

Exemplo simples

import asyncio
from async_cnes import CnesClient

async def main():
    async with CnesClient() as client:
        unidade = await client.consultar_estabelecimento("2400737")

        print(unidade)

asyncio.run(main())

Exemplo JSON

json_data = unidade.model_dump()

print(json_data)

Exemplo JSON String

json_string = unidade.model_dump_json(indent=2)

print(json_string)

Tratamento de erros

import asyncio

from async_cnes import CnesClient
from async_cnes.exceptions import (
    CnesConnectionError,
    CnesNotFoundError,
    CnesSoapFaultError,
    CnesTimeoutError,
    CnesValidationError,
)

async def main():
    try:
        async with CnesClient(timeout=15.0) as client:
            unidade = await client.consultar_estabelecimento("2400737")
            print(unidade.model_dump())
    except CnesValidationError:
        print("CNES inválido")
    except CnesNotFoundError:
        print("Estabelecimento não encontrado")
    except CnesSoapFaultError as exc:
        print(exc.fault_string)
    except CnesTimeoutError:
        print("Timeout ao consultar o CNES")
    except CnesConnectionError:
        print("Falha de conexão com o CNES")

asyncio.run(main())

Configuração

from async_cnes import CnesClient, RetryConfig

client = CnesClient(
    endpoint="https://servicos.saude.gov.br/cnes/CnesService/v1r0",
    timeout=10.0,
    retry=RetryConfig(enabled=True, max_attempts=3, initial_backoff=0.2, max_backoff=2.0),
    headers={"X-Request-Id": "exemplo"},
    user_agent="minha-aplicacao/1.0",
    proxy=None,
)

Contrato SOAP implementado

WSDL oficial:

https://servicos.saude.gov.br/cnes/CnesService/v1r0?wsdl

Primeira operação implementada:

consultarEstabelecimentoSaude

O contrato usa SOAP 1.2 document/literal. O soapAction não é obrigatório. A requisição envia requestConsultarEstabelecimentoSaude com CodigoCNES/codigo, validado como string de 7 dígitos.

Desenvolvimento

python -m pip install -e ".[dev]"
ruff check .
mypy src
pytest
python -m build

Os testes unitários usam respostas SOAP mockadas e não acessam o serviço real. Testes de integração são opcionais:

ASYNC_CNES_RUN_INTEGRATION=1 pytest -m integration

Publicação

O projeto inclui workflow de GitHub Actions para testes, build e publicação no PyPI com Trusted Publishing. Veja docs/publicacao.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

3s_cnes_client-0.1.0.tar.gz (19.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

3s_cnes_client-0.1.0-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file 3s_cnes_client-0.1.0.tar.gz.

File metadata

  • Download URL: 3s_cnes_client-0.1.0.tar.gz
  • Upload date:
  • Size: 19.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for 3s_cnes_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 04b7c499e66d8a3c7715c5fa44607774f3eb53d08d98db183f59de3bbe4ede2d
MD5 5397d8cd4f0831ed791e29a3f2a8e51f
BLAKE2b-256 5aac7a29d5ea9af11b86b0117497fed39a4bc1e9cfa36c1f0babbc9cf9114fa4

See more details on using hashes here.

Provenance

The following attestation bundles were made for 3s_cnes_client-0.1.0.tar.gz:

Publisher: publish.yml on 3S-Saude/3s-cnes-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file 3s_cnes_client-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: 3s_cnes_client-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for 3s_cnes_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1624ac8f28cc6b2e4cf76742392067541b0dfeb59944da303feef6935b81d486
MD5 50540ee3d6a6b86d17215f44dd6e4a84
BLAKE2b-256 b04785df5c7272d3a070829642758d2912ee1ea286ae190ac98de4deeec040a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for 3s_cnes_client-0.1.0-py3-none-any.whl:

Publisher: publish.yml on 3S-Saude/3s-cnes-client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page