Skip to main content

Python client for Nova Poshta API

Project description

novaposhta-python-client

A Python client for interfacing with the Nova Poshta API. Designed to provide easy access to all API functionalities with emphasis on consistency and usability.

License: MIT

Description

This client is compatible with python = "^3.9" and aims to mirror the Nova Poshta API's endpoints, offering a 1:1 mapping where possible. However, users should be aware of the inconsistencies and occasional ambiguities present in the API. This can affect the signatures of some methods, and special attention is needed when working with endpoints like the save method from some models which can create different items based on the provided input.

Efforts to enhance the consistency and robustness of the client are ongoing, and future releases will focus on thorough testing and refinement of all possible parameter combinations.

Installation

pip install novaposhta-python-client

Usage

Here's a basic example of how to use the client:

from novaposhta.client import NovaPoshtaApi

# Instantiate the client
client = NovaPoshtaApi('my-api-token', timeout=30)

# Example usage of different models
settlements = client.address.search_settlements(city_name='Київ', limit=5)
my_pack_list = client.common.get_pack_list(length=1, width=5)
return_reason = client.additional_service.get_return_reasons()

# Print results
print(settlements, my_pack_list, return_reason)
client.close_sync()

Please, close the client whenever you are done with it to avoid resource leaks.

You can also use async client:

import asyncio
from novaposhta.client import NovaPoshtaApi

async def use_api_async():
    async_client = NovaPoshtaApi('your_api_key', timeout=30, async_mode=True, raise_for_errors=True)
    address = async_client.address
    settlements = await address.search_settlements(city_name='Київ', limit=5)
    print(settlements)
    await async_client.close_async()
asyncio.run(use_api_async())

You can use context manager to automatically close the client:

from novaposhta.client import NovaPoshtaApi
def use_api_sync():
    with NovaPoshtaApi(api_key='your_api_key', async_mode=False) as api:
        # Do something with the API
        pass

async def use_api_async():
    async with NovaPoshtaApi(api_key='your_api_key', async_mode=True) as api:
        # Do something with the API
        pass

Error handling

import httpx
from novaposhta.client import NovaPoshtaApi, InvalidAPIKeyError, APIRequestError

# Instantiate the client
client = NovaPoshtaApi('your_api_key', timeout=30, raise_for_errors=True)

try:
    client.common.get_cargo_types()
except httpx.HTTPError as error:
    print(f"HTTP error: {error}")
except InvalidAPIKeyError as error:
    print(f"API key expired or otherwise invalid: {error}")
except APIRequestError as error:
    print(f"Something else is wrong with API request: {error}")

Extending the Client

Custom HTTP Client

While httpx is the default HTTP library, you can easily substitute it with requests or another library, provided it follows the same interface:

from novaposhta.client import NovaPoshtaApi
import my_http_client

client = NovaPoshtaApi('your_api_key', http_client=my_http_client.Client)

Adding New Methods

If a method isn’t implemented, or you prefer a custom implementation, extend the model as shown below:

from novaposhta.models.base import BaseModel, api_method


class MyCustomModel(BaseModel):
    @api_method('MissingMethod')
    def missing_method(self, some_param: str):
        return self._call_with_props(SomeParam=some_param)

The client caches all model instances. To reset and create a new model instance, use the new method:

from novaposhta.client import NovaPoshtaApi
from novaposhta.models.address import Address

client = NovaPoshtaApi('my-api-token')
address = client.new(Address)

To get your custom model instance, use the get method:

my_custom_model = client.get(MyCustomModel.name)

Testing and linting

poetry run black novaposhta/
poetry run isort novaposhta/
poetry run mypy novaposhta/
poetry run pytest tests/

Contributing

We welcome contributions that can help in enhancing the functionality and improving the consistency of the client. For bugs or feature requests, please open an issue on the GitHub repository. Please, use black, isort and mypy as your instrument for code formatting and type checking.

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

novaposhta_python_client-0.1.10.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

novaposhta_python_client-0.1.10-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file novaposhta_python_client-0.1.10.tar.gz.

File metadata

  • Download URL: novaposhta_python_client-0.1.10.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.10 Darwin/23.6.0

File hashes

Hashes for novaposhta_python_client-0.1.10.tar.gz
Algorithm Hash digest
SHA256 c8a948d076d24e3edac7ddf550d67ab9f1c2c983c29c91c535bfa6da21163ba7
MD5 2066af28dacff866bf78f04771af57e5
BLAKE2b-256 ceca09e7834a01a625d0ba48f818b69b69e0e3e81fb474d9e56e3b6ce8b00f2a

See more details on using hashes here.

File details

Details for the file novaposhta_python_client-0.1.10-py3-none-any.whl.

File metadata

File hashes

Hashes for novaposhta_python_client-0.1.10-py3-none-any.whl
Algorithm Hash digest
SHA256 eec3229d47b75a307c9c2cf90acea7bb03bf49a335ab298c92ac1f91145f790e
MD5 ebc0e80503f6ec3b199308427bdf7366
BLAKE2b-256 9f35bfb9cff998f28c914e4eb71602eacfe1f03d0d557ff4097b33619037188b

See more details on using hashes here.

Supported by

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