novaposhta-python-client 0.1.0__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/PKG-INFO +31 -7
  2. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/README.md +23 -5
  3. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/client.py +29 -1
  4. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/base.py +2 -0
  5. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/pyproject.toml +12 -2
  6. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/LICENSE +0 -0
  7. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/__init__.py +0 -0
  8. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/__init__.py +0 -0
  9. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/additional_service.py +0 -0
  10. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/address.py +0 -0
  11. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/common.py +0 -0
  12. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/contact_person.py +0 -0
  13. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/counterparty.py +0 -0
  14. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/internet_document.py +0 -0
  15. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/scan_sheet.py +0 -0
  16. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/models/tracking_document.py +0 -0
  17. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/mypi.ini +0 -0
  18. {novaposhta_python_client-0.1.0 → novaposhta_python_client-0.1.2}/novaposhta/types.py +0 -0
@@ -1,18 +1,24 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: novaposhta-python-client
3
- Version: 0.1.0
3
+ Version: 0.1.2
4
4
  Summary: Python client for Nova Poshta API
5
5
  Home-page: https://github.com/semolex/novaposhta-python-client
6
6
  License: MIT
7
+ Keywords: api,nova-poshta,novaposhta,nova-poshta-api,novaposhta-api
7
8
  Author: Oleksii
8
9
  Author-email: semolex@live.com
9
10
  Requires-Python: >=3.9,<4.0
11
+ Classifier: Development Status :: 4 - Beta
10
12
  Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
11
14
  Classifier: Programming Language :: Python :: 3
12
15
  Classifier: Programming Language :: Python :: 3.9
13
16
  Classifier: Programming Language :: Python :: 3.10
14
17
  Classifier: Programming Language :: Python :: 3.11
15
- Requires-Dist: httpx (>=0.25.0,<0.26.0)
18
+ Classifier: Programming Language :: Python :: 3 :: Only
19
+ Classifier: Topic :: Software Development :: Build Tools
20
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
21
+ Requires-Dist: httpx (>=0.24.0)
16
22
  Project-URL: Repository, https://github.com/semolex/novaposhta-python-client
17
23
  Description-Content-Type: text/markdown
18
24
 
@@ -29,17 +35,15 @@ with emphasis on consistency and usability.
29
35
  This client is compatible with `python = "^3.9"` and aims to mirror the Nova Poshta API's endpoints, offering a 1:1
30
36
  mapping where possible. However, users should be aware of the inconsistencies and occasional ambiguities present in the
31
37
  API. This can affect the signatures of some methods, and special attention is needed when working with endpoints like
32
- the save method which can create different items based on the provided input.
38
+ the `save` method from some models which can create different items based on the provided input.
33
39
 
34
40
  Efforts to enhance the consistency and robustness of the client are ongoing, and future releases will focus on thorough
35
41
  testing and refinement of all possible parameter combinations.
36
42
 
37
43
  ## Installation
38
44
 
39
- The package will be available on PyPI soon. For now, it can be installed directly from the GitHub repository
40
-
41
45
  ```bash
42
- pip install git+https://github.com/semolex/novaposhta-python-client
46
+ pip install novaposhta-python-client==0.1.0
43
47
  ```
44
48
 
45
49
  ## Usage
@@ -61,6 +65,25 @@ return_reason = client.additional_service.get_return_reasons()
61
65
  print(settlements, my_pack_list, return_reason)
62
66
  ```
63
67
 
68
+ ## Error handling
69
+
70
+ ```python
71
+ import httpx
72
+ from novaposhta.client import NovaPoshtaApi, InvalidAPIKeyError, APIRequestError
73
+
74
+ # Instantiate the client
75
+ client = NovaPoshtaApi('my-api-token', timeout=30, raise_for_errors=True)
76
+
77
+ try:
78
+ client.common.get_cargo_types()
79
+ except httpx.HTTPError as error:
80
+ print(f"HTTP error: {error}")
81
+ except InvalidAPIKeyError as error:
82
+ print(f"API key expired or otherwise invalid: {error}")
83
+ except APIRequestError as error:
84
+ print(f"Something else is wrong with API request: {error}")
85
+ ```
86
+
64
87
  ## Extending the Client
65
88
 
66
89
  ### Custom HTTP Client
@@ -72,7 +95,7 @@ follows the same interface:
72
95
  from novaposhta.client import NovaPoshtaApi
73
96
  import my_http_client
74
97
 
75
- client = NovaPoshtaApi('my-api-token', http_client=my_http_client.Client())
98
+ client = NovaPoshtaApi('my-api-token', http_client=my_http_client.Client)
76
99
  ```
