Package: novaposhta-python-client
novaposhta-python-clientA Python client for interfacing with the Nova Poshta API. Designed to provide easy access to all API functionalities
with emphasis on consistency and usability.DescriptionThis client is compatible withpython = "^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
thesavemethod 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.Installationpipinstallnovaposhta-python-clientUsageHere's a basic example of how to use the client:fromnovaposhta.clientimportNovaPoshtaApi# Instantiate the clientclient=NovaPoshtaApi('my-api-token',timeout=30)# Example usage of different modelssettlements=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 resultsprint(settlements,my_pack_list,return_reason)You can also use async client:importasynciofromnovaposhta.clientimportNovaPoshtaApiasync_client=NovaPoshtaApi('my-api-token',timeout=30,async_mode=True)a_address=async_client.addressa_settlement=asyncio.run(a_address.search_settlements(city_name='Київ',limit=5))print(a_settlement)Error handlingimporthttpxfromnovaposhta.clientimportNovaPoshtaApi,InvalidAPIKeyError,APIRequestError# Instantiate the clientclient=NovaPoshtaApi('my-api-token',timeout=30,raise_for_errors=True)try:client.common.get_cargo_types()excepthttpx.HTTPErroraserror:print(f"HTTP error:{error}")exceptInvalidAPIKeyErroraserror:print(f"API key expired or otherwise invalid:{error}")exceptAPIRequestErroraserror:print(f"Something else is wrong with API request:{error}")Extending the ClientCustom HTTP ClientWhilehttpxis the default HTTP library, you can easily substitute it with requests or another library, provided it
follows the same interface:fromnovaposhta.clientimportNovaPoshtaApiimportmy_http_clientclient=NovaPoshtaApi('my-api-token',http_client=my_http_client.Client)Custom client should support context manager, e.g:withself.http_client()asclient:response=client.post(self.api_endpoint,json=request,headers=HEADERS,timeout=self.timeout)Adding New MethodsIf a method isn’t implemented, or you prefer a custom implementation, extend the model as shown below:fromnovaposhta.models.baseimportBaseModel,api_methodclassMyCustomModel(BaseModel):@api_method('MissingMethod')defmissing_method(self,some_param:str):returnself._call_with_props(SomeParam=some_param)The client caches all model instances. To reset and create a new model instance, use the new method:fromnovaposhta.clientimportNovaPoshtaApifromnovaposhta.models.addressimportAddressclient=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 lintingpoetryrunblacknovaposhta
poetryrunmypynovaposhta/
poetryrunpytesttests/ContributingWe 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, useblackandmypyas your instrument for code formatting and type checking.