Skip to main content

A library to check for existing obligations from KAT Bulgaria

Project description

Summary

PyPI Link Last release License PyPI Downloads Code size

This library allows you to check if you have fines from KAT Bulgaria programatically.

The code here is a simple wrapper around the API, providing you with error validation and type safety.

It does NOT save or log your data anywhere and it works with a single API endpoint.

The reason this library is needed is because the government website is highly unstable and often throws random errors and Timeouts. This library handles all known bad responses (as of the time of writing) and provides a meaningful error message and an error code for every request.


If you like my work, please consider donating

ko-fi


Installation

pip install kat_bulgaria

Example usage:

"""Sample usage script."""

import asyncio

from kat_bulgaria.kat_api_client import KatApiClient
from kat_bulgaria.errors import KatError, KatErrorType

INDIVIDUAL_EGN = "0011223344"
INDIVIDUAL_DRIVER_LICENSE = "123456789"

BUSINESS_OWNER_EGN = "0011223344"
BUSINESS_OWNER_GOVT_ID = "AA1234567"
BUSINESS_BULSTAT = "123456789"


async def sample_code():
    """Validates credentials"""

    try:
        # For individuals:
        # Validates EGN and Driver License Number locally and with the API.
        is_valid = await KatApiClient().validate_credentials_individual(INDIVIDUAL_EGN, INDIVIDUAL_DRIVER_LICENSE)
        print(f"Individual Credentials Valid: {is_valid}\n")

        # Checks if an individual has obligations, returns true or false.
        obligations = await KatApiClient().get_obligations_individual(INDIVIDUAL_EGN, INDIVIDUAL_DRIVER_LICENSE)
        print(f"Individual Obligation Count: {len(obligations)}\n")
        print(f"Raw: {obligations}\n")

        # For businesses:
        # Validates EGN, Government ID and BULSTAT locally and with the API.
        is_valid = await KatApiClient().validate_credentials_business(BUSINESS_OWNER_EGN, BUSINESS_OWNER_GOVT_ID, BUSINESS_BULSTAT)
        print(f"Business Credentials Valid: {is_valid}\n")

        # Checks if an individual has obligations, returns true or false.
        obligations = await KatApiClient().get_obligations_business(BUSINESS_OWNER_EGN, BUSINESS_OWNER_GOVT_ID, BUSINESS_BULSTAT)
        print(f"Business Obligation Count: {len(obligations)}\n")
        print(f"Raw: {obligations}\n")

    except KatError as err:
        # Code should throw only KatError.
        # Open an issue if you encounter another exception type.
        print(f"Error Type: {err.error_type}\n")
        print(f"Error Message: {err.error_message}")

        if err.error_type in (
            # Regex validation for EGN failed.
            KatErrorType.VALIDATION_EGN_INVALID,

            # Regex validation for Driving License failed.
            KatErrorType.VALIDATION_ID_DOCUMENT_INVALID,

            # KAT API returned an error because the EGN/License combination was not found.
            KatErrorType.VALIDATION_USER_NOT_FOUND_ONLINE
        ):
            print("Invalid user input")

        if err.error_type in (
            # KAT API is slow. Happens couple of times per day.
            # Retry or wait for some time to pass.
            KatErrorType.API_TIMEOUT,

            # KAT API has pooped its pants and is unable to do anything.
            # That happens sometimes, either retry or wait for a bit.
            KatErrorType.API_ERROR_READING_DATA,

            # KAT API returned a non-200 status code. Should never happen.
            # If it happens - open an issue and attach the response of the body.
            KatErrorType.API_UNKNOWN_ERROR,

            # KAT API returned response with a new schema.
            # Indicates the API has been updated and I should update this package.
            # Open an issue if you encounter this.
            KatErrorType.API_INVALID_SCHEMA,
        ):
            print("Unable to connect to KAT API")

# Run the async function
asyncio.run(sample_code())

Known raw API responses:

You can find sample API responses in /tests/fixtures.

I also document all sample responses in this issue for clarity.

If you have any fines, please add a comment to the issue above with the full API response.

You can get it by copying the url below and replacing EGN_GOES_HERE and LICENSE_GOES_HERE with your own data, then loading it in a browser.

https://e-uslugi.mvr.bg/api/Obligations/AND?obligatedPersonType=1&additinalDataForObligatedPersonType=1&mode=1&obligedPersonIdent=EGN_GOES_HERE&drivingLicenceNumber=LICENSE_GOES_HERE

Feel free to remove any personal data in the strings, but try not to modify the json structure.

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

kat_bulgaria-2.0.0.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

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

kat_bulgaria-2.0.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file kat_bulgaria-2.0.0.tar.gz.

File metadata

  • Download URL: kat_bulgaria-2.0.0.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kat_bulgaria-2.0.0.tar.gz
Algorithm Hash digest
SHA256 f34904901f5cb939beffeb252912c0d94387fa1a1e17eb6dadfcd73f4edadbd6
MD5 c616bc49b18bf5213591dc8f32bc30f0
BLAKE2b-256 c1c3c4cabde5f1932201bf1e6c2fd836af4798c457f86aa06acda16bdfbc7cb5

See more details on using hashes here.

Provenance

The following attestation bundles were made for kat_bulgaria-2.0.0.tar.gz:

Publisher: on-release-publish.yml on Nedevski/py_kat_bulgaria

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

File details

Details for the file kat_bulgaria-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: kat_bulgaria-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for kat_bulgaria-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 06847cc8c1944279426bc51ead17591425c6dc202a31670d15156f742cfeb872
MD5 e3118a69ccb85b13484feb70fc74f1a3
BLAKE2b-256 17b953e7c228652812063f0297861dd489eac0ea2c48b08f88b019dbd3d42dd5

See more details on using hashes here.

Provenance

The following attestation bundles were made for kat_bulgaria-2.0.0-py3-none-any.whl:

Publisher: on-release-publish.yml on Nedevski/py_kat_bulgaria

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