77
100
 
78
101
  Custom client should support context manager, e.g:
@@ -126,4 +149,5 @@ poetry run pytest tests/
126
149
 
127
150
  We welcome contributions that can help in enhancing the functionality and improving the consistency of the client. For
128
151
  bugs or feature requests, please open an issue on the GitHub repository.
152
+ Please, use `black` and `mypy` as your instrument for code formatting and type checking.
129
153
 
@@ -11,17 +11,15 @@ with emphasis on consistency and usability.
11
11
  This client is compatible with `python = "^3.9"` and aims to mirror the Nova Poshta API's endpoints, offering a 1:1
12
12
  mapping where possible. However, users should be aware of the inconsistencies and occasional ambiguities present in the
13
13
  API. This can affect the signatures of some methods, and special attention is needed when working with endpoints like
14
- the save method which can create different items based on the provided input.
14
+ the `save` method from some models which can create different items based on the provided input.
15
15
 
16
16
  Efforts to enhance the consistency and robustness of the client are ongoing, and future releases will focus on thorough
17
17
  testing and refinement of all possible parameter combinations.
18
18
 
19
19
  ## Installation
20
20
 
21
- The package will be available on PyPI soon. For now, it can be installed directly from the GitHub repository
22
-
23
21
  ```bash
24
- pip install git+https://github.com/semolex/novaposhta-python-client
22
+ pip install novaposhta-python-client==0.1.0
25
23
  ```
26
24
 
27
25
  ## Usage
@@ -43,6 +41,25 @@ return_reason = client.additional_service.get_return_reasons()
43
41
  print(settlements, my_pack_list, return_reason)
44
42
  ```
45
43
 
44
+ ## Error handling
45
+
46
+ ```python
47
+ import httpx
48
+ from novaposhta.client import NovaPoshtaApi, InvalidAPIKeyError, APIRequestError
49
+
50
+ # Instantiate the client
51
+ client = NovaPoshtaApi('my-api-token', timeout=30, raise_for_errors=True)
52
+
53
+ try:
54
+ client.common.get_cargo_types()
55
+ except httpx.HTTPError as error:
56
+ print(f"HTTP error: {error}")
57
+ except InvalidAPIKeyError as error:
58
+ print(f"API key expired or otherwise invalid: {error}")
59
+ except APIRequestError as error:
60
+ print(f"Something else is wrong with API request: {error}")
61
+ ```
62
+
46
63
  ## Extending the Client
47
64
 
48
65
  ### Custom HTTP Client
@@ -54,7 +71,7 @@ follows the same interface:
54
71
  from novaposhta.client import NovaPoshtaApi
55
72
  import my_http_client
56
73
 
57
- client = NovaPoshtaApi('my-api-token', http_client=my_http_client.Client())
74
+ client = NovaPoshtaApi('my-api-token', http_client=my_http_client.Client)
58
75
  ```
59
76
 
60
77
  Custom client should support context manager, e.g:
@@ -108,3 +125,4 @@ poetry run pytest tests/
108
125
 
109
126
  We welcome contributions that can help in enhancing the functionality and improving the consistency of the client. For
110
127
  bugs or feature requests, please open an issue on the GitHub repository.
128
+ Please, use `black` and `mypy` as your instrument for code formatting and type checking.
@@ -35,6 +35,7 @@ class NovaPoshtaApi:
35
35
  api_endpoint="https://api.novaposhta.ua/v2.0/json/",
36
36
  http_client=httpx,
37
37
  timeout=10,
38
+ raise_for_errors=False,
38
39
  ):
