waysdrop 1.0.0__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.
- waysdrop-1.0.0/.github/workflows/ci.yml +34 -0
- waysdrop-1.0.0/.gitignore +14 -0
- waysdrop-1.0.0/CHANGELOG.md +6 -0
- waysdrop-1.0.0/LICENSE +21 -0
- waysdrop-1.0.0/PKG-INFO +49 -0
- waysdrop-1.0.0/README.md +33 -0
- waysdrop-1.0.0/pyproject.toml +29 -0
- waysdrop-1.0.0/src/waysdrop/__init__.py +59 -0
- waysdrop-1.0.0/src/waysdrop/client.py +249 -0
- waysdrop-1.0.0/src/waysdrop/errors.py +36 -0
- waysdrop-1.0.0/src/waysdrop/types.py +244 -0
- waysdrop-1.0.0/src/waysdrop/webhooks.py +38 -0
- waysdrop-1.0.0/tests/fixtures/signature.json +16 -0
- waysdrop-1.0.0/tests/run_tests.py +72 -0
- waysdrop-1.0.0/tests/test_client.py +38 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main, master]
|
|
6
|
+
tags: ["v*"]
|
|
7
|
+
pull_request:
|
|
8
|
+
|
|
9
|
+
jobs:
|
|
10
|
+
test:
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/checkout@v4
|
|
14
|
+
- uses: actions/setup-python@v5
|
|
15
|
+
with:
|
|
16
|
+
python-version: "3.11"
|
|
17
|
+
- run: pip install httpx
|
|
18
|
+
- run: python tests/run_tests.py
|
|
19
|
+
|
|
20
|
+
publish:
|
|
21
|
+
if: startsWith(github.ref, 'refs/tags/v')
|
|
22
|
+
needs: test
|
|
23
|
+
runs-on: ubuntu-latest
|
|
24
|
+
steps:
|
|
25
|
+
- uses: actions/checkout@v4
|
|
26
|
+
- uses: actions/setup-python@v5
|
|
27
|
+
with:
|
|
28
|
+
python-version: "3.11"
|
|
29
|
+
- run: pip install build twine
|
|
30
|
+
- run: python -m build
|
|
31
|
+
- run: twine upload dist/*
|
|
32
|
+
env:
|
|
33
|
+
TWINE_USERNAME: __token__
|
|
34
|
+
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
|
waysdrop-1.0.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Waysdrop
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
waysdrop-1.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: waysdrop
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: Official Waysdrop Partner API SDK for Python
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
|
+
Keywords: api,delivery,sdk,waysdrop
|
|
8
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
9
|
+
Classifier: Operating System :: OS Independent
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Requires-Python: >=3.9
|
|
12
|
+
Requires-Dist: httpx>=0.27.0
|
|
13
|
+
Provides-Extra: dev
|
|
14
|
+
Requires-Dist: pytest>=8.0.0; extra == 'dev'
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# waysdrop
|
|
18
|
+
|
|
19
|
+
Official Waysdrop Partner API SDK for Python.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install waysdrop
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quickstart
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
from waysdrop import WaysdropClient
|
|
31
|
+
|
|
32
|
+
client = WaysdropClient(api_key="wsp_staging_...", display_currency="NGN")
|
|
33
|
+
account = client.get_account()
|
|
34
|
+
client.close()
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Webhooks
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from waysdrop import verify_signature, parse_webhook
|
|
41
|
+
|
|
42
|
+
if not verify_signature(request.body, request.headers.get("x-waysdrop-signature"), API_KEY):
|
|
43
|
+
return Response(status=401)
|
|
44
|
+
event = parse_webhook(request.body)
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## License
|
|
48
|
+
|
|
49
|
+
MIT
|
waysdrop-1.0.0/README.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# waysdrop
|
|
2
|
+
|
|
3
|
+
Official Waysdrop Partner API SDK for Python.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install waysdrop
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quickstart
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from waysdrop import WaysdropClient
|
|
15
|
+
|
|
16
|
+
client = WaysdropClient(api_key="wsp_staging_...", display_currency="NGN")
|
|
17
|
+
account = client.get_account()
|
|
18
|
+
client.close()
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Webhooks
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from waysdrop import verify_signature, parse_webhook
|
|
25
|
+
|
|
26
|
+
if not verify_signature(request.body, request.headers.get("x-waysdrop-signature"), API_KEY):
|
|
27
|
+
return Response(status=401)
|
|
28
|
+
event = parse_webhook(request.body)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## License
|
|
32
|
+
|
|
33
|
+
MIT
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "waysdrop"
|
|
7
|
+
version = "1.0.0"
|
|
8
|
+
description = "Official Waysdrop Partner API SDK for Python"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
requires-python = ">=3.9"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"httpx>=0.27.0",
|
|
14
|
+
]
|
|
15
|
+
keywords = ["waysdrop", "delivery", "api", "sdk"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Programming Language :: Python :: 3",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
[project.optional-dependencies]
|
|
23
|
+
dev = ["pytest>=8.0.0"]
|
|
24
|
+
|
|
25
|
+
[tool.hatch.build.targets.wheel]
|
|
26
|
+
packages = ["src/waysdrop"]
|
|
27
|
+
|
|
28
|
+
[tool.pytest.ini_options]
|
|
29
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""Waysdrop Partner API SDK for Python."""
|
|
2
|
+
|
|
3
|
+
from waysdrop.types import (
|
|
4
|
+
FleetType,
|
|
5
|
+
CityLocation,
|
|
6
|
+
StateLocation,
|
|
7
|
+
AccountSummary,
|
|
8
|
+
DeliveryDetail,
|
|
9
|
+
MerchantWallet,
|
|
10
|
+
CountryLocation,
|
|
11
|
+
DeliveryPackage,
|
|
12
|
+
DeliverySummary,
|
|
13
|
+
PricingResponse,
|
|
14
|
+
WebhookEnvelope,
|
|
15
|
+
WebhookEventName,
|
|
16
|
+
RouteDataResponse,
|
|
17
|
+
ExchangeRateResponse,
|
|
18
|
+
ListDeliveriesResponse,
|
|
19
|
+
CancelDeliveryResponse,
|
|
20
|
+
CreateDeliveryResponse,
|
|
21
|
+
ConvertCurrencyResponse,
|
|
22
|
+
PaymentCheckoutResponse,
|
|
23
|
+
)
|
|
24
|
+
from waysdrop.client import AsyncWaysdropClient, WaysdropClient
|
|
25
|
+
from waysdrop.errors import WaysdropError, infer_base_url, validate_api_key
|
|
26
|
+
from waysdrop.webhooks import is_webhook_event, parse_webhook, parse_webhook_event, verify_signature
|
|
27
|
+
|
|
28
|
+
__all__ = [
|
|
29
|
+
"WaysdropClient",
|
|
30
|
+
"AsyncWaysdropClient",
|
|
31
|
+
"WaysdropError",
|
|
32
|
+
"infer_base_url",
|
|
33
|
+
"validate_api_key",
|
|
34
|
+
"verify_signature",
|
|
35
|
+
"parse_webhook",
|
|
36
|
+
"parse_webhook_event",
|
|
37
|
+
"is_webhook_event",
|
|
38
|
+
"AccountSummary",
|
|
39
|
+
"CancelDeliveryResponse",
|
|
40
|
+
"CityLocation",
|
|
41
|
+
"ConvertCurrencyResponse",
|
|
42
|
+
"CountryLocation",
|
|
43
|
+
"CreateDeliveryResponse",
|
|
44
|
+
"DeliveryDetail",
|
|
45
|
+
"DeliveryPackage",
|
|
46
|
+
"DeliverySummary",
|
|
47
|
+
"ExchangeRateResponse",
|
|
48
|
+
"FleetType",
|
|
49
|
+
"ListDeliveriesResponse",
|
|
50
|
+
"MerchantWallet",
|
|
51
|
+
"PaymentCheckoutResponse",
|
|
52
|
+
"PricingResponse",
|
|
53
|
+
"RouteDataResponse",
|
|
54
|
+
"StateLocation",
|
|
55
|
+
"WebhookEnvelope",
|
|
56
|
+
"WebhookEventName",
|
|
57
|
+
]
|
|
58
|
+
|
|
59
|
+
__version__ = "1.0.0"
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Mapping, cast
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from waysdrop.types import (
|
|
8
|
+
FleetType,
|
|
9
|
+
CityLocation,
|
|
10
|
+
StateLocation,
|
|
11
|
+
AccountSummary,
|
|
12
|
+
DeliveryDetail,
|
|
13
|
+
MerchantWallet,
|
|
14
|
+
CountryLocation,
|
|
15
|
+
DeliveryPackage,
|
|
16
|
+
PricingResponse,
|
|
17
|
+
WebhookEnvelope,
|
|
18
|
+
RouteDataResponse,
|
|
19
|
+
ExchangeRateResponse,
|
|
20
|
+
CancelDeliveryResponse,
|
|
21
|
+
CreateDeliveryResponse,
|
|
22
|
+
ListDeliveriesResponse,
|
|
23
|
+
ConvertCurrencyResponse,
|
|
24
|
+
PaymentCheckoutResponse,
|
|
25
|
+
)
|
|
26
|
+
from waysdrop.errors import WaysdropError, infer_base_url, validate_api_key
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class WaysdropClient:
|
|
30
|
+
def __init__(
|
|
31
|
+
self,
|
|
32
|
+
api_key: str,
|
|
33
|
+
*,
|
|
34
|
+
base_url: str | None = None,
|
|
35
|
+
timeout: float = 30.0,
|
|
36
|
+
display_currency: str | None = None,
|
|
37
|
+
correlation_id: str | None = None,
|
|
38
|
+
client: httpx.Client | None = None,
|
|
39
|
+
) -> None:
|
|
40
|
+
validate_api_key(api_key)
|
|
41
|
+
self._api_key = api_key
|
|
42
|
+
self._base_url = (base_url or infer_base_url(api_key)).rstrip("/")
|
|
43
|
+
self._timeout = timeout
|
|
44
|
+
self._display_currency = display_currency
|
|
45
|
+
self._correlation_id = correlation_id
|
|
46
|
+
self._client = client or httpx.Client(timeout=timeout)
|
|
47
|
+
|
|
48
|
+
def close(self) -> None:
|
|
49
|
+
self._client.close()
|
|
50
|
+
|
|
51
|
+
def __enter__(self) -> WaysdropClient:
|
|
52
|
+
return self
|
|
53
|
+
|
|
54
|
+
def __exit__(self, *args: object) -> None:
|
|
55
|
+
self.close()
|
|
56
|
+
|
|
57
|
+
def list_countries(self, *, search: str | None = None, limit: int | None = None) -> list[CountryLocation]:
|
|
58
|
+
return cast(list[CountryLocation], self._get("/api/countries", search=search, limit=limit))
|
|
59
|
+
|
|
60
|
+
def list_states(self, *, search: str | None = None, limit: int | None = None) -> list[StateLocation]:
|
|
61
|
+
return cast(list[StateLocation], self._get("/api/states", search=search, limit=limit))
|
|
62
|
+
|
|
63
|
+
def list_cities(self, *, search: str | None = None, limit: int | None = None) -> list[CityLocation]:
|
|
64
|
+
return cast(list[CityLocation], self._get("/api/cities", search=search, limit=limit))
|
|
65
|
+
|
|
66
|
+
def get_route(self, origin: dict, destination: dict) -> RouteDataResponse:
|
|
67
|
+
return cast(RouteDataResponse, self._post("/api/route", {"origin": origin, "destination": destination}))
|
|
68
|
+
|
|
69
|
+
def list_fleet_types(self) -> list[FleetType]:
|
|
70
|
+
return cast(list[FleetType], self._get("/api/fleet-types"))
|
|
71
|
+
|
|
72
|
+
def get_pricing(self, body: dict, *, currency: str | None = None) -> PricingResponse:
|
|
73
|
+
def create_or_update_package(self, body: dict, *, currency: str | None = None) -> DeliveryPackage:
|
|
74
|
+
return cast(PricingResponse, self._post("/api/pricing", self._with_currency(body, currency), currency=currency))
|
|
75
|
+
|
|
76
|
+
def create_delivery_request(self, body: dict, *, currency: str | None = None) -> CreateDeliveryResponse:
|
|
77
|
+
|
|
78
|
+
self._request("DELETE", f"/api/package/{package_id}")
|
|
79
|
+
return cast(CreateDeliveryResponse, self._post("/api/request", self._with_currency(body, currency), currency=currency))
|
|
80
|
+
def cancel_delivery_request(self, delivery_id: str) -> CancelDeliveryResponse:
|
|
81
|
+
return cast(CancelDeliveryResponse, self._post(f"/api/request/{delivery_id}/cancel", {}))
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
return cast(DeliveryPackage, self._post("/api/package", self._with_currency(body, currency), currency=currency))
|
|
85
|
+
|
|
86
|
+
def delete_package(self, package_id: str) -> None:
|
|
87
|
+
def list_packages(self, *, currency: str | None = None) -> list[DeliveryPackage]:
|
|
88
|
+
return cast(list[DeliveryPackage], self._get("/api/packages", currency=currency))
|
|
89
|
+
|
|
90
|
+
def get_wallet(self, *, currency: str | None = None) -> MerchantWallet:
|
|
91
|
+
return cast(MerchantWallet, self._get("/api/wallet", currency=currency))
|
|
92
|
+
|
|
93
|
+
def create_payment_checkout(self, body: dict, *, currency: str | None = None) -> PaymentCheckoutResponse:
|
|
94
|
+
return cast(PaymentCheckoutResponse, self._post("/api/payments/checkout", self._with_currency(body, currency), currency=currency))
|
|
95
|
+
|
|
96
|
+
def get_account(self) -> AccountSummary:
|
|
97
|
+
return cast(AccountSummary, self._get("/api/account"))
|
|
98
|
+
|
|
99
|
+
def get_exchange_rate(self, from_currency: str, to_currency: str) -> ExchangeRateResponse:
|
|
100
|
+
return cast(ExchangeRateResponse, self._get("/api/exchange-rate", from_=from_currency, to=to_currency))
|
|
101
|
+
|
|
102
|
+
def convert_currency(self, amount: float, from_currency: str, to_currency: str) -> ConvertCurrencyResponse:
|
|
103
|
+
return cast(ConvertCurrencyResponse, self._get("/api/convert", amount=amount, from_=from_currency, to=to_currency))
|
|
104
|
+
|
|
105
|
+
def list_deliveries(
|
|
106
|
+
self,
|
|
107
|
+
*,
|
|
108
|
+
status: str | None = None,
|
|
109
|
+
search: str | None = None,
|
|
110
|
+
page: int | None = None,
|
|
111
|
+
limit: int | None = None,
|
|
112
|
+
currency: str | None = None,
|
|
113
|
+
) -> ListDeliveriesResponse:
|
|
114
|
+
return cast(
|
|
115
|
+
ListDeliveriesResponse,
|
|
116
|
+
self._get("/api/deliveries", status=status, search=search, page=page, limit=limit, currency=currency),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
def get_delivery(self, delivery_id: str, *, currency: str | None = None) -> DeliveryDetail:
|
|
120
|
+
return cast(DeliveryDetail, self._get(f"/api/deliveries/{delivery_id}", currency=currency))
|
|
121
|
+
|
|
122
|
+
def _with_currency(self, body: dict, currency: str | None) -> dict:
|
|
123
|
+
c = currency or self._display_currency
|
|
124
|
+
if c and "currency" not in body:
|
|
125
|
+
return {**body, "currency": c}
|
|
126
|
+
return body
|
|
127
|
+
|
|
128
|
+
def _get(self, path: str, **params: object) -> object:
|
|
129
|
+
return self._request("GET", path, params={k: v for k, v in params.items() if v is not None})
|
|
130
|
+
|
|
131
|
+
def _post(self, path: str, body: dict, *, currency: str | None = None) -> object:
|
|
132
|
+
params: dict[str, str] = {}
|
|
133
|
+
c = currency or self._display_currency
|
|
134
|
+
if c:
|
|
135
|
+
params["currency"] = c
|
|
136
|
+
return self._request("POST", path, json=body, params=params or None)
|
|
137
|
+
|
|
138
|
+
def _request(
|
|
139
|
+
self,
|
|
140
|
+
method: str,
|
|
141
|
+
path: str,
|
|
142
|
+
*,
|
|
143
|
+
params: Mapping[str, object] | None = None,
|
|
144
|
+
json: dict | None = None,
|
|
145
|
+
) -> object:
|
|
146
|
+
headers = {"api-key": self._api_key, "Accept": "application/json"}
|
|
147
|
+
if self._correlation_id:
|
|
148
|
+
headers["X-Correlation-Id"] = self._correlation_id
|
|
149
|
+
|
|
150
|
+
query = dict(params or {})
|
|
151
|
+
if method == "GET" and self._display_currency and "currency" not in query:
|
|
152
|
+
query.setdefault("currency", self._display_currency)
|
|
153
|
+
|
|
154
|
+
response = self._client.request(
|
|
155
|
+
method,
|
|
156
|
+
f"{self._base_url}{path}",
|
|
157
|
+
headers=headers,
|
|
158
|
+
params=query or None,
|
|
159
|
+
json=json,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
if response.status_code == 204:
|
|
163
|
+
return None
|
|
164
|
+
|
|
165
|
+
data = response.json() if response.content else {}
|
|
166
|
+
|
|
167
|
+
if not response.is_success:
|
|
168
|
+
message = data.get("message", response.reason_phrase)
|
|
169
|
+
if not isinstance(message, str):
|
|
170
|
+
message = str(message)
|
|
171
|
+
raise WaysdropError(
|
|
172
|
+
message,
|
|
173
|
+
status_code=data.get("statusCode", response.status_code),
|
|
174
|
+
details=data.get("details"),
|
|
175
|
+
quota=data.get("quota"),
|
|
176
|
+
path=data.get("path"),
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if isinstance(data, dict) and data.get("success") is True and "data" in data:
|
|
180
|
+
return data["data"]
|
|
181
|
+
return data
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
class AsyncWaysdropClient:
|
|
185
|
+
def __init__(
|
|
186
|
+
self,
|
|
187
|
+
api_key: str,
|
|
188
|
+
*,
|
|
189
|
+
base_url: str | None = None,
|
|
190
|
+
timeout: float = 30.0,
|
|
191
|
+
display_currency: str | None = None,
|
|
192
|
+
correlation_id: str | None = None,
|
|
193
|
+
client: httpx.AsyncClient | None = None,
|
|
194
|
+
) -> None:
|
|
195
|
+
validate_api_key(api_key)
|
|
196
|
+
self._api_key = api_key
|
|
197
|
+
self._base_url = (base_url or infer_base_url(api_key)).rstrip("/")
|
|
198
|
+
self._timeout = timeout
|
|
199
|
+
self._display_currency = display_currency
|
|
200
|
+
self._correlation_id = correlation_id
|
|
201
|
+
self._client = client or httpx.AsyncClient(timeout=timeout)
|
|
202
|
+
|
|
203
|
+
async def aclose(self) -> None:
|
|
204
|
+
await self._client.aclose()
|
|
205
|
+
|
|
206
|
+
async def get_account(self) -> AccountSummary:
|
|
207
|
+
return cast(AccountSummary, await self._arequest("GET", "/api/account"))
|
|
208
|
+
|
|
209
|
+
async def list_fleet_types(self) -> list[FleetType]:
|
|
210
|
+
return cast(list[FleetType], await self._arequest("GET", "/api/fleet-types"))
|
|
211
|
+
|
|
212
|
+
async def _arequest(
|
|
213
|
+
self,
|
|
214
|
+
method: str,
|
|
215
|
+
path: str,
|
|
216
|
+
*,
|
|
217
|
+
params: Mapping[str, object] | None = None,
|
|
218
|
+
json: dict | None = None,
|
|
219
|
+
) -> object:
|
|
220
|
+
headers = {"api-key": self._api_key, "Accept": "application/json"}
|
|
221
|
+
if self._correlation_id:
|
|
222
|
+
headers["X-Correlation-Id"] = self._correlation_id
|
|
223
|
+
|
|
224
|
+
response = await self._client.request(
|
|
225
|
+
method,
|
|
226
|
+
f"{self._base_url}{path}",
|
|
227
|
+
headers=headers,
|
|
228
|
+
params=params,
|
|
229
|
+
json=json,
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
if response.status_code == 204:
|
|
233
|
+
return None
|
|
234
|
+
|
|
235
|
+
data = response.json() if response.content else {}
|
|
236
|
+
if not response.is_success:
|
|
237
|
+
message = data.get("message", response.reason_phrase)
|
|
238
|
+
if not isinstance(message, str):
|
|
239
|
+
message = str(message)
|
|
240
|
+
raise WaysdropError(
|
|
241
|
+
message,
|
|
242
|
+
status_code=data.get("statusCode", response.status_code),
|
|
243
|
+
details=data.get("details"),
|
|
244
|
+
quota=data.get("quota"),
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
if isinstance(data, dict) and data.get("success") is True and "data" in data:
|
|
248
|
+
return data["data"]
|
|
249
|
+
return data
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from typing import Any, Optional
|
|
3
|
+
|
|
4
|
+
API_KEY_PATTERN = re.compile(r"^wsp_(live|staging)_[a-f0-9]{64}$")
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class WaysdropError(Exception):
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
message: str,
|
|
11
|
+
*,
|
|
12
|
+
status_code: int,
|
|
13
|
+
details: Optional[dict[str, Any]] = None,
|
|
14
|
+
quota: Optional[dict[str, Any]] = None,
|
|
15
|
+
path: Optional[str] = None,
|
|
16
|
+
) -> None:
|
|
17
|
+
super().__init__(message)
|
|
18
|
+
self.status_code = status_code
|
|
19
|
+
self.details = details
|
|
20
|
+
self.quota = quota
|
|
21
|
+
self.path = path
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def validate_api_key(api_key: str) -> None:
|
|
25
|
+
if not API_KEY_PATTERN.match(api_key):
|
|
26
|
+
raise ValueError(
|
|
27
|
+
"Invalid API key format. Expected wsp_live_… or wsp_staging_… with 64 hex chars."
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def infer_base_url(api_key: str) -> str:
|
|
32
|
+
if api_key.startswith("wsp_staging_"):
|
|
33
|
+
return "https://staging-api.waysdrop.com"
|
|
34
|
+
if api_key.startswith("wsp_live_"):
|
|
35
|
+
return "https://api.waysdrop.com"
|
|
36
|
+
return "https://staging-api.waysdrop.com"
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Literal, NotRequired, TypedDict
|
|
4
|
+
|
|
5
|
+
DeliveryStatus = Literal[
|
|
6
|
+
"REQUEST_CREATED",
|
|
7
|
+
"ASSIGNING",
|
|
8
|
+
"ACCEPTED",
|
|
9
|
+
"AWAITING_COLLECTION",
|
|
10
|
+
"PACKAGE_COLLECTED",
|
|
11
|
+
"IN_TRANSIT",
|
|
12
|
+
"DELIVERED",
|
|
13
|
+
"CANCELLED",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
RouteType = Literal["INTRA_CITY", "INTER_CITY", "INTER_STATE", "INTER_COUNTRY"]
|
|
17
|
+
PackageSize = Literal["SMALL", "MEDIUM", "LARGE", "EXTRA_LARGE"]
|
|
18
|
+
PaymentProcessor = Literal["PAYSTACK", "NOMBA", "STRIPE"]
|
|
19
|
+
DeliveryType = Literal["P2P", "ERRAND", "STORE", "PERSONAL"]
|
|
20
|
+
|
|
21
|
+
WebhookEventName = Literal[
|
|
22
|
+
"p2p.delivery.created",
|
|
23
|
+
"p2p.delivery.cancelled",
|
|
24
|
+
"errand.delivery.created",
|
|
25
|
+
"errand.delivery.cancelled",
|
|
26
|
+
"delivery.request.accepted",
|
|
27
|
+
"delivery.request.declined",
|
|
28
|
+
"delivery.awaiting.collection",
|
|
29
|
+
"delivery.collected",
|
|
30
|
+
"delivery.in.transit",
|
|
31
|
+
"delivery.delivered",
|
|
32
|
+
"delivery.reassignment.created",
|
|
33
|
+
"delivery.reassignment.direct_assigned",
|
|
34
|
+
"delivery.reassignment.requested",
|
|
35
|
+
"delivery.reassignment.collected",
|
|
36
|
+
"payment.received",
|
|
37
|
+
"order.created",
|
|
38
|
+
"order.cancelled",
|
|
39
|
+
"order.confirmed",
|
|
40
|
+
"order.declined",
|
|
41
|
+
"order.requested",
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class DisplayMoneyLocal(TypedDict, total=False):
|
|
46
|
+
currency: str
|
|
47
|
+
amount: float
|
|
48
|
+
symbol: str
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class DisplayMoney(TypedDict):
|
|
52
|
+
usd: float
|
|
53
|
+
local: NotRequired[DisplayMoneyLocal]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
class DistanceInfo(TypedDict):
|
|
57
|
+
distanceKm: float
|
|
58
|
+
etaSeconds: float
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class GeoLocation(TypedDict, total=False):
|
|
62
|
+
id: str
|
|
63
|
+
addressLine1: str
|
|
64
|
+
lgaOrCity: str
|
|
65
|
+
state: str
|
|
66
|
+
country: str
|
|
67
|
+
countryCode: str
|
|
68
|
+
lat: float
|
|
69
|
+
lon: float
|
|
70
|
+
googlePlaceId: str
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class CountryLocation(TypedDict, total=False):
|
|
74
|
+
value: str
|
|
75
|
+
name: str
|
|
76
|
+
type: Literal["INTER_COUNTRY"]
|
|
77
|
+
country: str
|
|
78
|
+
countryCode: str
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class StateLocation(TypedDict, total=False):
|
|
82
|
+
value: str
|
|
83
|
+
name: str
|
|
84
|
+
type: Literal["INTER_STATE"]
|
|
85
|
+
state: str
|
|
86
|
+
country: str
|
|
87
|
+
countryCode: str
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class CityLocation(TypedDict, total=False):
|
|
91
|
+
locationId: str
|
|
92
|
+
value: str
|
|
93
|
+
country: str
|
|
94
|
+
countryCode: str
|
|
95
|
+
lat: float
|
|
96
|
+
lon: float
|
|
97
|
+
lgaOrCity: str
|
|
98
|
+
state: str
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class FleetType(TypedDict, total=False):
|
|
102
|
+
id: str
|
|
103
|
+
name: str
|
|
104
|
+
icon: str | None
|
|
105
|
+
description: str | None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class PricingCosts(TypedDict, total=False):
|
|
109
|
+
base: float | str
|
|
110
|
+
weight: float | str
|
|
111
|
+
fleet: float | str
|
|
112
|
+
insurance: float | str
|
|
113
|
+
surcharge: float | str
|
|
114
|
+
serviceFee: float | str
|
|
115
|
+
deliverySubtotal: float | str
|
|
116
|
+
total: float | str
|
|
117
|
+
totalDisplay: DisplayMoney
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class PricingResponse(TypedDict, total=False):
|
|
121
|
+
distance: DistanceInfo
|
|
122
|
+
routeType: RouteType
|
|
123
|
+
costs: PricingCosts
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class RouteDataResponse(TypedDict):
|
|
127
|
+
distance: DistanceInfo
|
|
128
|
+
routeType: RouteType
|
|
129
|
+
origin: GeoLocation
|
|
130
|
+
destination: GeoLocation
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
class DeliveryPackage(TypedDict, total=False):
|
|
134
|
+
id: str
|
|
135
|
+
name: str
|
|
136
|
+
quantity: int
|
|
137
|
+
weight: float | str
|
|
138
|
+
value: float | str
|
|
139
|
+
valueDisplay: DisplayMoney
|
|
140
|
+
size: PackageSize
|
|
141
|
+
p2pDeliveryId: str | None
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
class DeliverySummary(TypedDict, total=False):
|
|
145
|
+
id: str
|
|
146
|
+
trackingId: str
|
|
147
|
+
status: DeliveryStatus
|
|
148
|
+
type: DeliveryType
|
|
149
|
+
routeType: RouteType
|
|
150
|
+
deliveryFee: float | str
|
|
151
|
+
deliveryFeeDisplay: DisplayMoney
|
|
152
|
+
origin: GeoLocation
|
|
153
|
+
destination: GeoLocation
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
class DeliveryDetail(DeliverySummary, total=False):
|
|
157
|
+
deliverySteps: list[dict[str, object]]
|
|
158
|
+
proofs: list[dict[str, object]]
|
|
159
|
+
fleetType: FleetType
|
|
160
|
+
p2pDelivery: dict[str, object]
|
|
161
|
+
courier: dict[str, object]
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
class CreateDeliveryResponse(TypedDict, total=False):
|
|
165
|
+
id: str
|
|
166
|
+
status: str
|
|
167
|
+
totalWeight: float | str
|
|
168
|
+
totalValue: float | str
|
|
169
|
+
deliveryId: str
|
|
170
|
+
delivery: DeliverySummary
|
|
171
|
+
processor: PaymentProcessor
|
|
172
|
+
reference: str
|
|
173
|
+
charge_currency: str
|
|
174
|
+
charge_amount: float
|
|
175
|
+
authorization_url: str
|
|
176
|
+
checkout_url: str
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
class CancelDeliveryResponse(TypedDict):
|
|
180
|
+
delivery: dict[str, str]
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class MerchantWallet(TypedDict, total=False):
|
|
184
|
+
id: str
|
|
185
|
+
currencyCode: str
|
|
186
|
+
balance: str
|
|
187
|
+
balanceDisplay: DisplayMoney
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
class PaymentCheckoutResponse(TypedDict, total=False):
|
|
191
|
+
processor: PaymentProcessor
|
|
192
|
+
reference: str
|
|
193
|
+
charge_currency: str
|
|
194
|
+
charge_amount: float
|
|
195
|
+
authorization_url: str
|
|
196
|
+
checkout_url: str
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
class StoreProfile(TypedDict):
|
|
200
|
+
id: str
|
|
201
|
+
name: str
|
|
202
|
+
tag: str
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
class AccountSummary(TypedDict, total=False):
|
|
206
|
+
userId: str
|
|
207
|
+
countryCode: str
|
|
208
|
+
displayCurrency: str
|
|
209
|
+
merchantWalletCurrencyCode: str
|
|
210
|
+
storeProfile: StoreProfile | None
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
ExchangeRateResponse = TypedDict(
|
|
214
|
+
"ExchangeRateResponse",
|
|
215
|
+
{"from": str, "to": str, "rate": float, "isStale": NotRequired[bool]},
|
|
216
|
+
)
|
|
217
|
+
|
|
218
|
+
ConvertCurrencyResponse = TypedDict(
|
|
219
|
+
"ConvertCurrencyResponse",
|
|
220
|
+
{
|
|
221
|
+
"from": str,
|
|
222
|
+
"to": str,
|
|
223
|
+
"amount": float,
|
|
224
|
+
"convertedAmount": float,
|
|
225
|
+
"rate": float,
|
|
226
|
+
},
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class PaginatedMeta(TypedDict):
|
|
231
|
+
total: int
|
|
232
|
+
page: int
|
|
233
|
+
limit: int
|
|
234
|
+
totalPages: int
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
class ListDeliveriesResponse(TypedDict):
|
|
238
|
+
data: list[DeliveryDetail]
|
|
239
|
+
meta: PaginatedMeta
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
class WebhookEnvelope(TypedDict):
|
|
243
|
+
event: WebhookEventName
|
|
244
|
+
data: dict[str, object]
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from typing import Union
|
|
3
|
+
|
|
4
|
+
from waysdrop.types import WebhookEnvelope, WebhookEventName
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def verify_signature(
|
|
8
|
+
raw_body: Union[bytes, str],
|
|
9
|
+
signature_header: str | None,
|
|
10
|
+
api_key: str,
|
|
11
|
+
) -> bool:
|
|
12
|
+
if not signature_header:
|
|
13
|
+
return False
|
|
14
|
+
body = raw_body if isinstance(raw_body, bytes) else raw_body.encode("utf-8")
|
|
15
|
+
import hmac
|
|
16
|
+
import hashlib
|
|
17
|
+
|
|
18
|
+
expected = hmac.new(api_key.encode("utf-8"), body, hashlib.sha256).hexdigest()
|
|
19
|
+
return hmac.compare_digest(expected, signature_header)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def parse_webhook(raw_body: Union[bytes, str]) -> WebhookEnvelope:
|
|
23
|
+
text = raw_body if isinstance(raw_body, str) else raw_body.decode("utf-8")
|
|
24
|
+
payload = json.loads(text)
|
|
25
|
+
if "event" not in payload or "data" not in payload:
|
|
26
|
+
raise ValueError("Invalid webhook payload: expected { event, data }")
|
|
27
|
+
return {
|
|
28
|
+
"event": payload["event"],
|
|
29
|
+
"data": payload["data"],
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def parse_webhook_event(raw_body: Union[bytes, str]) -> WebhookEnvelope:
|
|
34
|
+
return parse_webhook(raw_body)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def is_webhook_event(envelope: WebhookEnvelope, event: WebhookEventName) -> bool:
|
|
38
|
+
return envelope["event"] == event
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"apiKey": "wsp_staging_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
|
3
|
+
"payload": {
|
|
4
|
+
"event": "p2p.delivery.created",
|
|
5
|
+
"data": {
|
|
6
|
+
"status": "REQUEST_CREATED",
|
|
7
|
+
"trackingId": "P2P-TEST-001",
|
|
8
|
+
"deliveryId": "550e8400-e29b-41d4-a716-446655440000",
|
|
9
|
+
"p2pDeliveryId": "660e8400-e29b-41d4-a716-446655440001"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"rawBody": "{\"event\":\"p2p.delivery.created\",\"data\":{\"status\":\"REQUEST_CREATED\",\"trackingId\":\"P2P-TEST-001\",\"deliveryId\":\"550e8400-e29b-41d4-a716-446655440000\",\"p2pDeliveryId\":\"660e8400-e29b-41d4-a716-446655440001\"}}",
|
|
13
|
+
"signature": "7f180019583a154433a360d5f4ee6e3470e78c01e94c6879783641c024c28af3",
|
|
14
|
+
"algorithm": "HMAC-SHA256",
|
|
15
|
+
"signInput": "JSON.stringify(payload) using the exact raw body bytes"
|
|
16
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
import sys
|
|
3
|
+
import json
|
|
4
|
+
import importlib.util
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
ROOT = Path(__file__).resolve().parents[1]
|
|
8
|
+
SRC = ROOT / "src" / "waysdrop"
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def load_module(name: str, filename: str):
|
|
12
|
+
spec = importlib.util.spec_from_file_location(name, SRC / filename)
|
|
13
|
+
module = importlib.util.module_from_spec(spec)
|
|
14
|
+
assert spec.loader is not None
|
|
15
|
+
spec.loader.exec_module(module)
|
|
16
|
+
return module
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def bootstrap_package() -> None:
|
|
20
|
+
pkg = importlib.util.module_from_spec(
|
|
21
|
+
importlib.util.spec_from_loader("waysdrop", loader=None)
|
|
22
|
+
)
|
|
23
|
+
sys.modules["waysdrop"] = pkg
|
|
24
|
+
types_mod = load_module("waysdrop.types", "types.py")
|
|
25
|
+
sys.modules["waysdrop.types"] = types_mod
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
bootstrap_package()
|
|
29
|
+
errors = load_module("waysdrop_errors", "errors.py")
|
|
30
|
+
webhooks = load_module("waysdrop_webhooks", "webhooks.py")
|
|
31
|
+
|
|
32
|
+
FIXTURE = json.loads(
|
|
33
|
+
(Path(__file__).resolve().parent / "fixtures" / "signature.json").read_text()
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_validate_api_key():
|
|
38
|
+
try:
|
|
39
|
+
errors.validate_api_key("bad")
|
|
40
|
+
raise AssertionError("expected ValueError")
|
|
41
|
+
except ValueError:
|
|
42
|
+
pass
|
|
43
|
+
errors.validate_api_key(FIXTURE["apiKey"])
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_infer_base_url():
|
|
47
|
+
assert errors.infer_base_url(FIXTURE["apiKey"]) == "https://staging-api.waysdrop.com"
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def test_verify_signature():
|
|
51
|
+
assert webhooks.verify_signature(FIXTURE["rawBody"], FIXTURE["signature"], FIXTURE["apiKey"])
|
|
52
|
+
assert not webhooks.verify_signature(FIXTURE["rawBody"], "bad", FIXTURE["apiKey"])
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def test_parse_webhook():
|
|
56
|
+
parsed = webhooks.parse_webhook(FIXTURE["rawBody"])
|
|
57
|
+
assert parsed["event"] == "p2p.delivery.created"
|
|
58
|
+
assert parsed["data"]["trackingId"] == "P2P-TEST-001"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_waysdrop_error():
|
|
62
|
+
err = errors.WaysdropError("quota", status_code=429, quota={"limit": 1000})
|
|
63
|
+
assert err.status_code == 429
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if __name__ == "__main__":
|
|
67
|
+
test_validate_api_key()
|
|
68
|
+
test_infer_base_url()
|
|
69
|
+
test_verify_signature()
|
|
70
|
+
test_parse_webhook()
|
|
71
|
+
test_waysdrop_error()
|
|
72
|
+
print("all tests passed")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from waysdrop.errors import WaysdropError, infer_base_url, validate_api_key
|
|
7
|
+
from waysdrop.webhooks import parse_webhook, verify_signature
|
|
8
|
+
|
|
9
|
+
FIXTURES = Path(__file__).resolve().parent / "fixtures"
|
|
10
|
+
SIGNATURE = json.loads((FIXTURES / "signature.json").read_text())
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_validate_api_key():
|
|
14
|
+
with pytest.raises(ValueError, match="Invalid API key"):
|
|
15
|
+
validate_api_key("bad")
|
|
16
|
+
validate_api_key(SIGNATURE["apiKey"])
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def test_infer_base_url():
|
|
20
|
+
assert infer_base_url(SIGNATURE["apiKey"]) == "https://staging-api.waysdrop.com"
|
|
21
|
+
assert infer_base_url("wsp_live_" + "a" * 64) == "https://api.waysdrop.com"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_verify_signature():
|
|
25
|
+
assert verify_signature(SIGNATURE["rawBody"], SIGNATURE["signature"], SIGNATURE["apiKey"])
|
|
26
|
+
assert not verify_signature(SIGNATURE["rawBody"], "bad", SIGNATURE["apiKey"])
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_parse_webhook():
|
|
30
|
+
parsed = parse_webhook(SIGNATURE["rawBody"])
|
|
31
|
+
assert parsed["event"] == "p2p.delivery.created"
|
|
32
|
+
assert parsed["data"]["trackingId"] == "P2P-TEST-001"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def test_waysdrop_error():
|
|
36
|
+
err = WaysdropError("quota", status_code=429, quota={"limit": 1000})
|
|
37
|
+
assert err.status_code == 429
|
|
38
|
+
assert err.quota == {"limit": 1000}
|