agilis-ads 0.1.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.
- agilis_ads-0.1.0/.gitignore +24 -0
- agilis_ads-0.1.0/CHANGELOG.md +14 -0
- agilis_ads-0.1.0/LICENSE +21 -0
- agilis_ads-0.1.0/PKG-INFO +341 -0
- agilis_ads-0.1.0/README.md +308 -0
- agilis_ads-0.1.0/SECURITY.md +19 -0
- agilis_ads-0.1.0/examples/sample-offer.png +0 -0
- agilis_ads-0.1.0/examples/test_mode.py +69 -0
- agilis_ads-0.1.0/examples/webhook_receiver.py +43 -0
- agilis_ads-0.1.0/pyproject.toml +94 -0
- agilis_ads-0.1.0/src/agilis_ads/__init__.py +54 -0
- agilis_ads-0.1.0/src/agilis_ads/_client.py +357 -0
- agilis_ads-0.1.0/src/agilis_ads/_errors.py +127 -0
- agilis_ads-0.1.0/src/agilis_ads/_multipart.py +158 -0
- agilis_ads-0.1.0/src/agilis_ads/_resources.py +369 -0
- agilis_ads-0.1.0/src/agilis_ads/_response.py +60 -0
- agilis_ads-0.1.0/src/agilis_ads/_version.py +1 -0
- agilis_ads-0.1.0/src/agilis_ads/py.typed +0 -0
- agilis_ads-0.1.0/src/agilis_ads/types.py +763 -0
- agilis_ads-0.1.0/src/agilis_ads/webhooks.py +158 -0
- agilis_ads-0.1.0/tests/__init__.py +0 -0
- agilis_ads-0.1.0/tests/conftest.py +118 -0
- agilis_ads-0.1.0/tests/fixtures/webhook-signature-vectors.json +78 -0
- agilis_ads-0.1.0/tests/images.py +41 -0
- agilis_ads-0.1.0/tests/test_client.py +201 -0
- agilis_ads-0.1.0/tests/test_errors.py +162 -0
- agilis_ads-0.1.0/tests/test_images.py +33 -0
- agilis_ads-0.1.0/tests/test_integration.py +141 -0
- agilis_ads-0.1.0/tests/test_operations.py +482 -0
- agilis_ads-0.1.0/tests/test_retries.py +178 -0
- agilis_ads-0.1.0/tests/test_webhooks.py +109 -0
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Secrets: never commit API keys
|
|
2
|
+
.env
|
|
3
|
+
.env.*
|
|
4
|
+
!.env.example
|
|
5
|
+
*.pem
|
|
6
|
+
|
|
7
|
+
# Python
|
|
8
|
+
__pycache__/
|
|
9
|
+
*.py[cod]
|
|
10
|
+
*.egg-info/
|
|
11
|
+
.venv/
|
|
12
|
+
venv/
|
|
13
|
+
build/
|
|
14
|
+
dist/
|
|
15
|
+
.pytest_cache/
|
|
16
|
+
.mypy_cache/
|
|
17
|
+
.ruff_cache/
|
|
18
|
+
.coverage
|
|
19
|
+
htmlcov/
|
|
20
|
+
|
|
21
|
+
# Editors and OS
|
|
22
|
+
.idea/
|
|
23
|
+
.vscode/
|
|
24
|
+
.DS_Store
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this package are listed here. It follows [Semantic Versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
## 0.1.0 (22 November 2026)
|
|
6
|
+
|
|
7
|
+
- `events.preview` takes every field of `events.publish` (all optional), so a preview can be checked exactly as the publish would be.
|
|
8
|
+
- First release: the `Agilis` client with every operation of the Agilis Advertising REST API (categories, interests, Offers, Events, webhook endpoints and webhook events).
|
|
9
|
+
- Typed parameters and responses (`agilis_ads.types`).
|
|
10
|
+
- Automatic `Idempotency-Key` on Offer creation and Event publishing, kept across retries.
|
|
11
|
+
- Retries with `Retry-After` and exponential backoff.
|
|
12
|
+
- Cursor pagination helpers (`iter_all`).
|
|
13
|
+
- Webhook signature verification (`construct_event`).
|
|
14
|
+
- `events.edit` is always sent as a multipart `POST` with `X-HTTP-Method-Override: PATCH`.
|
agilis_ads-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agilis Dating Ltd
|
|
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,341 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: agilis-ads
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: The official Python SDK for the Agilis Advertising REST API: Offers, Events and webhooks.
|
|
5
|
+
Project-URL: Homepage, https://github.com/agilisdating/agilis-ads-python
|
|
6
|
+
Project-URL: Documentation, https://www.agilis.dating/api-doc
|
|
7
|
+
Project-URL: Source, https://github.com/agilisdating/agilis-ads-python
|
|
8
|
+
Project-URL: Issues, https://github.com/agilisdating/agilis-ads-python/issues
|
|
9
|
+
Project-URL: Changelog, https://github.com/agilisdating/agilis-ads-python/blob/main/CHANGELOG.md
|
|
10
|
+
Author-email: Agilis Dating Ltd <help@agilis.dating>
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
13
|
+
Keywords: advertising,agilis,api,events,offers,sdk,webhooks
|
|
14
|
+
Classifier: Development Status :: 4 - Beta
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Operating System :: OS Independent
|
|
17
|
+
Classifier: Programming Language :: Python :: 3
|
|
18
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
24
|
+
Classifier: Typing :: Typed
|
|
25
|
+
Requires-Python: >=3.10
|
|
26
|
+
Requires-Dist: httpx<1,>=0.25
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: build>=1.2; extra == 'dev'
|
|
29
|
+
Requires-Dist: mypy>=1.10; extra == 'dev'
|
|
30
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
31
|
+
Requires-Dist: ruff>=0.6; extra == 'dev'
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# Agilis Advertising API: Python SDK
|
|
35
|
+
|
|
36
|
+
The official Python library for the [Agilis Advertising REST API](https://www.agilis.dating/api-doc): create and manage Offers and Events, read their results, and receive webhooks.
|
|
37
|
+
|
|
38
|
+
- Python 3.10 or newer, with [`httpx`](https://www.python-httpx.org/) as the only dependency
|
|
39
|
+
- Fully typed (`TypedDict` parameters and responses, `py.typed`)
|
|
40
|
+
- Retries with backoff, idempotency keys, cursor pagination and webhook signature checks built in
|
|
41
|
+
|
|
42
|
+
## Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install agilis-ads
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Quick start (Test mode)
|
|
49
|
+
|
|
50
|
+
Create an API key in the Ad Portal under **Manage API Keys**, with the Ad Portal in Test mode. A `pk_test_` key works in Test mode only: members never see what you create and nothing is charged.
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
export AGILIS_API_KEY=pk_test_...
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from agilis_ads import Agilis
|
|
58
|
+
|
|
59
|
+
client = Agilis() # reads AGILIS_API_KEY
|
|
60
|
+
|
|
61
|
+
print(client.mode) # "test" or "live", decided by the key
|
|
62
|
+
|
|
63
|
+
for category in client.categories.list()["categories"]:
|
|
64
|
+
print(category["category_id"], category["category_name"])
|
|
65
|
+
|
|
66
|
+
estimate = client.offers.estimate_price(
|
|
67
|
+
{
|
|
68
|
+
"start_datetime": "2027-07-10T08:00:00Z",
|
|
69
|
+
"end_datetime": "2027-07-12T20:30:00Z",
|
|
70
|
+
"location": [51.5074, -0.1278],
|
|
71
|
+
"radius": 10,
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
print(estimate["estimated_cost"], estimate["currency"])
|
|
75
|
+
|
|
76
|
+
# dry_run=True checks everything and says what it would cost, without creating anything.
|
|
77
|
+
result = client.offers.create(
|
|
78
|
+
{
|
|
79
|
+
"category_id": 1,
|
|
80
|
+
"title": "20% off summer sale",
|
|
81
|
+
"description": "Discount valid through July",
|
|
82
|
+
"push_notification_title": "20% off this summer",
|
|
83
|
+
"push_notification_message": "Show this at the till for 20% off everything.",
|
|
84
|
+
"url": "https://example.com/offer",
|
|
85
|
+
"image": "offer.png",
|
|
86
|
+
"location": [51.5074, -0.1278],
|
|
87
|
+
"radius": 10,
|
|
88
|
+
"start_datetime": "2027-07-10T08:00:00Z",
|
|
89
|
+
"expiry_datetime": "2027-07-12T20:30:00Z",
|
|
90
|
+
"payment_method": "B",
|
|
91
|
+
},
|
|
92
|
+
dry_run=True,
|
|
93
|
+
)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
Operations that take an `environment` (`TEST` or `LIVE`) fill it in from the key when you leave it out. To make sure a script never runs against Live:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
if client.mode != "test":
|
|
100
|
+
raise SystemExit("Use a Test-mode key")
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
More in [`examples/`](examples): a Test-mode tour and a webhook receiver.
|
|
104
|
+
|
|
105
|
+
## Client options
|
|
106
|
+
|
|
107
|
+
```python
|
|
108
|
+
import httpx
|
|
109
|
+
from agilis_ads import Agilis
|
|
110
|
+
|
|
111
|
+
client = Agilis(
|
|
112
|
+
api_key="pk_test_...", # default: the AGILIS_API_KEY environment variable
|
|
113
|
+
base_url="https://ads.agilis.dating", # the default
|
|
114
|
+
timeout=60.0, # seconds per request; the default
|
|
115
|
+
max_retries=2, # the default
|
|
116
|
+
http_client=httpx.Client(), # optional: your own client (proxies, transports)
|
|
117
|
+
)
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
| Attribute | Meaning |
|
|
121
|
+
|---|---|
|
|
122
|
+
| `client.mode` | `"test"` for a `pk_test_` key, `"live"` for a `pk_live_` key |
|
|
123
|
+
| `client.environment` | `"TEST"` or `"LIVE"`, as sent to the API |
|
|
124
|
+
| `client.is_live` | `True` for a Live key |
|
|
125
|
+
|
|
126
|
+
Use the client as a context manager (`with Agilis() as client:`), or call `client.close()` when done. A client you pass in as `http_client` is never closed by the SDK.
|
|
127
|
+
|
|
128
|
+
The key is sent in the `x-api-key` header only. It is never logged, never part of a URL, and never included in an error message.
|
|
129
|
+
|
|
130
|
+
## Resources and methods
|
|
131
|
+
|
|
132
|
+
Each method takes path parameters as arguments and everything else as one dict, with the API's own field names. Responses are the parsed JSON as a `dict`, with every field kept (including any the API adds later).
|
|
133
|
+
|
|
134
|
+
| Method | HTTP |
|
|
135
|
+
|---|---|
|
|
136
|
+
| `client.categories.list()` | `GET /categories` |
|
|
137
|
+
| `client.interests.list()` | `GET /interests` |
|
|
138
|
+
| `client.offers.estimate_price(params)` | `POST /offers/price-estimate` |
|
|
139
|
+
| `client.offers.create(params, *, idempotency_key=None, dry_run=False)` | `POST /offers` |
|
|
140
|
+
| `client.offers.list(params=None)` | `GET /offers` |
|
|
141
|
+
| `client.offers.iter_all(params=None)` | every page of `GET /offers` |
|
|
142
|
+
| `client.offers.pause(offer_id)` | `POST /offers/{offer_id}/pause` |
|
|
143
|
+
| `client.offers.resume(offer_id)` | `POST /offers/{offer_id}/resume` |
|
|
144
|
+
| `client.offers.analytics(offer_id, {"interval": "7d"})` | `GET /offers/{offer_id}/analytics` |
|
|
145
|
+
| `client.offers.delete(offer_id, *, dry_run=False)` | `DELETE /offers/{offer_id}` |
|
|
146
|
+
| `client.event_categories.list()` | `GET /event-categories` |
|
|
147
|
+
| `client.event_currencies.list()` | `GET /event-currencies` |
|
|
148
|
+
| `client.event_config.get()` | `GET /event-config` |
|
|
149
|
+
| `client.events.preview(params=None)` | `POST /events/preview` |
|
|
150
|
+
| `client.events.publish(params, *, idempotency_key=None, dry_run=False)` | `POST /events` |
|
|
151
|
+
| `client.events.list(params=None)` | `GET /events` |
|
|
152
|
+
| `client.events.iter_all(params=None)` | every page of `GET /events` |
|
|
153
|
+
| `client.events.get(event_id)` | `GET /events/{event_id}` |
|
|
154
|
+
| `client.events.edit(event_id, params)` | `PATCH /events/{event_id}` (sent as `POST` with `X-HTTP-Method-Override: PATCH`) |
|
|
155
|
+
| `client.events.delete(event_id, *, dry_run=False)` | `DELETE /events/{event_id}` |
|
|
156
|
+
| `client.events.reschedule(event_id, params)` | `POST /events/{event_id}/reschedule` |
|
|
157
|
+
| `client.events.cancel(event_id, *, dry_run=False)` | `POST /events/{event_id}/cancel` |
|
|
158
|
+
| `client.events.analytics(event_id, {"interval": "all"})` | `GET /events/{event_id}/analytics` |
|
|
159
|
+
| `client.webhook_endpoints.list()` | `GET /webhook-endpoints` |
|
|
160
|
+
| `client.webhook_endpoints.create(params)` | `POST /webhook-endpoints` |
|
|
161
|
+
| `client.webhook_endpoints.get(endpoint_id)` | `GET /webhook-endpoints/{endpoint_id}` |
|
|
162
|
+
| `client.webhook_endpoints.update(endpoint_id, params)` | `PATCH /webhook-endpoints/{endpoint_id}` |
|
|
163
|
+
| `client.webhook_endpoints.delete(endpoint_id)` | `DELETE /webhook-endpoints/{endpoint_id}` |
|
|
164
|
+
| `client.webhook_endpoints.roll_secret(endpoint_id, params=None)` | `POST /webhook-endpoints/{endpoint_id}/roll-secret` |
|
|
165
|
+
| `client.webhook_endpoints.send_test(endpoint_id, params=None)` | `POST /webhook-endpoints/{endpoint_id}/test` |
|
|
166
|
+
| `client.webhook_events.list(params=None)` | `GET /webhook-events` |
|
|
167
|
+
| `client.webhook_events.iter_all(params=None)` | every page of `GET /webhook-events` |
|
|
168
|
+
| `client.webhook_events.get(webhook_event_id)` | `GET /webhook-events/{webhook_event_id}` |
|
|
169
|
+
| `client.webhook_events.resend(webhook_event_id, params=None)` | `POST /webhook-events/{webhook_event_id}/resend` |
|
|
170
|
+
| `client.webhooks.construct_event(payload, header, secret, tolerance=300)` | verifies a received webhook; no request |
|
|
171
|
+
|
|
172
|
+
The parameter and response types are in `agilis_ads.types` (for example `OfferCreateParams` and `OfferPage`). The fields and their rules are described in the [API reference](https://www.agilis.dating/api-doc).
|
|
173
|
+
|
|
174
|
+
### Images and multipart fields
|
|
175
|
+
|
|
176
|
+
`offers.create`, `events.preview`, `events.publish` and `events.edit` are sent as `multipart/form-data`. Pass the image as a file path, `bytes`, a binary stream, or a `(file_name, bytes_or_stream)` tuple; the content type is detected from the file name.
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
client.events.publish(
|
|
180
|
+
{
|
|
181
|
+
"title": "Beginners Cuban Salsa: Friday Social",
|
|
182
|
+
"description": "Drop-in class for absolute beginners, followed by a social.",
|
|
183
|
+
"venue_name": "Bar Salsa",
|
|
184
|
+
"address": "96 Charing Cross Road",
|
|
185
|
+
"city": "London",
|
|
186
|
+
"country_code": "GB",
|
|
187
|
+
"location": [51.5133, -0.1300],
|
|
188
|
+
"starts_at": "2027-10-16T18:30:00Z",
|
|
189
|
+
"ends_at": "2027-10-16T23:00:00Z",
|
|
190
|
+
"url": "https://example.com/classes/friday",
|
|
191
|
+
"image": ("salsa.jpg", open("salsa.jpg", "rb")),
|
|
192
|
+
"crop": {"x": 0, "y": 120, "w": 1080},
|
|
193
|
+
"payment_method": "B",
|
|
194
|
+
}
|
|
195
|
+
)
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
Lists are sent as repeated fields (`location[]`, `gender[]`, `interests[]`) and `crop` as `crop[x]`, `crop[y]` and `crop[w]`. `events.edit` is always sent as a multipart `POST` with `X-HTTP-Method-Override: PATCH`, with or without a new image, so its fields always reach the API.
|
|
199
|
+
|
|
200
|
+
### Status and headers
|
|
201
|
+
|
|
202
|
+
Each result remembers the HTTP response it came from:
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
import agilis_ads
|
|
206
|
+
|
|
207
|
+
page = client.offers.list()
|
|
208
|
+
meta = agilis_ads.response_meta(page)
|
|
209
|
+
print(meta.status_code, meta.rate_limit_remaining, meta.headers.get("X-RateLimit-Limit"))
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
## Pagination
|
|
213
|
+
|
|
214
|
+
`offers.list`, `events.list` and `webhook_events.list` return one page: `data` and `pagination` (`has_more`, `next_cursor`). Pass `next_cursor` back as `cursor` for the next page, or let `iter_all` follow the pages for you:
|
|
215
|
+
|
|
216
|
+
```python
|
|
217
|
+
for offer in client.offers.iter_all({"offer_status": "active", "limit": 100}):
|
|
218
|
+
print(offer["offer_id"], offer["title"])
|
|
219
|
+
```
|
|
220
|
+
|
|
221
|
+
`iter_all` is lazy: it fetches the next page only when the loop reaches it.
|
|
222
|
+
|
|
223
|
+
## Idempotency and retries
|
|
224
|
+
|
|
225
|
+
`offers.create` and `events.publish` always send an `Idempotency-Key`: yours if you pass `idempotency_key=`, otherwise a new UUID for that call. The same key is reused on every retry of the call, so a retry never creates a second Offer or Event. Store your own key if you may retry later, for example after a restart.
|
|
226
|
+
|
|
227
|
+
A request is retried up to `max_retries` times (default 2) when:
|
|
228
|
+
|
|
229
|
+
- the connection fails or times out;
|
|
230
|
+
- the API answers HTTP 429;
|
|
231
|
+
- the API answers HTTP 500, 502, 503 or 504 to a `GET` or `DELETE`, or to a `POST` that carries an `Idempotency-Key`.
|
|
232
|
+
|
|
233
|
+
Other `POST` and `PATCH` requests are not retried after a 5xx, because the change may already have been made. The wait is the API's `Retry-After` when it sends one; otherwise 0.5 seconds, doubling each time, with ±25% jitter, at most 8 seconds.
|
|
234
|
+
|
|
235
|
+
## Errors
|
|
236
|
+
|
|
237
|
+
Every error is an `agilis_ads.AgilisError` with `http_status`, `return_code`, `message`, `body` (the parsed response) and `headers`.
|
|
238
|
+
|
|
239
|
+
| Error | When |
|
|
240
|
+
|---|---|
|
|
241
|
+
| `AuthenticationError` | HTTP 401 (or no API key) |
|
|
242
|
+
| `agilis_ads.PermissionError` | HTTP 403 |
|
|
243
|
+
| `NotFoundError` | HTTP 404 |
|
|
244
|
+
| `ConflictError` | HTTP 409 |
|
|
245
|
+
| `InvalidRequestError` | HTTP 400 or 422, or HTTP 200 with a `return_code` starting with `E` |
|
|
246
|
+
| `RateLimitError` | HTTP 429; `retry_after` holds the seconds to wait |
|
|
247
|
+
| `ApiError` | HTTP 5xx and anything else unexpected |
|
|
248
|
+
| `agilis_ads.ConnectionError` | no response: the connection failed or timed out |
|
|
249
|
+
| `SignatureVerificationError` | a webhook could not be verified |
|
|
250
|
+
|
|
251
|
+
`PermissionError` and `ConnectionError` share their names with Python built-ins, so use them through the package (`agilis_ads.PermissionError`) rather than importing them by name.
|
|
252
|
+
|
|
253
|
+
Some refusals arrive as HTTP 200 with an `E` code, for example `E3` when the balance is too low. They raise `InvalidRequestError` with `http_status` 200. Return codes are scoped to each endpoint; the [API reference](https://www.agilis.dating/api-doc) lists them.
|
|
254
|
+
|
|
255
|
+
```python
|
|
256
|
+
import agilis_ads
|
|
257
|
+
|
|
258
|
+
try:
|
|
259
|
+
client.offers.create(params)
|
|
260
|
+
except agilis_ads.InvalidRequestError as error:
|
|
261
|
+
print("Refused:", error.return_code, error.message)
|
|
262
|
+
except agilis_ads.RateLimitError as error:
|
|
263
|
+
print("Slow down for", error.retry_after, "seconds")
|
|
264
|
+
except agilis_ads.AgilisError as error:
|
|
265
|
+
print("Failed:", error)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## Webhooks
|
|
269
|
+
|
|
270
|
+
Create an endpoint and store its `secret` (it is shown only once):
|
|
271
|
+
|
|
272
|
+
```python
|
|
273
|
+
endpoint = client.webhook_endpoints.create(
|
|
274
|
+
{
|
|
275
|
+
"url": "https://example.com/agilis/webhooks",
|
|
276
|
+
"enabled_events": ["offer.started", "event.published"],
|
|
277
|
+
}
|
|
278
|
+
)
|
|
279
|
+
secret = endpoint["secret"]
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
Every webhook carries an `Agilis-Signature` header. `construct_event` checks it against the raw body, refuses a timestamp more than 300 seconds away (change it with `tolerance`), and returns the parsed event. It needs no API key:
|
|
283
|
+
|
|
284
|
+
```python
|
|
285
|
+
import os
|
|
286
|
+
|
|
287
|
+
from flask import Flask, request
|
|
288
|
+
|
|
289
|
+
from agilis_ads import SignatureVerificationError, construct_event
|
|
290
|
+
|
|
291
|
+
app = Flask(__name__)
|
|
292
|
+
SECRET = os.environ["AGILIS_WEBHOOK_SECRET"]
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
@app.post("/agilis/webhooks")
|
|
296
|
+
def agilis_webhook():
|
|
297
|
+
try:
|
|
298
|
+
event = construct_event(
|
|
299
|
+
request.get_data(), # the raw body, exactly as received
|
|
300
|
+
request.headers.get("Agilis-Signature", ""),
|
|
301
|
+
SECRET,
|
|
302
|
+
)
|
|
303
|
+
except SignatureVerificationError:
|
|
304
|
+
return "", 400
|
|
305
|
+
|
|
306
|
+
if event["type"] == "offer.started":
|
|
307
|
+
print("Offer started:", event["data"]["object"]["offer_id"])
|
|
308
|
+
return "", 200
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
Answer with a 2xx quickly and do slow work afterwards. An event can arrive more than once and out of order: use `event["id"]` to ignore repeats and `event["created"]` to order them. While a secret is being rolled, the header carries two signatures and either one is accepted.
|
|
312
|
+
|
|
313
|
+
[`examples/webhook_receiver.py`](examples/webhook_receiver.py) is the same receiver using only the standard library.
|
|
314
|
+
|
|
315
|
+
## Development
|
|
316
|
+
|
|
317
|
+
```bash
|
|
318
|
+
python -m venv .venv
|
|
319
|
+
. .venv/bin/activate
|
|
320
|
+
pip install -e ".[dev]"
|
|
321
|
+
ruff check . && ruff format --check .
|
|
322
|
+
mypy
|
|
323
|
+
pytest
|
|
324
|
+
```
|
|
325
|
+
|
|
326
|
+
`pytest -m integration` runs a short read-only check against the API in Test mode; it needs `AGILIS_TEST_API_KEY` set to a `pk_test_` key and refuses any other.
|
|
327
|
+
|
|
328
|
+
## Links
|
|
329
|
+
|
|
330
|
+
- [API reference](https://www.agilis.dating/api-doc) ([Markdown](https://www.agilis.dating/api-doc.md))
|
|
331
|
+
- [OpenAPI document](https://www.agilis.dating/openapi.json)
|
|
332
|
+
- [AsyncAPI document (webhooks)](https://www.agilis.dating/asyncapi.json)
|
|
333
|
+
- [agilis-ads-api](https://github.com/agilisdating/agilis-ads-api): the API's public repository, with examples and a Postman collection
|
|
334
|
+
|
|
335
|
+
## Security
|
|
336
|
+
|
|
337
|
+
See [SECURITY.md](SECURITY.md) to report a problem or if an API key has been exposed.
|
|
338
|
+
|
|
339
|
+
## License
|
|
340
|
+
|
|
341
|
+
MIT. See [LICENSE](LICENSE).
|