39
40
  """
40
41
  Initialize Nova Poshta API client.
@@ -43,11 +44,13 @@ class NovaPoshtaApi:
43
44
  :param api_endpoint: API endpoint to use.
44
45
  :param http_client: HTTP client to use. Defaults to httpx.
45
46
  :param timeout: Timeout for HTTP requests.
47
+ :param raise_for_errors: Whether to check and raise errors as exceptions.
46
48
  """
47
49
  self.api_key = api_key
48
50
  self.api_endpoint = api_endpoint
49
51
  self.http_client = http_client.Client
50
52
  self.timeout = timeout
53
+ self.raise_for_errors = raise_for_errors
51
54
  self._models_pool = {}
52
55
 
53
56
  def send(
@@ -71,7 +74,7 @@ class NovaPoshtaApi:
71
74
  response = client.post(
72
75
  self.api_endpoint, json=request, headers=HEADERS, timeout=self.timeout
73
76
  )
74
- return response.json()
77
+ return self._maybe_check_errors(response.json())
75
78
 
76
79
  def new(self, model: Type[BaseModelType]) -> BaseModelType:
77
80
  """
@@ -96,6 +99,19 @@ class NovaPoshtaApi:
96
99
  """
97
100
  return self._models_pool.get(name)
98
101
 
102
+ def _maybe_check_errors(self, response):
103
+ if not self.raise_for_errors:
104
+ return response
105
+
106
+ if response["success"]:
107
+ return response
108
+
109
+ error = response["errors"][0]
110
+ if error.startswith("API key"):
111
+ raise InvalidAPIKeyError(error)
112
+ else:
113
+ raise APIRequestError(error)
114
+
99
115
  @property
100
116
  def address(self) -> Address:
101
117
  """
@@ -151,3 +167,15 @@ class NovaPoshtaApi:
151
167
  Provide access to the TrackingDocument model.
152
168
  """
153
169
  return self.new(TrackingDocument)
170
+
171
+
172
+ class NovaPoshtaError(Exception):
173
+ """General Nova Poshta exception."""
174
+
175
+
176
+ class InvalidAPIKeyError(NovaPoshtaError):
177
+ """Invalid API key exception."""
178
+
179
+
180
+ class APIRequestError(NovaPoshtaError):
181
+ """Invalid API request exception."""
@@ -1,5 +1,6 @@
1
1
  """BaseModel module."""
2
2
 
3
+ from functools import wraps
3
4
  from typing import Any
4
5
 
5
6
  from ..types import DictStrAny
@@ -12,6 +13,7 @@ def api_method(method_name: str):
12
13
  """
13
14
 
14
15
  def decorator(func):
16
+ @wraps(func)
15
17
  def wrapper(self, *args, **kwargs):
16
18
  props = func(self, *args, **kwargs)
17
19
  return self._call(method_name, props)
@@ -3,17 +3,27 @@ name = "novaposhta-python-client"
3
3
  packages = [
4
4
  { include = "novaposhta"}
5
5
  ]
6
- version = "0.1.0"
6
+ version = "0.1.2"
7
7
  description = "Python client for Nova Poshta API"
8
8
  authors = ["Oleksii <semolex@live.com>"]
9
9
  license = "MIT"
10
10
  readme = "README.md"
11
11
  homepage = "https://github.com/semolex/novaposhta-python-client"
12
12
  repository = "https://github.com/semolex/novaposhta-python-client"
13
+ keywords = [ "api", "nova-poshta", "novaposhta", "nova-poshta-api", "novaposhta-api"]
14
+ classifiers = [
15
+ "Development Status :: 4 - Beta",
16
+ "Topic :: Software Development :: Build Tools",
17
+ "Topic :: Software Development :: Libraries :: Python Modules",
18
+ "Operating System :: OS Independent",
19
+ "Programming Language :: Python :: 3 :: Only",
20
+ "Programming Language :: Python :: 3.9",
21
+ "License :: OSI Approved :: MIT License"
22
+ ]
13
23
 
14
24
  [tool.poetry.dependencies]
15
25
  python = "^3.9"
16
- httpx = "^0.25.0"
26
+ httpx = ">=0.24.0"
17
27
 
18
28
 
19
29
  [tool.poetry.group.dev.dependencies]