vardic-uk-vehicle-data 1.0.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.
- vardic_uk_vehicle_data-1.0.2/CHANGELOG.md +39 -0
- vardic_uk_vehicle_data-1.0.2/CONTRIBUTING.md +26 -0
- vardic_uk_vehicle_data-1.0.2/LICENSE +21 -0
- vardic_uk_vehicle_data-1.0.2/MANIFEST.in +4 -0
- vardic_uk_vehicle_data-1.0.2/PKG-INFO +106 -0
- vardic_uk_vehicle_data-1.0.2/README.md +77 -0
- vardic_uk_vehicle_data-1.0.2/SECURITY.md +15 -0
- vardic_uk_vehicle_data-1.0.2/examples/lookup.py +25 -0
- vardic_uk_vehicle_data-1.0.2/examples/mileage_check.py +24 -0
- vardic_uk_vehicle_data-1.0.2/examples/webhook_server.py +38 -0
- vardic_uk_vehicle_data-1.0.2/pyproject.toml +40 -0
- vardic_uk_vehicle_data-1.0.2/setup.cfg +4 -0
- vardic_uk_vehicle_data-1.0.2/tests/__init__.py +0 -0
- vardic_uk_vehicle_data-1.0.2/tests/helpers.py +54 -0
- vardic_uk_vehicle_data-1.0.2/tests/test_client.py +117 -0
- vardic_uk_vehicle_data-1.0.2/tests/test_live.py +29 -0
- vardic_uk_vehicle_data-1.0.2/tests/test_webhook.py +42 -0
- vardic_uk_vehicle_data-1.0.2/uk_vehicle_data/__init__.py +95 -0
- vardic_uk_vehicle_data-1.0.2/uk_vehicle_data/py.typed +0 -0
- vardic_uk_vehicle_data-1.0.2/vardic_uk_vehicle_data.egg-info/PKG-INFO +106 -0
- vardic_uk_vehicle_data-1.0.2/vardic_uk_vehicle_data.egg-info/SOURCES.txt +21 -0
- vardic_uk_vehicle_data-1.0.2/vardic_uk_vehicle_data.egg-info/dependency_links.txt +1 -0
- vardic_uk_vehicle_data-1.0.2/vardic_uk_vehicle_data.egg-info/top_level.txt +1 -0
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this package are recorded here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [Semantic Versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
<!-- Version badges for PyPI are left out of the README on purpose: add one once the first release is published, because it shows "not found" until then. -->
|
|
6
|
+
|
|
7
|
+
## [Unreleased]
|
|
8
|
+
|
|
9
|
+
## [1.0.2] - 2026-11-29
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
- Published on PyPI as `vardic-uk-vehicle-data`.
|
|
13
|
+
|
|
14
|
+
## [1.0.1] - 2026-11-29
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- Unit tests with mocked HTTP for every public method, error mapping and webhook verification, plus an optional live smoke test against the sandbox (set `VARDIC_TEST_KEY`).
|
|
19
|
+
- GitHub Actions: CI on Python 3.9 to 3.13, and a publish workflow for releases.
|
|
20
|
+
- Runnable examples in `examples/`.
|
|
21
|
+
- SECURITY.md, CONTRIBUTING.md, issue templates and Dependabot for actions.
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
|
|
25
|
+
- Docstring example now shows the mileage check.
|
|
26
|
+
- README: clearer examples and setup.
|
|
27
|
+
- Package metadata: classifiers for Python 3.9 to 3.13, project URLs, keywords, typed package marker and a fuller source distribution.
|
|
28
|
+
|
|
29
|
+
### Fixed
|
|
30
|
+
|
|
31
|
+
- HTTP error responses are now closed after the error body is read.
|
|
32
|
+
|
|
33
|
+
## [1.0.0] - 2026-11-29
|
|
34
|
+
|
|
35
|
+
- First public release.
|
|
36
|
+
|
|
37
|
+
[Unreleased]: https://github.com/vardic-dev/uk-vehicle-data-python/compare/v1.0.1...HEAD
|
|
38
|
+
[1.0.1]: https://github.com/vardic-dev/uk-vehicle-data-python/compare/v1.0.0...v1.0.1
|
|
39
|
+
[1.0.0]: https://github.com/vardic-dev/uk-vehicle-data-python/releases/tag/v1.0.0
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
Thanks for helping. Bug reports and small, focused pull requests are welcome.
|
|
4
|
+
|
|
5
|
+
## Set up
|
|
6
|
+
|
|
7
|
+
You need Python 3.9 or newer. There are no dependencies to install.
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
python -m unittest discover -s tests -t . -v
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Tests use the standard library `unittest` with mocked HTTP, so they need no network or API key. To also run the live smoke test against the sandbox, set `VARDIC_TEST_KEY` to a `vd_test_` key:
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
VARDIC_TEST_KEY=vd_test_... python -m unittest discover -s tests -t . -v
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Pull requests
|
|
20
|
+
|
|
21
|
+
- Keep the package free of runtime dependencies (standard library only).
|
|
22
|
+
- Add or update a test for every change in behaviour.
|
|
23
|
+
- Update the README when you change the public API.
|
|
24
|
+
- Add a line under "Unreleased" in `CHANGELOG.md`.
|
|
25
|
+
|
|
26
|
+
Questions about the API itself: hello@vardic.dev. Security problems: see `SECURITY.md`.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vardic
|
|
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.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vardic-uk-vehicle-data
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: UK vehicle data API client: DVLA vehicle enquiry and DVSA MOT history in one call, with ULEZ estimate, mileage checks and expiry webhooks.
|
|
5
|
+
Author-email: Vardic <hello@vardic.dev>
|
|
6
|
+
Maintainer-email: Vardic <hello@vardic.dev>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://vardic.dev/vehicle
|
|
9
|
+
Project-URL: Documentation, https://vardic.dev/docs/vehicle
|
|
10
|
+
Project-URL: Source, https://github.com/vardic-dev/uk-vehicle-data-python
|
|
11
|
+
Project-URL: Issues, https://github.com/vardic-dev/uk-vehicle-data-python/issues
|
|
12
|
+
Project-URL: Changelog, https://github.com/vardic-dev/uk-vehicle-data-python/blob/main/CHANGELOG.md
|
|
13
|
+
Keywords: dvla,dvsa,mot,mot-history,vehicle-data,ulez,uk
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# UK vehicle data API for Python
|
|
31
|
+
|
|
32
|
+
[](https://pypi.org/project/vardic-uk-vehicle-data/) [](https://github.com/vardic-dev/uk-vehicle-data-python/actions/workflows/ci.yml) [](LICENSE)
|
|
33
|
+
|
|
34
|
+
Look up any UK registration and get the DVLA record and full DVSA MOT history in one call, with mileage checks, an estimated ULEZ status and MOT and tax countdowns.
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from uk_vehicle_data import VehicleData
|
|
38
|
+
|
|
39
|
+
api = VehicleData() # reads VARDIC_API_KEY
|
|
40
|
+
v = api.lookup("AB12 CDE")["data"]
|
|
41
|
+
|
|
42
|
+
print(v["make"], v["model"], v["colour"], v["fuelType"])
|
|
43
|
+
print("MOT expires", v["mot"]["expiryDate"], f'({v["mot"]["daysUntilExpiry"]} days)')
|
|
44
|
+
print("Tax", v["tax"]["status"], v["tax"]["dueDate"])
|
|
45
|
+
print("ULEZ (estimate)", (v["cleanAirZone"] or {}).get("result"))
|
|
46
|
+
for a in v["mileage"]["anomalies"]:
|
|
47
|
+
print(a["type"], a["detail"])
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install vardic-uk-vehicle-data
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Python 3.9+, standard library only.
|
|
57
|
+
|
|
58
|
+
## What you get
|
|
59
|
+
|
|
60
|
+
- **Vehicle record** from the DVLA: make, colour, fuel, engine size, CO2, Euro status, first registration, tax status and due date.
|
|
61
|
+
- **MOT history** from the DVSA: every test with result, mileage, expiry and defects.
|
|
62
|
+
- **Mileage checks**: readings over time, average miles a year and anomalies such as a reading that went down.
|
|
63
|
+
- **Clean air zone estimate**: `likely_compliant`, `likely_non_compliant`, `uncertain` or `unknown`, with the basis for it.
|
|
64
|
+
- **Bulk lookups** (`bulk`): up to 25 registrations in one call.
|
|
65
|
+
- **Expiry webhooks** (`watch`): signed events before MOT or tax runs out, and when a new MOT test is recorded.
|
|
66
|
+
|
|
67
|
+
## Sandbox key
|
|
68
|
+
|
|
69
|
+
Every account gets a `vd_test_` key as well as a live key. It returns made-up data for any registration and is never counted. `ZZ99ZZZ` returns `vehicle_not_found` and `TL12OLD` returns an old diesel whose mileage went down.
|
|
70
|
+
|
|
71
|
+
## Webhooks
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
api.watch("AB12CDE", "https://example.com/hooks/vardic", days_before=[30, 7, 1])
|
|
75
|
+
|
|
76
|
+
from uk_vehicle_data import verify_webhook
|
|
77
|
+
ok = verify_webhook(request.body, request.headers["vardic-signature"], WEBHOOK_SECRET)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Watches need a live key. Pass the raw request body as bytes. Event types and payloads: https://vardic.dev/docs/webhooks
|
|
81
|
+
|
|
82
|
+
## Errors
|
|
83
|
+
|
|
84
|
+
Failed calls raise `VardicApiError` with `.status` and `.code`, for example `vehicle_not_found` (404), `quota_exceeded` (429) or `upstream_unavailable` (503). Calls that fail with 503 are not counted.
|
|
85
|
+
|
|
86
|
+
## Pricing
|
|
87
|
+
|
|
88
|
+
Free plan: 100 lookups a month, no card needed. Paid plans start at £59 a month for 2,000 lookups. See https://vardic.dev/pricing
|
|
89
|
+
|
|
90
|
+
## Links
|
|
91
|
+
|
|
92
|
+
- Get a free key: https://vardic.dev/signup
|
|
93
|
+
- API docs: https://vardic.dev/docs/vehicle
|
|
94
|
+
- JavaScript client: https://github.com/vardic-dev/uk-vehicle-data-js
|
|
95
|
+
- Examples: [examples/](examples/)
|
|
96
|
+
- Changelog: [CHANGELOG.md](CHANGELOG.md)
|
|
97
|
+
- Contributing: [CONTRIBUTING.md](CONTRIBUTING.md)
|
|
98
|
+
- Security: [SECURITY.md](SECURITY.md)
|
|
99
|
+
|
|
100
|
+
## Data
|
|
101
|
+
|
|
102
|
+
Contains public sector information licensed under the Open Government Licence v3.0. Not affiliated with or endorsed by the DVLA or DVSA. Clean air zone results are estimates: check the official TfL or GOV.UK vehicle checker before relying on one.
|
|
103
|
+
|
|
104
|
+
## Licence
|
|
105
|
+
|
|
106
|
+
MIT. Made by [Vardic](https://vardic.dev).
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# UK vehicle data API for Python
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/vardic-uk-vehicle-data/) [](https://github.com/vardic-dev/uk-vehicle-data-python/actions/workflows/ci.yml) [](LICENSE)
|
|
4
|
+
|
|
5
|
+
Look up any UK registration and get the DVLA record and full DVSA MOT history in one call, with mileage checks, an estimated ULEZ status and MOT and tax countdowns.
|
|
6
|
+
|
|
7
|
+
```python
|
|
8
|
+
from uk_vehicle_data import VehicleData
|
|
9
|
+
|
|
10
|
+
api = VehicleData() # reads VARDIC_API_KEY
|
|
11
|
+
v = api.lookup("AB12 CDE")["data"]
|
|
12
|
+
|
|
13
|
+
print(v["make"], v["model"], v["colour"], v["fuelType"])
|
|
14
|
+
print("MOT expires", v["mot"]["expiryDate"], f'({v["mot"]["daysUntilExpiry"]} days)')
|
|
15
|
+
print("Tax", v["tax"]["status"], v["tax"]["dueDate"])
|
|
16
|
+
print("ULEZ (estimate)", (v["cleanAirZone"] or {}).get("result"))
|
|
17
|
+
for a in v["mileage"]["anomalies"]:
|
|
18
|
+
print(a["type"], a["detail"])
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
pip install vardic-uk-vehicle-data
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Python 3.9+, standard library only.
|
|
28
|
+
|
|
29
|
+
## What you get
|
|
30
|
+
|
|
31
|
+
- **Vehicle record** from the DVLA: make, colour, fuel, engine size, CO2, Euro status, first registration, tax status and due date.
|
|
32
|
+
- **MOT history** from the DVSA: every test with result, mileage, expiry and defects.
|
|
33
|
+
- **Mileage checks**: readings over time, average miles a year and anomalies such as a reading that went down.
|
|
34
|
+
- **Clean air zone estimate**: `likely_compliant`, `likely_non_compliant`, `uncertain` or `unknown`, with the basis for it.
|
|
35
|
+
- **Bulk lookups** (`bulk`): up to 25 registrations in one call.
|
|
36
|
+
- **Expiry webhooks** (`watch`): signed events before MOT or tax runs out, and when a new MOT test is recorded.
|
|
37
|
+
|
|
38
|
+
## Sandbox key
|
|
39
|
+
|
|
40
|
+
Every account gets a `vd_test_` key as well as a live key. It returns made-up data for any registration and is never counted. `ZZ99ZZZ` returns `vehicle_not_found` and `TL12OLD` returns an old diesel whose mileage went down.
|
|
41
|
+
|
|
42
|
+
## Webhooks
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
api.watch("AB12CDE", "https://example.com/hooks/vardic", days_before=[30, 7, 1])
|
|
46
|
+
|
|
47
|
+
from uk_vehicle_data import verify_webhook
|
|
48
|
+
ok = verify_webhook(request.body, request.headers["vardic-signature"], WEBHOOK_SECRET)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Watches need a live key. Pass the raw request body as bytes. Event types and payloads: https://vardic.dev/docs/webhooks
|
|
52
|
+
|
|
53
|
+
## Errors
|
|
54
|
+
|
|
55
|
+
Failed calls raise `VardicApiError` with `.status` and `.code`, for example `vehicle_not_found` (404), `quota_exceeded` (429) or `upstream_unavailable` (503). Calls that fail with 503 are not counted.
|
|
56
|
+
|
|
57
|
+
## Pricing
|
|
58
|
+
|
|
59
|
+
Free plan: 100 lookups a month, no card needed. Paid plans start at £59 a month for 2,000 lookups. See https://vardic.dev/pricing
|
|
60
|
+
|
|
61
|
+
## Links
|
|
62
|
+
|
|
63
|
+
- Get a free key: https://vardic.dev/signup
|
|
64
|
+
- API docs: https://vardic.dev/docs/vehicle
|
|
65
|
+
- JavaScript client: https://github.com/vardic-dev/uk-vehicle-data-js
|
|
66
|
+
- Examples: [examples/](examples/)
|
|
67
|
+
- Changelog: [CHANGELOG.md](CHANGELOG.md)
|
|
68
|
+
- Contributing: [CONTRIBUTING.md](CONTRIBUTING.md)
|
|
69
|
+
- Security: [SECURITY.md](SECURITY.md)
|
|
70
|
+
|
|
71
|
+
## Data
|
|
72
|
+
|
|
73
|
+
Contains public sector information licensed under the Open Government Licence v3.0. Not affiliated with or endorsed by the DVLA or DVSA. Clean air zone results are estimates: check the official TfL or GOV.UK vehicle checker before relying on one.
|
|
74
|
+
|
|
75
|
+
## Licence
|
|
76
|
+
|
|
77
|
+
MIT. Made by [Vardic](https://vardic.dev).
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# Security policy
|
|
2
|
+
|
|
3
|
+
## Reporting a vulnerability
|
|
4
|
+
|
|
5
|
+
Email hello@vardic.dev with the subject "Security: uk-vehicle-data-python". Include what you found, how to reproduce it and the version you tested. Please do not open a public issue for a security problem.
|
|
6
|
+
|
|
7
|
+
We aim to reply within 3 working days and to ship a fix or a clear plan within 14 days.
|
|
8
|
+
|
|
9
|
+
## Supported versions
|
|
10
|
+
|
|
11
|
+
Only the latest release of this package receives security fixes.
|
|
12
|
+
|
|
13
|
+
## Your API key
|
|
14
|
+
|
|
15
|
+
Never commit an API key or a webhook secret. If one leaks, revoke it in your Vardic dashboard at https://vardic.dev and create a new one.
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Look up one registration and print the main facts.
|
|
2
|
+
|
|
3
|
+
Run: VARDIC_API_KEY=vd_test_... python examples/lookup.py "AB12 CDE"
|
|
4
|
+
"""
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
9
|
+
|
|
10
|
+
from uk_vehicle_data import VardicApiError, VehicleData # noqa: E402
|
|
11
|
+
|
|
12
|
+
reg = sys.argv[1] if len(sys.argv) > 1 else "AB12CDE"
|
|
13
|
+
api = VehicleData() # reads VARDIC_API_KEY
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
v = api.lookup(reg)["data"]
|
|
17
|
+
except VardicApiError as e:
|
|
18
|
+
if e.code == "vehicle_not_found":
|
|
19
|
+
sys.exit(f"No vehicle found for {reg}")
|
|
20
|
+
raise
|
|
21
|
+
|
|
22
|
+
print(f"{v['make']} {v['model']}, {v['colour']}, {v['fuelType']}")
|
|
23
|
+
print(f"MOT expires {v['mot']['expiryDate']} ({v['mot']['daysUntilExpiry']} days)")
|
|
24
|
+
print(f"Tax {v['tax']['status']}, due {v['tax']['dueDate']}")
|
|
25
|
+
print("ULEZ estimate:", (v.get("cleanAirZone") or {}).get("result", "unknown"))
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""Check several registrations at once and flag any with a mileage anomaly.
|
|
2
|
+
|
|
3
|
+
Run: VARDIC_API_KEY=vd_test_... python examples/mileage_check.py AB12CDE TL12OLD
|
|
4
|
+
"""
|
|
5
|
+
import os
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
9
|
+
|
|
10
|
+
from uk_vehicle_data import VehicleData # noqa: E402
|
|
11
|
+
|
|
12
|
+
regs = sys.argv[1:] or ["AB12CDE", "TL12OLD"]
|
|
13
|
+
out = VehicleData().bulk(regs)
|
|
14
|
+
|
|
15
|
+
for item in out["data"]:
|
|
16
|
+
reg = item["registration"]
|
|
17
|
+
if item.get("error"):
|
|
18
|
+
print(f"{reg}: {item['error']['code']}")
|
|
19
|
+
continue
|
|
20
|
+
mileage = item["data"].get("mileage") or {}
|
|
21
|
+
if mileage.get("anomalyFound"):
|
|
22
|
+
print(f"{reg}: mileage anomaly", mileage.get("anomalies"))
|
|
23
|
+
else:
|
|
24
|
+
print(f"{reg}: mileage looks consistent")
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Receive MOT and tax expiry webhooks and check their signature.
|
|
2
|
+
|
|
3
|
+
Run: VARDIC_WEBHOOK_SECRET=whsec_... python examples/webhook_server.py
|
|
4
|
+
Then register the public URL of this server with api.watch(registration, url).
|
|
5
|
+
"""
|
|
6
|
+
import json
|
|
7
|
+
import os
|
|
8
|
+
import sys
|
|
9
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
10
|
+
|
|
11
|
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
|
|
12
|
+
|
|
13
|
+
from uk_vehicle_data import verify_webhook # noqa: E402
|
|
14
|
+
|
|
15
|
+
SECRET = os.environ.get("VARDIC_WEBHOOK_SECRET")
|
|
16
|
+
if not SECRET:
|
|
17
|
+
sys.exit("Set VARDIC_WEBHOOK_SECRET")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Handler(BaseHTTPRequestHandler):
|
|
21
|
+
def do_POST(self):
|
|
22
|
+
raw = self.rfile.read(int(self.headers.get("content-length", 0))) # verify the exact bytes
|
|
23
|
+
if not verify_webhook(raw, self.headers.get("vardic-signature", ""), SECRET):
|
|
24
|
+
self.send_response(400)
|
|
25
|
+
self.end_headers()
|
|
26
|
+
self.wfile.write(b"bad signature")
|
|
27
|
+
return
|
|
28
|
+
event = json.loads(raw)
|
|
29
|
+
print("Received", event.get("type"), event.get("data"))
|
|
30
|
+
self.send_response(200)
|
|
31
|
+
self.end_headers()
|
|
32
|
+
self.wfile.write(b"ok")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
if __name__ == "__main__":
|
|
36
|
+
port = int(os.environ.get("PORT", "3000"))
|
|
37
|
+
print(f"Listening for webhooks on port {port}")
|
|
38
|
+
HTTPServer(("", port), Handler).serve_forever()
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "vardic-uk-vehicle-data"
|
|
7
|
+
version = "1.0.2"
|
|
8
|
+
description = "UK vehicle data API client: DVLA vehicle enquiry and DVSA MOT history in one call, with ULEZ estimate, mileage checks and expiry webhooks."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.9"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
maintainers = [{ name = "Vardic", email = "hello@vardic.dev" }]
|
|
13
|
+
authors = [{ name = "Vardic", email = "hello@vardic.dev" }]
|
|
14
|
+
keywords = ["dvla", "dvsa", "mot", "mot-history", "vehicle-data", "ulez", "uk"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 5 - Production/Stable",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
"License :: OSI Approved :: MIT License",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Programming Language :: Python :: 3.13",
|
|
26
|
+
"Typing :: Typed",
|
|
27
|
+
]
|
|
28
|
+
|
|
29
|
+
[project.urls]
|
|
30
|
+
Homepage = "https://vardic.dev/vehicle"
|
|
31
|
+
Documentation = "https://vardic.dev/docs/vehicle"
|
|
32
|
+
Source = "https://github.com/vardic-dev/uk-vehicle-data-python"
|
|
33
|
+
Issues = "https://github.com/vardic-dev/uk-vehicle-data-python/issues"
|
|
34
|
+
Changelog = "https://github.com/vardic-dev/uk-vehicle-data-python/blob/main/CHANGELOG.md"
|
|
35
|
+
|
|
36
|
+
[tool.setuptools]
|
|
37
|
+
packages = ["uk_vehicle_data"]
|
|
38
|
+
|
|
39
|
+
[tool.setuptools.package-data]
|
|
40
|
+
uk_vehicle_data = ["py.typed"]
|
|
File without changes
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"""Test helpers: a fake urlopen that records requests and a webhook signer."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import hmac
|
|
4
|
+
import io
|
|
5
|
+
import json
|
|
6
|
+
import time
|
|
7
|
+
import urllib.error
|
|
8
|
+
from unittest import mock
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class FakeResponse:
|
|
12
|
+
def __init__(self, body=b""):
|
|
13
|
+
self._body = body
|
|
14
|
+
|
|
15
|
+
def read(self):
|
|
16
|
+
return self._body
|
|
17
|
+
|
|
18
|
+
def __enter__(self):
|
|
19
|
+
return self
|
|
20
|
+
|
|
21
|
+
def __exit__(self, *exc):
|
|
22
|
+
return False
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def ok(payload=None):
|
|
26
|
+
return FakeResponse(json.dumps(payload).encode() if payload is not None else b"")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def http_error(status, payload=None, raw=None):
|
|
30
|
+
body = raw if raw is not None else json.dumps(payload if payload is not None else {}).encode()
|
|
31
|
+
return urllib.error.HTTPError("https://api.vardic.dev/x", status, "error", {}, io.BytesIO(body))
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def patch_urlopen(*results):
|
|
35
|
+
"""Patch urlopen to return each result in turn (the last repeats). Exceptions are raised."""
|
|
36
|
+
queue = list(results)
|
|
37
|
+
|
|
38
|
+
def fake(req, timeout=None):
|
|
39
|
+
fake.requests.append(req)
|
|
40
|
+
fake.timeouts.append(timeout)
|
|
41
|
+
r = queue.pop(0) if len(queue) > 1 else queue[0]
|
|
42
|
+
if isinstance(r, BaseException):
|
|
43
|
+
raise r
|
|
44
|
+
return r
|
|
45
|
+
|
|
46
|
+
fake.requests = []
|
|
47
|
+
fake.timeouts = []
|
|
48
|
+
return mock.patch("urllib.request.urlopen", fake), fake
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def sign(raw_body: bytes, secret: str, t=None) -> str:
|
|
52
|
+
t = int(time.time()) if t is None else t
|
|
53
|
+
v1 = hmac.new(secret.encode(), f"{t}.".encode() + raw_body, hashlib.sha256).hexdigest()
|
|
54
|
+
return f"t={t},v1={v1}"
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
import unittest
|
|
4
|
+
from unittest import mock
|
|
5
|
+
|
|
6
|
+
from uk_vehicle_data import VardicApiError, VehicleData
|
|
7
|
+
from tests.helpers import http_error, ok, patch_urlopen
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def make(**kw):
|
|
11
|
+
return VehicleData(api_key="vd_test_abc", **kw)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ConstructorTests(unittest.TestCase):
|
|
15
|
+
def test_requires_a_key(self):
|
|
16
|
+
with mock.patch.dict(os.environ, {}, clear=True):
|
|
17
|
+
with self.assertRaises(ValueError) as cm:
|
|
18
|
+
VehicleData()
|
|
19
|
+
self.assertIn("VARDIC_API_KEY", str(cm.exception))
|
|
20
|
+
|
|
21
|
+
def test_reads_key_from_environment(self):
|
|
22
|
+
with mock.patch.dict(os.environ, {"VARDIC_API_KEY": "vd_test_env"}):
|
|
23
|
+
self.assertEqual(VehicleData().api_key, "vd_test_env")
|
|
24
|
+
|
|
25
|
+
def test_base_url_default_and_trailing_slash(self):
|
|
26
|
+
self.assertEqual(make().base_url, "https://api.vardic.dev")
|
|
27
|
+
self.assertEqual(make(base_url="https://example.test/").base_url, "https://example.test")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class RequestTests(unittest.TestCase):
|
|
31
|
+
def call(self, fn, payload=None):
|
|
32
|
+
patcher, fake = patch_urlopen(ok(payload if payload is not None else {}))
|
|
33
|
+
with patcher:
|
|
34
|
+
result = fn(make())
|
|
35
|
+
return result, fake.requests[0], fake
|
|
36
|
+
|
|
37
|
+
def test_lookup(self):
|
|
38
|
+
result, req, fake = self.call(lambda a: a.lookup("AB12 CDE"), {"data": {"make": "FORD"}})
|
|
39
|
+
self.assertEqual(result, {"data": {"make": "FORD"}})
|
|
40
|
+
self.assertEqual(req.get_method(), "GET")
|
|
41
|
+
self.assertEqual(req.full_url, "https://api.vardic.dev/v1/vehicles/AB12%20CDE")
|
|
42
|
+
self.assertEqual(req.get_header("X-api-key"), "vd_test_abc")
|
|
43
|
+
self.assertTrue(req.get_header("User-agent").startswith("vardic-python/"))
|
|
44
|
+
self.assertIsNone(req.data)
|
|
45
|
+
self.assertEqual(fake.timeouts[0], 15.0)
|
|
46
|
+
|
|
47
|
+
def test_bulk(self):
|
|
48
|
+
_, req, _ = self.call(lambda a: a.bulk(iter(["AB12CDE", "TL12OLD"])))
|
|
49
|
+
self.assertEqual(req.get_method(), "POST")
|
|
50
|
+
self.assertEqual(req.full_url, "https://api.vardic.dev/v1/vehicles/bulk")
|
|
51
|
+
self.assertEqual(req.get_header("Content-type"), "application/json")
|
|
52
|
+
self.assertEqual(json.loads(req.data), {"registrations": ["AB12CDE", "TL12OLD"]})
|
|
53
|
+
|
|
54
|
+
def test_watch(self):
|
|
55
|
+
_, req, _ = self.call(lambda a: a.watch("AB12CDE", "https://example.com/hook", [30, 7, 1]))
|
|
56
|
+
self.assertEqual(req.get_method(), "POST")
|
|
57
|
+
self.assertEqual(req.full_url, "https://api.vardic.dev/v1/watches")
|
|
58
|
+
self.assertEqual(json.loads(req.data),
|
|
59
|
+
{"registration": "AB12CDE", "url": "https://example.com/hook", "daysBefore": [30, 7, 1]})
|
|
60
|
+
|
|
61
|
+
def test_list_watches(self):
|
|
62
|
+
_, req, _ = self.call(lambda a: a.list_watches())
|
|
63
|
+
self.assertEqual((req.get_method(), req.full_url), ("GET", "https://api.vardic.dev/v1/watches"))
|
|
64
|
+
|
|
65
|
+
def test_delete_watch_encodes_id_and_handles_empty_body(self):
|
|
66
|
+
patcher, fake = patch_urlopen(ok())
|
|
67
|
+
with patcher:
|
|
68
|
+
self.assertIsNone(make().delete_watch("w/1"))
|
|
69
|
+
req = fake.requests[0]
|
|
70
|
+
self.assertEqual(req.get_method(), "DELETE")
|
|
71
|
+
self.assertEqual(req.full_url, "https://api.vardic.dev/v1/watches/w%2F1")
|
|
72
|
+
|
|
73
|
+
def test_account(self):
|
|
74
|
+
result, req, _ = self.call(lambda a: a.account(), {"plan": "free"})
|
|
75
|
+
self.assertEqual(result, {"plan": "free"})
|
|
76
|
+
self.assertEqual(req.full_url, "https://api.vardic.dev/v1/account")
|
|
77
|
+
|
|
78
|
+
def test_custom_timeout_is_used(self):
|
|
79
|
+
patcher, fake = patch_urlopen(ok({}))
|
|
80
|
+
with patcher:
|
|
81
|
+
make(timeout=3).account()
|
|
82
|
+
self.assertEqual(fake.timeouts[0], 3)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class ErrorTests(unittest.TestCase):
|
|
86
|
+
def test_maps_api_error_body(self):
|
|
87
|
+
err = http_error(404, {"error": {"code": "vehicle_not_found", "message": "No vehicle found"}})
|
|
88
|
+
patcher, _ = patch_urlopen(err)
|
|
89
|
+
with patcher, self.assertRaises(VardicApiError) as cm:
|
|
90
|
+
make().lookup("ZZ99ZZZ")
|
|
91
|
+
e = cm.exception
|
|
92
|
+
self.assertEqual((e.status, e.code, str(e)), (404, "vehicle_not_found", "No vehicle found"))
|
|
93
|
+
self.assertEqual(e.body["error"]["code"], "vehicle_not_found")
|
|
94
|
+
|
|
95
|
+
def test_falls_back_to_http_error_for_other_bodies(self):
|
|
96
|
+
patcher, _ = patch_urlopen(http_error(502, raw=b"<html>bad gateway</html>"))
|
|
97
|
+
with patcher, self.assertRaises(VardicApiError) as cm:
|
|
98
|
+
make().lookup("AB12CDE")
|
|
99
|
+
e = cm.exception
|
|
100
|
+
self.assertEqual((e.status, e.code, str(e)), (502, "http_error", "HTTP 502"))
|
|
101
|
+
|
|
102
|
+
def test_keeps_quota_and_upstream_codes(self):
|
|
103
|
+
for status, code in [(429, "quota_exceeded"), (503, "upstream_unavailable")]:
|
|
104
|
+
with self.subTest(code=code):
|
|
105
|
+
patcher, _ = patch_urlopen(http_error(status, {"error": {"code": code, "message": code}}))
|
|
106
|
+
with patcher, self.assertRaises(VardicApiError) as cm:
|
|
107
|
+
make().lookup("AB12CDE")
|
|
108
|
+
self.assertEqual((cm.exception.status, cm.exception.code), (status, code))
|
|
109
|
+
|
|
110
|
+
def test_network_errors_pass_through(self):
|
|
111
|
+
patcher, _ = patch_urlopen(OSError("connection refused"))
|
|
112
|
+
with patcher, self.assertRaises(OSError):
|
|
113
|
+
make().lookup("AB12CDE")
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
if __name__ == "__main__":
|
|
117
|
+
unittest.main()
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"""Optional smoke test against the public sandbox.
|
|
2
|
+
|
|
3
|
+
Skipped unless VARDIC_TEST_KEY is set to a vd_test_ key (free at https://vardic.dev/signup).
|
|
4
|
+
Sandbox calls are never counted.
|
|
5
|
+
"""
|
|
6
|
+
import os
|
|
7
|
+
import unittest
|
|
8
|
+
|
|
9
|
+
from uk_vehicle_data import VardicApiError, VehicleData
|
|
10
|
+
|
|
11
|
+
KEY = os.environ.get("VARDIC_TEST_KEY")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@unittest.skipUnless(KEY, "set VARDIC_TEST_KEY to a vd_test_ key to run")
|
|
15
|
+
class SandboxSmokeTests(unittest.TestCase):
|
|
16
|
+
def api(self):
|
|
17
|
+
return VehicleData(api_key=KEY, base_url=os.environ.get("VARDIC_BASE_URL", "https://api.vardic.dev"))
|
|
18
|
+
|
|
19
|
+
def test_lookup_returns_a_vehicle(self):
|
|
20
|
+
self.assertTrue(self.api().lookup("AB12CDE")["data"]["make"])
|
|
21
|
+
|
|
22
|
+
def test_unknown_registration(self):
|
|
23
|
+
with self.assertRaises(VardicApiError) as cm:
|
|
24
|
+
self.api().lookup("ZZ99ZZZ")
|
|
25
|
+
self.assertEqual(cm.exception.code, "vehicle_not_found")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
if __name__ == "__main__":
|
|
29
|
+
unittest.main()
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import time
|
|
3
|
+
import unittest
|
|
4
|
+
|
|
5
|
+
from uk_vehicle_data import verify_webhook
|
|
6
|
+
from tests.helpers import sign
|
|
7
|
+
|
|
8
|
+
SECRET = "whsec_test_secret"
|
|
9
|
+
BODY = json.dumps({"type": "example.event", "data": {"id": "evt_1"}}).encode()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class VerifyWebhookTests(unittest.TestCase):
|
|
13
|
+
def test_accepts_a_correctly_signed_webhook(self):
|
|
14
|
+
self.assertTrue(verify_webhook(BODY, sign(BODY, SECRET), SECRET))
|
|
15
|
+
|
|
16
|
+
def test_rejects_a_wrong_secret(self):
|
|
17
|
+
self.assertFalse(verify_webhook(BODY, sign(BODY, "other_secret"), SECRET))
|
|
18
|
+
|
|
19
|
+
def test_rejects_a_body_changed_after_signing(self):
|
|
20
|
+
self.assertFalse(verify_webhook(BODY + b" ", sign(BODY, SECRET), SECRET))
|
|
21
|
+
|
|
22
|
+
def test_rejects_a_tampered_signature(self):
|
|
23
|
+
header = sign(BODY, SECRET)
|
|
24
|
+
header = header[:-1] + ("0" if header[-1] != "0" else "1")
|
|
25
|
+
self.assertFalse(verify_webhook(BODY, header, SECRET))
|
|
26
|
+
|
|
27
|
+
def test_rejects_a_signature_of_the_wrong_length(self):
|
|
28
|
+
self.assertFalse(verify_webhook(BODY, f"t={int(time.time())},v1=abcd", SECRET))
|
|
29
|
+
|
|
30
|
+
def test_rejects_an_old_timestamp_unless_tolerance_is_wider(self):
|
|
31
|
+
header = sign(BODY, SECRET, t=int(time.time()) - 3600)
|
|
32
|
+
self.assertFalse(verify_webhook(BODY, header, SECRET))
|
|
33
|
+
self.assertTrue(verify_webhook(BODY, header, SECRET, tolerance_secs=7200))
|
|
34
|
+
|
|
35
|
+
def test_rejects_missing_or_malformed_headers(self):
|
|
36
|
+
for header in ["", "garbage", "t=abc,v1=abcd", "v1=abcd", f"t={int(time.time())}"]:
|
|
37
|
+
with self.subTest(header=header):
|
|
38
|
+
self.assertFalse(verify_webhook(BODY, header, SECRET))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
if __name__ == "__main__":
|
|
42
|
+
unittest.main()
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""UK vehicle data API client: DVLA vehicle enquiry and DVSA MOT history in one call.
|
|
2
|
+
|
|
3
|
+
Docs: https://vardic.dev/docs/vehicle Free key: https://vardic.dev/signup
|
|
4
|
+
Standard library only (Python 3.9+).
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import hashlib
|
|
9
|
+
import hmac
|
|
10
|
+
import json
|
|
11
|
+
import os
|
|
12
|
+
import time
|
|
13
|
+
import urllib.error
|
|
14
|
+
import urllib.parse
|
|
15
|
+
import urllib.request
|
|
16
|
+
from typing import Any, Iterable, Optional
|
|
17
|
+
|
|
18
|
+
__all__ = ["VehicleData", "VardicApiError", "verify_webhook"]
|
|
19
|
+
__version__ = "1.0.2"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class VardicApiError(Exception):
|
|
23
|
+
def __init__(self, status: int, code: str, message: str, body: Any = None):
|
|
24
|
+
super().__init__(message)
|
|
25
|
+
self.status, self.code, self.body = status, code, body
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class _Client:
|
|
29
|
+
def __init__(self, api_key: Optional[str] = None, base_url: str = "https://api.vardic.dev", timeout: float = 15.0):
|
|
30
|
+
self.api_key = api_key or os.environ.get("VARDIC_API_KEY")
|
|
31
|
+
if not self.api_key:
|
|
32
|
+
raise ValueError("Pass api_key or set VARDIC_API_KEY. Free key: https://vardic.dev/signup")
|
|
33
|
+
self.base_url = base_url.rstrip("/")
|
|
34
|
+
self.timeout = timeout
|
|
35
|
+
|
|
36
|
+
def _req(self, method: str, path: str, body: Any = None) -> Any:
|
|
37
|
+
data = json.dumps(body).encode() if body is not None else None
|
|
38
|
+
req = urllib.request.Request(self.base_url + path, data=data, method=method)
|
|
39
|
+
req.add_header("x-api-key", self.api_key)
|
|
40
|
+
req.add_header("user-agent", f"vardic-python/{__version__}")
|
|
41
|
+
if data is not None:
|
|
42
|
+
req.add_header("content-type", "application/json")
|
|
43
|
+
try:
|
|
44
|
+
with urllib.request.urlopen(req, timeout=self.timeout) as res:
|
|
45
|
+
raw = res.read()
|
|
46
|
+
return json.loads(raw) if raw else None
|
|
47
|
+
except urllib.error.HTTPError as e:
|
|
48
|
+
try:
|
|
49
|
+
j = json.loads(e.read() or b"{}")
|
|
50
|
+
except ValueError:
|
|
51
|
+
j = {}
|
|
52
|
+
finally:
|
|
53
|
+
e.close()
|
|
54
|
+
err = j.get("error") or {}
|
|
55
|
+
raise VardicApiError(e.code, err.get("code", "http_error"), err.get("message", f"HTTP {e.code}"), j) from None
|
|
56
|
+
|
|
57
|
+
def list_watches(self) -> Any:
|
|
58
|
+
return self._req("GET", "/v1/watches")
|
|
59
|
+
|
|
60
|
+
def delete_watch(self, watch_id: str) -> None:
|
|
61
|
+
self._req("DELETE", "/v1/watches/" + urllib.parse.quote(watch_id, safe=""))
|
|
62
|
+
|
|
63
|
+
def account(self) -> Any:
|
|
64
|
+
return self._req("GET", "/v1/account")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class VehicleData(_Client):
|
|
68
|
+
"""Client for the UK vehicle data API.
|
|
69
|
+
|
|
70
|
+
>>> api = VehicleData() # reads VARDIC_API_KEY
|
|
71
|
+
>>> v = api.lookup("AB12 CDE")["data"]
|
|
72
|
+
>>> v["mot"]["expiryDate"], v["mileage"]["anomalyFound"]
|
|
73
|
+
"""
|
|
74
|
+
|
|
75
|
+
def lookup(self, registration: str) -> Any:
|
|
76
|
+
return self._req("GET", "/v1/vehicles/" + urllib.parse.quote(registration, safe=""))
|
|
77
|
+
|
|
78
|
+
def bulk(self, registrations: Iterable[str]) -> Any:
|
|
79
|
+
return self._req("POST", "/v1/vehicles/bulk", {"registrations": list(registrations)})
|
|
80
|
+
|
|
81
|
+
def watch(self, registration: str, url: str, days_before: Optional[list] = None) -> Any:
|
|
82
|
+
return self._req("POST", "/v1/watches", {"registration": registration, "url": url, "daysBefore": days_before})
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def verify_webhook(raw_body: bytes, header: str, secret: str, tolerance_secs: int = 300) -> bool:
|
|
86
|
+
"""Check the vardic-signature header of a webhook. raw_body must be the exact bytes received."""
|
|
87
|
+
try:
|
|
88
|
+
parts = dict(p.split("=", 1) for p in header.split(","))
|
|
89
|
+
ts = int(parts["t"])
|
|
90
|
+
except (KeyError, ValueError):
|
|
91
|
+
return False
|
|
92
|
+
if abs(time.time() - ts) > tolerance_secs:
|
|
93
|
+
return False
|
|
94
|
+
expected = hmac.new(secret.encode(), f"{ts}.".encode() + raw_body, hashlib.sha256).hexdigest()
|
|
95
|
+
return hmac.compare_digest(expected, parts.get("v1", ""))
|
|
File without changes
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: vardic-uk-vehicle-data
|
|
3
|
+
Version: 1.0.2
|
|
4
|
+
Summary: UK vehicle data API client: DVLA vehicle enquiry and DVSA MOT history in one call, with ULEZ estimate, mileage checks and expiry webhooks.
|
|
5
|
+
Author-email: Vardic <hello@vardic.dev>
|
|
6
|
+
Maintainer-email: Vardic <hello@vardic.dev>
|
|
7
|
+
License: MIT
|
|
8
|
+
Project-URL: Homepage, https://vardic.dev/vehicle
|
|
9
|
+
Project-URL: Documentation, https://vardic.dev/docs/vehicle
|
|
10
|
+
Project-URL: Source, https://github.com/vardic-dev/uk-vehicle-data-python
|
|
11
|
+
Project-URL: Issues, https://github.com/vardic-dev/uk-vehicle-data-python/issues
|
|
12
|
+
Project-URL: Changelog, https://github.com/vardic-dev/uk-vehicle-data-python/blob/main/CHANGELOG.md
|
|
13
|
+
Keywords: dvla,dvsa,mot,mot-history,vehicle-data,ulez,uk
|
|
14
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.9
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# UK vehicle data API for Python
|
|
31
|
+
|
|
32
|
+
[](https://pypi.org/project/vardic-uk-vehicle-data/) [](https://github.com/vardic-dev/uk-vehicle-data-python/actions/workflows/ci.yml) [](LICENSE)
|
|
33
|
+
|
|
34
|
+
Look up any UK registration and get the DVLA record and full DVSA MOT history in one call, with mileage checks, an estimated ULEZ status and MOT and tax countdowns.
|
|
35
|
+
|
|
36
|
+
```python
|
|
37
|
+
from uk_vehicle_data import VehicleData
|
|
38
|
+
|
|
39
|
+
api = VehicleData() # reads VARDIC_API_KEY
|
|
40
|
+
v = api.lookup("AB12 CDE")["data"]
|
|
41
|
+
|
|
42
|
+
print(v["make"], v["model"], v["colour"], v["fuelType"])
|
|
43
|
+
print("MOT expires", v["mot"]["expiryDate"], f'({v["mot"]["daysUntilExpiry"]} days)')
|
|
44
|
+
print("Tax", v["tax"]["status"], v["tax"]["dueDate"])
|
|
45
|
+
print("ULEZ (estimate)", (v["cleanAirZone"] or {}).get("result"))
|
|
46
|
+
for a in v["mileage"]["anomalies"]:
|
|
47
|
+
print(a["type"], a["detail"])
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Install
|
|
51
|
+
|
|
52
|
+
```sh
|
|
53
|
+
pip install vardic-uk-vehicle-data
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Python 3.9+, standard library only.
|
|
57
|
+
|
|
58
|
+
## What you get
|
|
59
|
+
|
|
60
|
+
- **Vehicle record** from the DVLA: make, colour, fuel, engine size, CO2, Euro status, first registration, tax status and due date.
|
|
61
|
+
- **MOT history** from the DVSA: every test with result, mileage, expiry and defects.
|
|
62
|
+
- **Mileage checks**: readings over time, average miles a year and anomalies such as a reading that went down.
|
|
63
|
+
- **Clean air zone estimate**: `likely_compliant`, `likely_non_compliant`, `uncertain` or `unknown`, with the basis for it.
|
|
64
|
+
- **Bulk lookups** (`bulk`): up to 25 registrations in one call.
|
|
65
|
+
- **Expiry webhooks** (`watch`): signed events before MOT or tax runs out, and when a new MOT test is recorded.
|
|
66
|
+
|
|
67
|
+
## Sandbox key
|
|
68
|
+
|
|
69
|
+
Every account gets a `vd_test_` key as well as a live key. It returns made-up data for any registration and is never counted. `ZZ99ZZZ` returns `vehicle_not_found` and `TL12OLD` returns an old diesel whose mileage went down.
|
|
70
|
+
|
|
71
|
+
## Webhooks
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
api.watch("AB12CDE", "https://example.com/hooks/vardic", days_before=[30, 7, 1])
|
|
75
|
+
|
|
76
|
+
from uk_vehicle_data import verify_webhook
|
|
77
|
+
ok = verify_webhook(request.body, request.headers["vardic-signature"], WEBHOOK_SECRET)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Watches need a live key. Pass the raw request body as bytes. Event types and payloads: https://vardic.dev/docs/webhooks
|
|
81
|
+
|
|
82
|
+
## Errors
|
|
83
|
+
|
|
84
|
+
Failed calls raise `VardicApiError` with `.status` and `.code`, for example `vehicle_not_found` (404), `quota_exceeded` (429) or `upstream_unavailable` (503). Calls that fail with 503 are not counted.
|
|
85
|
+
|
|
86
|
+
## Pricing
|
|
87
|
+
|
|
88
|
+
Free plan: 100 lookups a month, no card needed. Paid plans start at £59 a month for 2,000 lookups. See https://vardic.dev/pricing
|
|
89
|
+
|
|
90
|
+
## Links
|
|
91
|
+
|
|
92
|
+
- Get a free key: https://vardic.dev/signup
|
|
93
|
+
- API docs: https://vardic.dev/docs/vehicle
|
|
94
|
+
- JavaScript client: https://github.com/vardic-dev/uk-vehicle-data-js
|
|
95
|
+
- Examples: [examples/](examples/)
|
|
96
|
+
- Changelog: [CHANGELOG.md](CHANGELOG.md)
|
|
97
|
+
- Contributing: [CONTRIBUTING.md](CONTRIBUTING.md)
|
|
98
|
+
- Security: [SECURITY.md](SECURITY.md)
|
|
99
|
+
|
|
100
|
+
## Data
|
|
101
|
+
|
|
102
|
+
Contains public sector information licensed under the Open Government Licence v3.0. Not affiliated with or endorsed by the DVLA or DVSA. Clean air zone results are estimates: check the official TfL or GOV.UK vehicle checker before relying on one.
|
|
103
|
+
|
|
104
|
+
## Licence
|
|
105
|
+
|
|
106
|
+
MIT. Made by [Vardic](https://vardic.dev).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
CHANGELOG.md
|
|
2
|
+
CONTRIBUTING.md
|
|
3
|
+
LICENSE
|
|
4
|
+
MANIFEST.in
|
|
5
|
+
README.md
|
|
6
|
+
SECURITY.md
|
|
7
|
+
pyproject.toml
|
|
8
|
+
examples/lookup.py
|
|
9
|
+
examples/mileage_check.py
|
|
10
|
+
examples/webhook_server.py
|
|
11
|
+
tests/__init__.py
|
|
12
|
+
tests/helpers.py
|
|
13
|
+
tests/test_client.py
|
|
14
|
+
tests/test_live.py
|
|
15
|
+
tests/test_webhook.py
|
|
16
|
+
uk_vehicle_data/__init__.py
|
|
17
|
+
uk_vehicle_data/py.typed
|
|
18
|
+
vardic_uk_vehicle_data.egg-info/PKG-INFO
|
|
19
|
+
vardic_uk_vehicle_data.egg-info/SOURCES.txt
|
|
20
|
+
vardic_uk_vehicle_data.egg-info/dependency_links.txt
|
|
21
|
+
vardic_uk_vehicle_data.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
uk_vehicle_data
|