disruptapi 0.2.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.
- disruptapi-0.2.0/.gitignore +27 -0
- disruptapi-0.2.0/LICENSE +21 -0
- disruptapi-0.2.0/PKG-INFO +342 -0
- disruptapi-0.2.0/README.md +312 -0
- disruptapi-0.2.0/pyproject.toml +57 -0
- disruptapi-0.2.0/src/disruptapi/__init__.py +63 -0
- disruptapi-0.2.0/src/disruptapi/__main__.py +6 -0
- disruptapi-0.2.0/src/disruptapi/_base_client.py +288 -0
- disruptapi-0.2.0/src/disruptapi/_client.py +117 -0
- disruptapi-0.2.0/src/disruptapi/_errors.py +154 -0
- disruptapi-0.2.0/src/disruptapi/_rate_limit.py +98 -0
- disruptapi-0.2.0/src/disruptapi/_version.py +1 -0
- disruptapi-0.2.0/src/disruptapi/cli.py +529 -0
- disruptapi-0.2.0/src/disruptapi/pagination.py +166 -0
- disruptapi-0.2.0/src/disruptapi/py.typed +0 -0
- disruptapi-0.2.0/src/disruptapi/resources/__init__.py +1 -0
- disruptapi-0.2.0/src/disruptapi/resources/_specs.py +236 -0
- disruptapi-0.2.0/src/disruptapi/resources/async_.py +315 -0
- disruptapi-0.2.0/src/disruptapi/resources/sync.py +307 -0
- disruptapi-0.2.0/src/disruptapi/types.py +455 -0
- disruptapi-0.2.0/src/disruptapi/webhooks.py +122 -0
- disruptapi-0.2.0/tests/__init__.py +0 -0
- disruptapi-0.2.0/tests/conftest.py +69 -0
- disruptapi-0.2.0/tests/test_async_and_pagination.py +76 -0
- disruptapi-0.2.0/tests/test_cli.py +352 -0
- disruptapi-0.2.0/tests/test_client.py +258 -0
- disruptapi-0.2.0/tests/test_phase6_endpoints.py +85 -0
- disruptapi-0.2.0/tests/test_rate_limit.py +34 -0
- disruptapi-0.2.0/tests/test_webhooks.py +91 -0
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Node / TypeScript
|
|
2
|
+
node_modules/
|
|
3
|
+
typescript/dist/
|
|
4
|
+
*.tgz
|
|
5
|
+
*.tsbuildinfo
|
|
6
|
+
coverage/
|
|
7
|
+
|
|
8
|
+
# Python
|
|
9
|
+
.venv/
|
|
10
|
+
__pycache__/
|
|
11
|
+
*.py[cod]
|
|
12
|
+
*.egg-info/
|
|
13
|
+
python/dist/
|
|
14
|
+
python/build/
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.mypy_cache/
|
|
17
|
+
.ruff_cache/
|
|
18
|
+
|
|
19
|
+
# Editors and OS
|
|
20
|
+
.DS_Store
|
|
21
|
+
.idea/
|
|
22
|
+
.vscode/
|
|
23
|
+
*.swp
|
|
24
|
+
|
|
25
|
+
# Secrets
|
|
26
|
+
.env
|
|
27
|
+
.env.*
|
disruptapi-0.2.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Digital Envision LLC
|
|
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,342 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: disruptapi
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: Official Python client for DisruptAPI, the travel disruption intelligence API.
|
|
5
|
+
Project-URL: Homepage, https://disruptapi.com
|
|
6
|
+
Project-URL: Documentation, https://disruptapi.com/docs/
|
|
7
|
+
Author: Digital Envision LLC
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Keywords: api-client,aviation,disruptapi,disruption,outage,sdk,travel
|
|
11
|
+
Classifier: Development Status :: 4 - Beta
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Requires-Python: >=3.9
|
|
23
|
+
Requires-Dist: httpx<1,>=0.23
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: mypy>=1.5; extra == 'dev'
|
|
26
|
+
Requires-Dist: pytest-asyncio>=0.21; extra == 'dev'
|
|
27
|
+
Requires-Dist: pytest>=7; extra == 'dev'
|
|
28
|
+
Requires-Dist: respx>=0.20; extra == 'dev'
|
|
29
|
+
Description-Content-Type: text/markdown
|
|
30
|
+
|
|
31
|
+
# disruptapi
|
|
32
|
+
|
|
33
|
+
The official Python client for [DisruptAPI](https://disruptapi.com), the travel disruption intelligence API: live signals (IT outages, aviation programs, weather warnings, natural hazards, advisories, internet outages), exposure checks for flights, trips, places, carriers and providers, and impact forecasts based on past events.
|
|
34
|
+
|
|
35
|
+
> **Not yet published.** This package is not on PyPI yet. Until it is, install it from this repository: `pip install ./python` (or build a wheel, see [Development](#development)).
|
|
36
|
+
|
|
37
|
+
- Python 3.9+, built on `httpx`. Sync `DisruptAPI` and async `AsyncDisruptAPI` clients.
|
|
38
|
+
- `TypedDict` models for every response (`disruptapi.types`), with a `py.typed` marker.
|
|
39
|
+
- Typed errors, automatic retries with backoff, rate-limit info on every response.
|
|
40
|
+
- Auto-paginating list methods.
|
|
41
|
+
- Webhook signature verification.
|
|
42
|
+
- A `disruptapi` command-line tool ([CLI](#cli)).
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install disruptapi
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quick start: an exposure check
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from disruptapi import DisruptAPI
|
|
54
|
+
|
|
55
|
+
client = DisruptAPI() # reads DISRUPT_API_KEY
|
|
56
|
+
|
|
57
|
+
results = client.exposure.check(
|
|
58
|
+
[
|
|
59
|
+
{"id": "BKG-7QX2LM", "kind": "flight", "carrier": "AS", "origin": "SEA", "destination": "HNL",
|
|
60
|
+
"depart_at": "2026-11-21T08:00:00Z"},
|
|
61
|
+
{"id": "HTL-ALCUDIA", "kind": "place", "lat": 39.85, "lon": 3.12, "radius_km": 40},
|
|
62
|
+
{"id": "checkout", "kind": "provider", "provider": "cloudflare"},
|
|
63
|
+
],
|
|
64
|
+
min_severity="moderate",
|
|
65
|
+
)
|
|
66
|
+
for r in results:
|
|
67
|
+
print(r["item_id"], r["risk_level"], [m["signal"]["title"] for m in r["matches"]])
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Async:
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import asyncio
|
|
74
|
+
from disruptapi import AsyncDisruptAPI
|
|
75
|
+
|
|
76
|
+
async def main():
|
|
77
|
+
async with AsyncDisruptAPI() as client:
|
|
78
|
+
results = await client.exposure.check([{"id": "SEA", "airport": "SEA"}])
|
|
79
|
+
|
|
80
|
+
asyncio.run(main())
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Responses are plain dicts, typed with the `TypedDict`s in `disruptapi.types` (`Signal`, `ExposureResult`, `Watchlist`, `Webhook`, ...). Keep your key on the server.
|
|
84
|
+
|
|
85
|
+
## Methods
|
|
86
|
+
|
|
87
|
+
Parameter names are the API's own. Every method returns the `data` member of the response. The async client has the same methods as coroutines.
|
|
88
|
+
|
|
89
|
+
| Method | Endpoint |
|
|
90
|
+
| --- | --- |
|
|
91
|
+
| `signals.list(status=, type=, min_severity=, country=, airport=, provider=, since=, limit=, offset=)` | `GET /v1/signals` (paginated) |
|
|
92
|
+
| `signals.get(signal_id)` | `GET /v1/signals/{signal_id}` |
|
|
93
|
+
| `exposure.check(items, trips=, min_severity=)` | `POST /v1/exposure/check` |
|
|
94
|
+
| `forecast.get(failure_layer=, provider=)` | `GET /v1/forecast` |
|
|
95
|
+
| `failure_layers.list()` | `GET /v1/failure-layers` |
|
|
96
|
+
| `watchlists.list()` | `GET /v1/watchlists` (returns `{"data", "limits"}`) |
|
|
97
|
+
| `watchlists.create(name=, description=, items=, idempotency_key=)` | `POST /v1/watchlists` |
|
|
98
|
+
| `watchlists.get(wl_id, limit=, after=)` | `GET /v1/watchlists/{wl_id}` (`items_meta` has `has_more` and `next_after`) |
|
|
99
|
+
| `watchlists.update(wl_id, name=, description=)` | `PATCH /v1/watchlists/{wl_id}` |
|
|
100
|
+
| `watchlists.delete(wl_id)` | `DELETE /v1/watchlists/{wl_id}` |
|
|
101
|
+
| `watchlists.items.upsert(wl_id, items, idempotency_key=)` | `POST /v1/watchlists/{wl_id}/items` |
|
|
102
|
+
| `watchlists.items.remove(wl_id, ref)` | `DELETE /v1/watchlists/{wl_id}/items/{ref}` |
|
|
103
|
+
| `watchlists.exposure(wl_id, min_severity=)` | `GET /v1/watchlists/{wl_id}/exposure` |
|
|
104
|
+
| `airports.get(iata)` | `GET /v1/airports/{iata}` |
|
|
105
|
+
| `airports.faa_outlook(iata)` | `GET /v1/airports/{iata}/faa-outlook` |
|
|
106
|
+
| `airports.observed_weather(iata, start=, end=)` | `GET /v1/airports/{iata}/observed-weather` |
|
|
107
|
+
| `changes.list(after=, limit=)` | `GET /v1/changes` (returns `{"data", "meta"}`) |
|
|
108
|
+
| `changes.iter(after=, limit=)` | every change from `after` to the newest, across pages |
|
|
109
|
+
| `incidents.list(status=, type=, min_severity=, country=, airport=, limit=, offset=)` | `GET /v1/incidents` (paginated) |
|
|
110
|
+
| `incidents.get(incident_id)` | `GET /v1/incidents/{incident_id}` |
|
|
111
|
+
| `severities.list()` | `GET /v1/severities` |
|
|
112
|
+
| `flights.outcome(carrier=, flight_number=, date=, origin=, destination=)` | `GET /v1/flights/outcome` |
|
|
113
|
+
| `flights.outlook(carrier=, origin=, destination=, date=, departure_time=)` | `GET /v1/flights/outlook` |
|
|
114
|
+
| `evidence.get(date=, airport=, origin=, destination=, carrier=, flight_number=)` | `GET /v1/evidence` (JSON) |
|
|
115
|
+
| `carriers.get(code)` | `GET /v1/carriers/{code}` |
|
|
116
|
+
| `sources.list()` | `GET /v1/sources` |
|
|
117
|
+
| `providers.list()` | `GET /v1/providers` |
|
|
118
|
+
| `events.list(provider=, failure_layer=, anchor_only=, limit=, offset=)` | `GET /v1/events` (paginated) |
|
|
119
|
+
| `events.get(event_id)` | `GET /v1/events/{event_id}` |
|
|
120
|
+
| `webhooks.list()` | `GET /v1/webhooks` |
|
|
121
|
+
| `webhooks.create(url=, filters=, format=, routing_key=, idempotency_key=)` | `POST /v1/webhooks` (`format`: `json`, `slack`, `teams`, `pagerduty`) |
|
|
122
|
+
| `webhooks.get(hook_id)` | `GET /v1/webhooks/{hook_id}` |
|
|
123
|
+
| `webhooks.update(hook_id, url=, filters=)` | `PATCH /v1/webhooks/{hook_id}` |
|
|
124
|
+
| `webhooks.rotate_secret(hook_id)` | `POST /v1/webhooks/{hook_id}/rotate-secret` (the old secret signs too for 24 hours) |
|
|
125
|
+
| `webhooks.deliveries(hook_id, status=, before=, limit=)` | `GET /v1/webhooks/{hook_id}/deliveries` |
|
|
126
|
+
| `webhooks.redeliver(hook_id, delivery_id)` | `POST /v1/webhooks/{hook_id}/deliveries/{delivery_id}/redeliver` |
|
|
127
|
+
| `webhooks.test(hook_id)` | `POST /v1/webhooks/{hook_id}/test` |
|
|
128
|
+
| `webhooks.delete(hook_id)` | `DELETE /v1/webhooks/{hook_id}` |
|
|
129
|
+
| `me()` | `GET /v1/me` |
|
|
130
|
+
| `usage(days=)` | `GET /v1/usage` |
|
|
131
|
+
| `plans()` | `GET /v1/plans` (needs no key) |
|
|
132
|
+
|
|
133
|
+
For other options on one call, use `client.with_options(timeout=5, max_retries=0).signals.get(...)`.
|
|
134
|
+
|
|
135
|
+
### Staying in sync: the change feed
|
|
136
|
+
|
|
137
|
+
`GET /v1/changes` lists every signal create, update and resolve in order. Store the last `cursor` you processed and resume from it; this never misses or repeats a change, unlike polling `signals.list(since=)`:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
cursor = load_cursor() # None the first time: start from the oldest kept change
|
|
141
|
+
for change in client.changes.iter(after=cursor):
|
|
142
|
+
handle(change["event"], change["signal"])
|
|
143
|
+
cursor = change["cursor"]
|
|
144
|
+
save_cursor(cursor) # call again in a minute to continue
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
Cursors are kept 30 days. An older one raises `GoneError` with code `cursor_expired`: start again with no cursor (and reconcile with `signals.list()`).
|
|
148
|
+
|
|
149
|
+
**Item ids with `/`.** The SDK URL-encodes path parameters, but API v1.0.0 does not route an encoded `/` (`%2F`), so `watchlists.items.remove` cannot remove an item whose `id` contains `/` (it gets a 404 `not_found`). Avoid `/` in item ids.
|
|
150
|
+
|
|
151
|
+
## Error handling
|
|
152
|
+
|
|
153
|
+
Every error is a `DisruptError` with `status`, `code` (the API's stable `error.code`), `message`, `details` (validation problems) and `request_id`. Branch on the class or on `code`, not on `message`.
|
|
154
|
+
|
|
155
|
+
| Class | When |
|
|
156
|
+
| --- | --- |
|
|
157
|
+
| `AuthenticationError` | 401 `missing_api_key`, `invalid_api_key` |
|
|
158
|
+
| `PermissionDeniedError` | 403 `plan_required`, `ip_not_allowed` (key used outside its IP allowlist), `insufficient_scope` (a read-only key on a write) |
|
|
159
|
+
| `NotFoundError` | 404 `not_found` |
|
|
160
|
+
| `ValidationError` | 400 and 422: `invalid_request`, `limit_too_large`, `invalid_items`, `invalid_webhook`, `idempotency_conflict` (same key, different body), ... |
|
|
161
|
+
| `ConflictError` | 409 `idempotency_in_progress` (retried automatically) |
|
|
162
|
+
| `GoneError` | 410 `cursor_expired` (change feed), `gone` (an operation past its sunset date) |
|
|
163
|
+
| `PayloadTooLargeError` | 413 `payload_too_large` |
|
|
164
|
+
| `RateLimitError` | 429 `rate_limited`, `quota_exceeded`, `daily_data_limit`; has `retry_after` and `limit_reason` |
|
|
165
|
+
| `InternalServerError` | 5xx |
|
|
166
|
+
| `APIConnectionError`, `APITimeoutError` | no response |
|
|
167
|
+
| `WebhookSignatureError` | from `construct_event` |
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
from disruptapi import DisruptError, RateLimitError, ValidationError
|
|
171
|
+
|
|
172
|
+
try:
|
|
173
|
+
client.signals.list(limit=500)
|
|
174
|
+
except ValidationError as e:
|
|
175
|
+
if e.code == "limit_too_large":
|
|
176
|
+
... # lower limit and page with offset
|
|
177
|
+
except RateLimitError as e:
|
|
178
|
+
print(e.code, "retry after", e.retry_after)
|
|
179
|
+
except DisruptError as e:
|
|
180
|
+
print(e.status, e.code, e.message, e.request_id)
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Retries
|
|
184
|
+
|
|
185
|
+
Failed calls are retried up to `max_retries` times (default 3) with exponential backoff and jitter:
|
|
186
|
+
|
|
187
|
+
- **429 `rate_limited`** on any method, after `Retry-After` seconds. A `Retry-After` longer than `max_retry_after` (default 60 s) is raised instead. `quota_exceeded` and `daily_data_limit` are never retried: they last until the month or UTC day resets.
|
|
188
|
+
- **5xx, 408 and network errors or timeouts** on safe calls: `GET`, `DELETE`, `PATCH`, `exposure.check` (read-only), and the creating calls (`watchlists.create`, `watchlists.items.upsert`, `webhooks.create`, `webhooks.rotate_secret`). The creating calls send an `Idempotency-Key` header, the same one on every retry of a call, so the API answers a repeat with the first result instead of creating a duplicate. Pass `idempotency_key=` to use your own (for example, your job id) across process restarts; keys last 24 hours. `webhooks.redeliver` and `webhooks.test` are not retried.
|
|
189
|
+
- **409 `idempotency_in_progress`**: the first attempt with that key is still running; the SDK waits and asks again.
|
|
190
|
+
|
|
191
|
+
## Rate limits
|
|
192
|
+
|
|
193
|
+
`client.last_response` holds the latest response in the current thread or asyncio task, and `client.last_rate_limit` its parsed headers:
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
client.signals.get(signal_id)
|
|
197
|
+
rl = client.last_rate_limit
|
|
198
|
+
print(rl.limit, rl.remaining, rl.reset, rl.quota_remaining, rl.data_records_remaining_today)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Pages have `page.rate_limit`, and errors have `e.rate_limit`.
|
|
202
|
+
|
|
203
|
+
The SDK reads the `X-RateLimit-Limit`, `-Remaining` and `-Reset` headers. The API is also adding the IETF fields (draft-ietf-httpapi-ratelimit-headers): `RateLimit-Limit`, `RateLimit-Remaining`, `RateLimit-Reset`, the structured `RateLimit` and `RateLimit-Policy`. When an `X-` header is absent, the SDK uses the IETF one instead. IETF resets are delta-seconds; `rate_limit.reset` is always Unix seconds, and `rate_limit.policy` holds the raw `RateLimit-Policy`.
|
|
204
|
+
|
|
205
|
+
The API's OpenAPI spec is also gaining OAuth 2.1 security schemes, for apps that act for a signed-in organisation. An OAuth access token works with this SDK wherever an API key does: pass it as `api_key`, and it is sent as `Authorization: Bearer <token>`. The SDK does not run the OAuth flow itself.
|
|
206
|
+
|
|
207
|
+
## Pagination
|
|
208
|
+
|
|
209
|
+
`signals.list` and `events.list` page with `limit` and `offset`. They return the first page; iterating it reads every page:
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
page = client.signals.list(type=["aviation", "weather"], country="US", limit=50)
|
|
213
|
+
page.data # this page's signals
|
|
214
|
+
page.meta # {"total", "limit", "offset"}
|
|
215
|
+
page.has_next_page()
|
|
216
|
+
page.next_page()
|
|
217
|
+
|
|
218
|
+
for signal in client.signals.list(status="all", since=datetime.now(timezone.utc) - timedelta(hours=1)):
|
|
219
|
+
... # every matching signal, across pages
|
|
220
|
+
|
|
221
|
+
first_200 = client.events.list(anchor_only=True).to_list(max_items=200)
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Async: `page = await client.signals.list(...)` for one page, or `async for signal in client.signals.list(...)` for all.
|
|
225
|
+
|
|
226
|
+
Iteration stops when `offset + len(data) >= total` or a page is empty, and skips items it has already yielded (offset paging over changing data can repeat one). `limit` must be at most your plan's page size (Sandbox 50, Starter 200, Pro 500). Records count against the daily record budget, so filter. A `datetime` for `since` is sent as `YYYY-MM-DDTHH:MM:SSZ` (naive datetimes are taken as UTC), the form the API compares against.
|
|
227
|
+
|
|
228
|
+
## Webhook verification
|
|
229
|
+
|
|
230
|
+
Deliveries carry `Disrupt-Signature: t=<unix seconds>,v1=<hex HMAC-SHA256>`. Verify the **raw** body before parsing it. Signatures more than 300 seconds from your clock are refused.
|
|
231
|
+
|
|
232
|
+
```python
|
|
233
|
+
from disruptapi import verify_signature, construct_event
|
|
234
|
+
|
|
235
|
+
ok = verify_signature(raw_body, signature_header, secret) # bool
|
|
236
|
+
event = construct_event(raw_body, signature_header, secret) # raises WebhookSignatureError
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Both are also on the client (`client.webhooks.verify_signature(...)`) and in `disruptapi.webhooks`. Pass `tolerance=` to change the window.
|
|
240
|
+
|
|
241
|
+
### FastAPI
|
|
242
|
+
|
|
243
|
+
```python
|
|
244
|
+
import os
|
|
245
|
+
from fastapi import FastAPI, HTTPException, Request, Response
|
|
246
|
+
from disruptapi import WebhookSignatureError, construct_event
|
|
247
|
+
|
|
248
|
+
app = FastAPI()
|
|
249
|
+
|
|
250
|
+
@app.post("/webhooks/disrupt")
|
|
251
|
+
async def disrupt_webhook(request: Request) -> Response:
|
|
252
|
+
payload = await request.body() # raw bytes
|
|
253
|
+
try:
|
|
254
|
+
event = construct_event(payload, request.headers.get("Disrupt-Signature"), os.environ["DISRUPT_WEBHOOK_SECRET"])
|
|
255
|
+
except WebhookSignatureError:
|
|
256
|
+
raise HTTPException(status_code=400, detail="bad signature")
|
|
257
|
+
signal = event["data"]["signal"] if event["type"].startswith("exposure.") else event["data"]
|
|
258
|
+
# Deduplicate on signal["id"] + signal["version"], not on event["id"].
|
|
259
|
+
return Response(status_code=204)
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
To test your handler, sign a sample with `generate_test_header(body, secret)`.
|
|
263
|
+
|
|
264
|
+
## CLI
|
|
265
|
+
|
|
266
|
+
Installing the package adds a `disruptapi` command (also `python -m disruptapi`). It uses only the standard library's `argparse` on top of the SDK.
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
export DISRUPT_API_KEY=dk_live_... # or DISRUPT_KEY, or --api-key
|
|
270
|
+
|
|
271
|
+
disruptapi signals list --type aviation,weather --country US --min-severity severe --format table
|
|
272
|
+
disruptapi signals list --status all --since 2026-11-18T06:00:00Z --limit 50 --all
|
|
273
|
+
disruptapi signals get faa_ground_stop_SEA.k3j9x2m1qa
|
|
274
|
+
disruptapi exposure check --file items.json --min-severity moderate
|
|
275
|
+
echo '[{"id": "SEA-stay", "airport": "SEA"}]' | disruptapi exposure check --file - --format table
|
|
276
|
+
disruptapi forecast identity --provider azure
|
|
277
|
+
disruptapi airports get SEA
|
|
278
|
+
disruptapi carriers get AA
|
|
279
|
+
disruptapi providers list --format table
|
|
280
|
+
disruptapi watchlists list --format table
|
|
281
|
+
disruptapi watchlists create --name "Winter departures" --file items.json
|
|
282
|
+
disruptapi watchlists get wl_3f9a1c2b7d4e5f60
|
|
283
|
+
disruptapi watchlists add-items wl_3f9a1c2b7d4e5f60 --file items.json
|
|
284
|
+
disruptapi watchlists remove-item wl_3f9a1c2b7d4e5f60 BKG-7QX2LM
|
|
285
|
+
disruptapi webhooks list --format table
|
|
286
|
+
disruptapi webhooks create --url https://example.com/webhooks/disrupt --watchlists '*' --min-severity moderate
|
|
287
|
+
disruptapi webhooks delete whk_8d2c41f0a6b9e357
|
|
288
|
+
disruptapi webhooks verify --secret whsec_... --signature 't=1795000000,v1=93028a...' --body-file body.json
|
|
289
|
+
disruptapi usage --days 7 --format table
|
|
290
|
+
disruptapi plans --format table
|
|
291
|
+
disruptapi --version
|
|
292
|
+
```
|
|
293
|
+
|
|
294
|
+
- Output is JSON (pretty-printed, UTF-8) unless you pass `--format table`, which the list-like commands support (`signals list`, `exposure check`, `providers list`, `watchlists list|get|exposure`, `webhooks list`, `usage`, `plans`).
|
|
295
|
+
- Item files hold a JSON list of items, or `{"items": [...], "min_severity": "..."}`. `-` reads stdin.
|
|
296
|
+
- `signals list --all` follows every page; records count against your daily budget.
|
|
297
|
+
- `webhooks verify` makes no API call. `--signature` takes the whole `Disrupt-Signature` header, or just the `v1` digest together with `--timestamp`. The secret can come from `DISRUPT_WEBHOOK_SECRET`. Deliveries older than `--tolerance` (300 s) fail unless you pass `--ignore-age`.
|
|
298
|
+
- `--api-key`, `--base-url`, `--timeout` and `--max-retries` work before or after the command. `DISRUPT_BASE_URL` also sets the base URL.
|
|
299
|
+
- Also available: `watchlists delete`, `watchlists exposure`, `me`.
|
|
300
|
+
|
|
301
|
+
| Exit code | Meaning |
|
|
302
|
+
| --- | --- |
|
|
303
|
+
| 0 | Success (for `webhooks verify`: the signature is valid) |
|
|
304
|
+
| 1 | API error, connection error, or an invalid signature. stderr shows `error.code: message` |
|
|
305
|
+
| 2 | Usage error: bad arguments, an unreadable file or invalid JSON |
|
|
306
|
+
| 3 | Authentication or permission error: no key, `missing_api_key`, `invalid_api_key` (401) or `plan_required` (403) |
|
|
307
|
+
|
|
308
|
+
## Configuration
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
client = DisruptAPI(
|
|
312
|
+
"dk_live_...", # default: DISRUPT_API_KEY
|
|
313
|
+
base_url="https://app.disruptapi.com", # default: DISRUPT_BASE_URL, then production
|
|
314
|
+
timeout=30.0, # seconds per attempt
|
|
315
|
+
max_retries=3,
|
|
316
|
+
initial_retry_delay=0.5, # seconds, doubles per retry, with jitter
|
|
317
|
+
max_retry_delay=8.0,
|
|
318
|
+
max_retry_after=60.0,
|
|
319
|
+
default_headers={"X-Trace": "abc"},
|
|
320
|
+
http_client=httpx.Client(proxy="http://proxy:8080"), # or transport=...
|
|
321
|
+
)
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Use the client as a context manager (`with DisruptAPI() as client:`) or call `close()` to release connections. Requests send `User-Agent: disruptapi-python/<version>` and `Authorization: Bearer <key>`.
|
|
325
|
+
|
|
326
|
+
## Examples
|
|
327
|
+
|
|
328
|
+
See [`examples/`](./examples): exposure check, syncing signals with `since`, the async client, and a FastAPI webhook receiver.
|
|
329
|
+
|
|
330
|
+
## Development
|
|
331
|
+
|
|
332
|
+
```bash
|
|
333
|
+
python3 -m venv .venv
|
|
334
|
+
.venv/bin/pip install -e '.[dev]'
|
|
335
|
+
.venv/bin/pytest # httpx MockTransport and respx
|
|
336
|
+
.venv/bin/mypy # strict
|
|
337
|
+
.venv/bin/pip install build && .venv/bin/python -m build # dist/*.whl, dist/*.tar.gz
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
## License
|
|
341
|
+
|
|
342
|
+
MIT, copyright Digital Envision LLC.
|