novaposhta-python-client 0.1.1__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.1 → novaposhta_python_client-0.1.2}/PKG-INFO +20 -1
  2. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/README.md +19 -0
  3. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/client.py +29 -1
  4. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/pyproject.toml +1 -1
  5. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/LICENSE +0 -0
  6. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/__init__.py +0 -0
  7. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/__init__.py +0 -0
  8. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/additional_service.py +0 -0
  9. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/address.py +0 -0
  10. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/base.py +0 -0
  11. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/common.py +0 -0
  12. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/contact_person.py +0 -0
  13. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/counterparty.py +0 -0
  14. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/internet_document.py +0 -0
  15. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/scan_sheet.py +0 -0
  16. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/models/tracking_document.py +0 -0
  17. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/mypi.ini +0 -0
  18. {novaposhta_python_client-0.1.1 → novaposhta_python_client-0.1.2}/novaposhta/types.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: novaposhta-python-client
3
- Version: 0.1.1
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
@@ -65,6 +65,25 @@ return_reason = client.additional_service.get_return_reasons()
65
65
  print(settlements, my_pack_list, return_reason)
66
66
  ```
67
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
+
68
87
  ## Extending the Client
69
88
 
70
89
  ### Custom HTTP Client
@@ -41,6 +41,25 @@ return_reason = client.additional_service.get_return_reasons()
41
41
  print(settlements, my_pack_list, return_reason)
42
42
  ```
43
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
+
44
63
  ## Extending the Client
45
64
 
46
65
  ### Custom HTTP Client
@@ -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."""
@@ -3,7 +3,7 @@ name = "novaposhta-python-client"
3
3
  packages = [
4
4
  { include = "novaposhta"}
5
5
  ]
6
- version = "0.1.1"
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"