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.
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)
EXPERIMENTAL: You can also use async client:
import asyncio
from novaposhta.client import NovaPoshtaApi
async_client = NovaPoshtaApi('my-api-token', timeout=30, async_mode=True)
a_address = async_client.address
a_settlement = asyncio.run(a_address.search_settlements(city_name='Київ', limit=5))
print(a_settlement)
This is an experimental feature and a subject to change.
Error handling
import httpx
from novaposhta.client import NovaPoshtaApi, InvalidAPIKeyError, APIRequestError
# Instantiate the client
client = NovaPoshtaApi('my-api-token', 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('my-api-token', http_client=my_http_client.Client)
Custom client should support context manager, e.g:
with self.http_client() as client:
response = client.post(
self.api_endpoint, json=request, headers=HEADERS, timeout=self.timeout
)
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 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
and mypy
as your instrument for code formatting and type checking.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for novaposhta_python_client-0.1.4.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b2a8bdbadf112c77a097afb1795bcae0de43af9b5cfa0b2b955f12fe6e470434 |
|
MD5 | 2988f7cf5cbdd6cb3aad27183c85ab33 |
|
BLAKE2b-256 | 8ffce563d8bbcda18b655d4963f9bc18844d1a81dca0768d07c5029320bf149c |
Hashes for novaposhta_python_client-0.1.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 515671e5b451ba34a34046fdd79e876799e3938849ab4fc23fcdacf2503c64b6 |
|
MD5 | 952f9959819f490da99714be42411214 |
|
BLAKE2b-256 | 47196b6354f698f249c871a6ea0f830d7bb210d64017005606623bee5df82065 |