dq-export-connector 0.1.0.dev34__tar.gz → 0.1.0.dev35__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/PKG-INFO +81 -2
  2. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/README.md +78 -1
  3. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/export_connector_credentials.py +30 -2
  4. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/export_receipt.py +76 -4
  5. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/export_worker_config.py +95 -6
  6. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/audience/classify.py +3 -0
  7. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/audience/http.py +36 -1
  8. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/audience/sync.py +36 -6
  9. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/errors.py +9 -0
  10. dq_export_connector-0.1.0.dev35/dq_export_connector/models/segment.py +8 -0
  11. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/models/snap.py +1 -6
  12. dq_export_connector-0.1.0.dev35/dq_export_connector/models/tiktok.py +43 -0
  13. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/oauth.py +8 -7
  14. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/redact.py +1 -1
  15. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/runner.py +54 -11
  16. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/snap/destination.py +1 -0
  17. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/snap/export.py +3 -3
  18. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/snap/session.py +5 -5
  19. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/snap/validate.py +3 -3
  20. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/accounts.py +27 -0
  21. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/client.py +251 -0
  22. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/destination.py +247 -0
  23. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/export.py +128 -0
  24. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/session.py +88 -0
  25. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/validate.py +38 -0
  26. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/pyproject.toml +6 -3
  27. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/scripts/_local.py +42 -11
  28. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/scripts/codegen-models.sh +1 -1
  29. dq_export_connector-0.1.0.dev35/scripts/run_local.py +96 -0
  30. dq_export_connector-0.1.0.dev35/tests/fakes/__init__.py +0 -0
  31. dq_export_connector-0.1.0.dev35/tests/fakes/tiktok_fake.py +322 -0
  32. dq_export_connector-0.1.0.dev35/tests/test_audience_http.py +104 -0
  33. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_http_and_oauth.py +11 -6
  34. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_live_snap.py +5 -5
  35. dq_export_connector-0.1.0.dev35/tests/test_live_tiktok.py +111 -0
  36. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_oauth.py +18 -7
  37. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_redact.py +11 -1
  38. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_snap_runner.py +8 -1
  39. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_sync_input.py +36 -4
  40. dq_export_connector-0.1.0.dev35/tests/test_tiktok_runner.py +579 -0
  41. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/uv.lock +8 -4
  42. dq_export_connector-0.1.0.dev34/scripts/run_local.py +0 -54
  43. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/.gitignore +0 -0
  44. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/.python-version +0 -0
  45. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/__init__.py +0 -0
  46. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/__init__.py +0 -0
  47. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/application_error.py +0 -0
  48. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/application_warning.py +0 -0
  49. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/destination_account_listing.py +0 -0
  50. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/audience/__init__.py +0 -0
  51. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/audience/identifiers.py +0 -0
  52. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/audience/inputs.py +0 -0
  53. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/__init__.py +0 -0
  54. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/export.py +0 -0
  55. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/paths.py +0 -0
  56. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/store.py +0 -0
  57. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/upload.py +0 -0
  58. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/validate.py +0 -0
  59. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/datasets.py +0 -0
  60. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/log.py +0 -0
  61. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/models/__init__.py +0 -0
  62. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/models/bucket.py +0 -0
  63. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/outcome.py +0 -0
  64. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/snap/__init__.py +0 -0
  65. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/snap/accounts.py +0 -0
  66. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/dq_export_connector/snap/client.py +0 -0
  67. {dq_export_connector-0.1.0.dev34/tests → dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok}/__init__.py +0 -0
  68. {dq_export_connector-0.1.0.dev34/tests/fakes → dq_export_connector-0.1.0.dev35/tests}/__init__.py +0 -0
  69. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/conftest.py +0 -0
  70. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/fakes/snap_fake.py +0 -0
  71. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_identifiers_and_inputs.py +0 -0
  72. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_live_buckets.py +0 -0
  73. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_models_are_current.py +0 -0
  74. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_paths.py +0 -0
  75. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_runner.py +0 -0
  76. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_scripts.py +0 -0
  77. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_store.py +0 -0
  78. {dq_export_connector-0.1.0.dev34 → dq_export_connector-0.1.0.dev35}/tests/test_upload.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: dq-export-connector
3
- Version: 0.1.0.dev34
3
+ Version: 0.1.0.dev35
4
4
  Summary: Worker logic for the Export Connector DCR.
5
5
  Author: Decentriq
6
6
  Requires-Python: >=3.12
@@ -9,6 +9,8 @@ Provides-Extra: bucket
9
9
  Requires-Dist: obstore<0.12,>=0.11.1; extra == 'bucket'
10
10
  Provides-Extra: snap
11
11
  Requires-Dist: requests<3,>=2.32; extra == 'snap'
12
+ Provides-Extra: tiktok
13
+ Requires-Dist: requests<3,>=2.32; extra == 'tiktok'
12
14
  Description-Content-Type: text/markdown
13
15
 
14
16
  # dq-export-connector
@@ -21,6 +23,8 @@ its dependencies.
21
23
  through [`obstore`](https://developmentseed.org/obstore/).
22
24
  - The **snap** kind fills one Snap Segment per Input with hashed identifiers,
23
25
  through Snap's Marketing API.
26
+ - The **tiktok** kind fills one TikTok Segment (a Custom Audience in TikTok's
27
+ API) per Input with hashed identifiers, through TikTok's API for Business.
24
28
 
25
29
  ddc pins this package by hash in a fully locked `requirements.txt` and runs
26
30
  `dq_export_connector.runner.main` in the task's Python environment. It is
@@ -54,6 +58,7 @@ published to PyPI, since the compiled graph installs it from there.
54
58
  allowlist with retries by idempotency class, OAuth, identifier
55
59
  normalisation, and the per-Input sync flow.
56
60
  - `dq_export_connector.snap`: the snap kind.
61
+ - `dq_export_connector.tiktok`: the tiktok kind.
57
62
  - `dq_export_connector.models`: the types the worker reads and writes, those
58
63
  of every Connector Kind directly in it and a kind's own in its submodule,
59
64
  such as `models.bucket`. They are pydantic models generated in
@@ -64,7 +69,7 @@ published to PyPI, since the compiled graph installs it from there.
64
69
  while they are stale.
65
70
 
66
71
  ```sh
67
- uv run pytest # unit tests, against obstore's in-memory store and a fake Snap API
72
+ uv run pytest # unit tests, against obstore's in-memory store and fake Snap and TikTok APIs
68
73
  uv run pytest -m live # against real buckets and Snap, credentials from the environment
69
74
  uv run mypy dq_export_connector tests scripts
70
75
  ```
@@ -147,6 +152,80 @@ The local files are gitignored in this directory:
147
152
  DQ_EXPORT_LIVE_SNAP_DIR=$PWD uv run pytest -m live -s tests/test_live_snap.py
148
153
  ```
149
154
 
155
+ ## TikTok
156
+
157
+ A TikTok connector's config names the advertiser (set after consent, from a
158
+ `ValidateConnection` in `list` mode) and, per Input, a Segment, the identifier
159
+ type its lines hold, a Merge Mode, and `replaceExistingSegment`. ddc validates
160
+ it. The credentials are the OAuth Grant of the TikTok user who consented, and
161
+ its Access Token is long-lived: TikTok neither expires nor refreshes it, so
162
+ a refused token ends the run with `UNAUTHENTICATED`, and the owner connects
163
+ again. Decentriq's app is the `export_connector_tiktok_app` platform secret
164
+ (`{"clientId", "clientSecret"}`), mounted at `/input/oauth_app.json`. Every
165
+ call carries the token as the `Access-Token` header, and the client waits one
166
+ second between calls to the same Segment endpoint. `ValidateConnection` in
167
+ `list` mode lists the advertisers the grant reaches; in `check` mode it checks
168
+ the picked one is among them and that its Segments can be listed.
169
+
170
+ A run looks each Segment up by name in the advertiser, listing them once
171
+ before the first Input, and creates it at the end of the Input when there is
172
+ none. TikTok has no description to mark a Segment with, so the run can tell
173
+ only that the advertiser created it through the API and every change to it
174
+ went through the API. A Segment that fails this, such as one edited in Ads
175
+ Manager, is written with a `SEGMENT_NOT_MANAGED` warning.
176
+
177
+ Each Input's values are uploaded as files first, which changes no Segment. The
178
+ run then makes one create, or one update with `APPEND` or `REPLACE`, over all
179
+ the files, because TikTok allows one REPLACE a day per Segment. Before
180
+ uploading, a run waits up to 10 minutes for TikTok to make the Segment
181
+ editable again, then fails with `SEGMENT_BUSY`.
182
+
183
+ REPLACE on a Segment the run didn't create needs `replaceExistingSegment: true`
184
+ on that Input, which confirms only this connector writes to it; without it the
185
+ run fails with `REPLACE_NOT_CONFIRMED` before any upload. TikTok can't tell
186
+ which API client made a Segment, so the confirmation is the only guard.
187
+ A Segment under 1,000 matched users can't be updated by TikTok, which gives a
188
+ `SEGMENT_TOO_SMALL` warning. An empty Input gives an `EMPTY_DATASET` warning
189
+ and leaves its Segment as it was, in both modes.
190
+
191
+ The identifiers are normalised for TikTok's `calculate_type`s:
192
+
193
+ | Type | Normalisation before SHA-256 | TikTok `calculate_type` |
194
+ |---|---|---|
195
+ | `EMAIL` | trim, lowercase | `EMAIL_SHA256` |
196
+ | `PHONE` | as for Snap, then E.164 with the `+` (`+442071838750`) | `PHONE_SHA256` |
197
+ | `IDFA`, `GAID` | UUID form with hyphens, lowercased | `IDFA_SHA256`, `GAID_SHA256` |
198
+
199
+ ### Testing against TikTok
200
+
201
+ The local files are gitignored in this directory. Give them to `run_local.py`
202
+ as paths, as for Snap:
203
+
204
+ - `platform_secrets.json`: `{"export_connector_tiktok_app": {"clientId": …, "clientSecret": …}}`, the dev app.
205
+ - `credentials.json`: `{"type": "tiktok", "credentials": {"accessToken": …}}`, the access token a consent on a dev environment, or a TikTok sandbox, issued. The worker wraps it in an OAuth Grant whose `clientId` is the app's `clientId`, so the two always match.
206
+ - `config.json`: `{"connector": {"type": "tiktok", "advertiserId": …, "inputs": [{"leafId": "segA", "segmentName": …, "identifierType": "EMAIL", "mergeMode": "ADD", "retentionDays": 30}]}}`.
207
+
208
+ 1. Run:
209
+
210
+ ```sh
211
+ mkdir -p local/inputs && printf 'a@example.com\nb@example.com\n' > local/inputs/segA
212
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
213
+ --platform-secrets platform_secrets.json --mode validate
214
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
215
+ --platform-secrets platform_secrets.json
216
+ ```
217
+
218
+ `--tiktok-sandbox` sends TikTok's calls to `sandbox-ads.tiktok.com` instead
219
+ of production, for a token the sandbox issued. TikTok retires the sandbox on
220
+ 2026-12-01.
221
+ 2. Live tests, against a test advertiser in production:
222
+
223
+ ```sh
224
+ DQ_EXPORT_LIVE_TIKTOK_DIR=$PWD uv run pytest -m live -s tests/test_live_tiktok.py
225
+ ```
226
+
227
+ The directory holds the three files above, with the same names.
228
+
150
229
  ## Releasing
151
230
 
152
231
  Bump the version with `uv version <version> --no-sync`, which also updates
@@ -8,6 +8,8 @@ its dependencies.
8
8
  through [`obstore`](https://developmentseed.org/obstore/).
9
9
  - The **snap** kind fills one Snap Segment per Input with hashed identifiers,
10
10
  through Snap's Marketing API.
11
+ - The **tiktok** kind fills one TikTok Segment (a Custom Audience in TikTok's
12
+ API) per Input with hashed identifiers, through TikTok's API for Business.
11
13
 
12
14
  ddc pins this package by hash in a fully locked `requirements.txt` and runs
13
15
  `dq_export_connector.runner.main` in the task's Python environment. It is
@@ -41,6 +43,7 @@ published to PyPI, since the compiled graph installs it from there.
41
43
  allowlist with retries by idempotency class, OAuth, identifier
42
44
  normalisation, and the per-Input sync flow.
43
45
  - `dq_export_connector.snap`: the snap kind.
46
+ - `dq_export_connector.tiktok`: the tiktok kind.
44
47
  - `dq_export_connector.models`: the types the worker reads and writes, those
45
48
  of every Connector Kind directly in it and a kind's own in its submodule,
46
49
  such as `models.bucket`. They are pydantic models generated in
@@ -51,7 +54,7 @@ published to PyPI, since the compiled graph installs it from there.
51
54
  while they are stale.
52
55
 
53
56
  ```sh
54
- uv run pytest # unit tests, against obstore's in-memory store and a fake Snap API
57
+ uv run pytest # unit tests, against obstore's in-memory store and fake Snap and TikTok APIs
55
58
  uv run pytest -m live # against real buckets and Snap, credentials from the environment
56
59
  uv run mypy dq_export_connector tests scripts
57
60
  ```
@@ -134,6 +137,80 @@ The local files are gitignored in this directory:
134
137
  DQ_EXPORT_LIVE_SNAP_DIR=$PWD uv run pytest -m live -s tests/test_live_snap.py
135
138
  ```
136
139
 
140
+ ## TikTok
141
+
142
+ A TikTok connector's config names the advertiser (set after consent, from a
143
+ `ValidateConnection` in `list` mode) and, per Input, a Segment, the identifier
144
+ type its lines hold, a Merge Mode, and `replaceExistingSegment`. ddc validates
145
+ it. The credentials are the OAuth Grant of the TikTok user who consented, and
146
+ its Access Token is long-lived: TikTok neither expires nor refreshes it, so
147
+ a refused token ends the run with `UNAUTHENTICATED`, and the owner connects
148
+ again. Decentriq's app is the `export_connector_tiktok_app` platform secret
149
+ (`{"clientId", "clientSecret"}`), mounted at `/input/oauth_app.json`. Every
150
+ call carries the token as the `Access-Token` header, and the client waits one
151
+ second between calls to the same Segment endpoint. `ValidateConnection` in
152
+ `list` mode lists the advertisers the grant reaches; in `check` mode it checks
153
+ the picked one is among them and that its Segments can be listed.
154
+
155
+ A run looks each Segment up by name in the advertiser, listing them once
156
+ before the first Input, and creates it at the end of the Input when there is
157
+ none. TikTok has no description to mark a Segment with, so the run can tell
158
+ only that the advertiser created it through the API and every change to it
159
+ went through the API. A Segment that fails this, such as one edited in Ads
160
+ Manager, is written with a `SEGMENT_NOT_MANAGED` warning.
161
+
162
+ Each Input's values are uploaded as files first, which changes no Segment. The
163
+ run then makes one create, or one update with `APPEND` or `REPLACE`, over all
164
+ the files, because TikTok allows one REPLACE a day per Segment. Before
165
+ uploading, a run waits up to 10 minutes for TikTok to make the Segment
166
+ editable again, then fails with `SEGMENT_BUSY`.
167
+
168
+ REPLACE on a Segment the run didn't create needs `replaceExistingSegment: true`
169
+ on that Input, which confirms only this connector writes to it; without it the
170
+ run fails with `REPLACE_NOT_CONFIRMED` before any upload. TikTok can't tell
171
+ which API client made a Segment, so the confirmation is the only guard.
172
+ A Segment under 1,000 matched users can't be updated by TikTok, which gives a
173
+ `SEGMENT_TOO_SMALL` warning. An empty Input gives an `EMPTY_DATASET` warning
174
+ and leaves its Segment as it was, in both modes.
175
+
176
+ The identifiers are normalised for TikTok's `calculate_type`s:
177
+
178
+ | Type | Normalisation before SHA-256 | TikTok `calculate_type` |
179
+ |---|---|---|
180
+ | `EMAIL` | trim, lowercase | `EMAIL_SHA256` |
181
+ | `PHONE` | as for Snap, then E.164 with the `+` (`+442071838750`) | `PHONE_SHA256` |
182
+ | `IDFA`, `GAID` | UUID form with hyphens, lowercased | `IDFA_SHA256`, `GAID_SHA256` |
183
+
184
+ ### Testing against TikTok
185
+
186
+ The local files are gitignored in this directory. Give them to `run_local.py`
187
+ as paths, as for Snap:
188
+
189
+ - `platform_secrets.json`: `{"export_connector_tiktok_app": {"clientId": …, "clientSecret": …}}`, the dev app.
190
+ - `credentials.json`: `{"type": "tiktok", "credentials": {"accessToken": …}}`, the access token a consent on a dev environment, or a TikTok sandbox, issued. The worker wraps it in an OAuth Grant whose `clientId` is the app's `clientId`, so the two always match.
191
+ - `config.json`: `{"connector": {"type": "tiktok", "advertiserId": …, "inputs": [{"leafId": "segA", "segmentName": …, "identifierType": "EMAIL", "mergeMode": "ADD", "retentionDays": 30}]}}`.
192
+
193
+ 1. Run:
194
+
195
+ ```sh
196
+ mkdir -p local/inputs && printf 'a@example.com\nb@example.com\n' > local/inputs/segA
197
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
198
+ --platform-secrets platform_secrets.json --mode validate
199
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
200
+ --platform-secrets platform_secrets.json
201
+ ```
202
+
203
+ `--tiktok-sandbox` sends TikTok's calls to `sandbox-ads.tiktok.com` instead
204
+ of production, for a token the sandbox issued. TikTok retires the sandbox on
205
+ 2026-12-01.
206
+ 2. Live tests, against a test advertiser in production:
207
+
208
+ ```sh
209
+ DQ_EXPORT_LIVE_TIKTOK_DIR=$PWD uv run pytest -m live -s tests/test_live_tiktok.py
210
+ ```
211
+
212
+ The directory holds the three files above, with the same names.
213
+
137
214
  ## Releasing
138
215
 
139
216
  Bump the version with `uv version <version> --no-sync`, which also updates
@@ -107,6 +107,20 @@ class SnapExportCredentials(RootModel[SnapExportCredentialsOauth]):
107
107
  ]
108
108
 
109
109
 
110
+ class TiktokExportCredentialsOauth(BaseModel):
111
+ model_config = ConfigDict(
112
+ populate_by_name=True,
113
+ )
114
+ grant: OAuthGrant
115
+ type: Literal['oauth']
116
+
117
+
118
+ class TiktokExportCredentials(RootModel[TiktokExportCredentialsOauth]):
119
+ root: Annotated[
120
+ TiktokExportCredentialsOauth, Field(description='What is sealed for TikTok.')
121
+ ]
122
+
123
+
110
124
  class ExportConnectorCredentialsBucket(BaseModel):
111
125
  model_config = ConfigDict(
112
126
  populate_by_name=True,
@@ -123,11 +137,25 @@ class ExportConnectorCredentialsSnap(BaseModel):
123
137
  type: Literal['snap']
124
138
 
125
139
 
140
+ class ExportConnectorCredentialsTiktok(BaseModel):
141
+ model_config = ConfigDict(
142
+ populate_by_name=True,
143
+ )
144
+ credentials: TiktokExportCredentials
145
+ type: Literal['tiktok']
146
+
147
+
126
148
  class ExportConnectorCredentials(
127
- RootModel[ExportConnectorCredentialsBucket | ExportConnectorCredentialsSnap]
149
+ RootModel[
150
+ ExportConnectorCredentialsBucket
151
+ | ExportConnectorCredentialsSnap
152
+ | ExportConnectorCredentialsTiktok
153
+ ]
128
154
  ):
129
155
  root: Annotated[
130
- ExportConnectorCredentialsBucket | ExportConnectorCredentialsSnap,
156
+ ExportConnectorCredentialsBucket
157
+ | ExportConnectorCredentialsSnap
158
+ | ExportConnectorCredentialsTiktok,
131
159
  Field(
132
160
  description='Keyed by Connector Kind, in step with [`ExportConnectorKind`]. The worker\nreads it at `/input/credentials.json`.',
133
161
  title='ExportConnectorCredentials',
@@ -128,6 +128,15 @@ class SnapSegmentStatus(BaseModel):
128
128
  upload_status: Annotated[str | None, Field(alias='uploadStatus')] = None
129
129
 
130
130
 
131
+ class TiktokSegmentStatus(BaseModel):
132
+ model_config = ConfigDict(
133
+ populate_by_name=True,
134
+ )
135
+ calculate_status: Annotated[str | None, Field(alias='calculateStatus')] = None
136
+ cover_num: Annotated[int | None, Field(alias='coverNum', ge=0)] = None
137
+ is_valid: Annotated[bool | None, Field(alias='isValid')] = None
138
+
139
+
131
140
  class BucketInputReceipt(BaseModel):
132
141
  model_config = ConfigDict(
133
142
  populate_by_name=True,
@@ -186,6 +195,31 @@ class SnapInputReceipt(BaseModel):
186
195
  ] = None
187
196
 
188
197
 
198
+ class TiktokInputReceipt(BaseModel):
199
+ model_config = ConfigDict(
200
+ populate_by_name=True,
201
+ )
202
+ counts: SegmentCounts
203
+ created: Annotated[bool, Field(description='Whether this run created the Segment.')]
204
+ leaf_id: Annotated[str, Field(alias='leafId')]
205
+ merge_mode: Annotated[MergeMode, Field(alias='mergeMode')]
206
+ segment_id: Annotated[
207
+ str | None,
208
+ Field(
209
+ alias='segmentId',
210
+ description='`None` until the run found or created the Segment.',
211
+ ),
212
+ ] = None
213
+ segment_name: Annotated[str, Field(alias='segmentName')]
214
+ segment_status: Annotated[
215
+ TiktokSegmentStatus | None,
216
+ Field(
217
+ alias='segmentStatus',
218
+ description='The Segment as TikTok reported it after the upload.',
219
+ ),
220
+ ] = None
221
+
222
+
189
223
  class VersionedExportReceiptV0Bucket(BaseModel):
190
224
  model_config = ConfigDict(
191
225
  populate_by_name=True,
@@ -217,11 +251,28 @@ class VersionedExportReceiptV0Snap(BaseModel):
217
251
  type: Literal['SNAP']
218
252
 
219
253
 
254
+ class VersionedExportReceiptV0Tiktok(BaseModel):
255
+ model_config = ConfigDict(
256
+ populate_by_name=True,
257
+ )
258
+ version: Literal['V0']
259
+ advertiser_id: Annotated[str, Field(alias='advertiserId')]
260
+ inputs: list[TiktokInputReceipt]
261
+ totals: SegmentTotals
262
+ type: Literal['TIKTOK']
263
+
264
+
220
265
  class ExportReceipt(
221
- RootModel[VersionedExportReceiptV0Bucket | VersionedExportReceiptV0Snap]
266
+ RootModel[
267
+ VersionedExportReceiptV0Bucket
268
+ | VersionedExportReceiptV0Snap
269
+ | VersionedExportReceiptV0Tiktok
270
+ ]
222
271
  ):
223
272
  root: Annotated[
224
- VersionedExportReceiptV0Bucket | VersionedExportReceiptV0Snap,
273
+ VersionedExportReceiptV0Bucket
274
+ | VersionedExportReceiptV0Snap
275
+ | VersionedExportReceiptV0Tiktok,
225
276
  Field(
226
277
  description='What an Export Connector Run sent and where. api-platform reads it in\nplaintext, so it holds only metadata, never dataset values. A released\nversion never changes: a change is a new version, and readers keep reading\nevery version ever written.',
227
278
  title='ExportReceipt',
@@ -257,8 +308,20 @@ class ExportReceiptV0Snap(BaseModel):
257
308
  type: Literal['SNAP']
258
309
 
259
310
 
260
- class ExportReceiptV0(RootModel[ExportReceiptV0Bucket | ExportReceiptV0Snap]):
261
- root: ExportReceiptV0Bucket | ExportReceiptV0Snap
311
+ class ExportReceiptV0Tiktok(BaseModel):
312
+ model_config = ConfigDict(
313
+ populate_by_name=True,
314
+ )
315
+ advertiser_id: Annotated[str, Field(alias='advertiserId')]
316
+ inputs: list[TiktokInputReceipt]
317
+ totals: SegmentTotals
318
+ type: Literal['TIKTOK']
319
+
320
+
321
+ class ExportReceiptV0(
322
+ RootModel[ExportReceiptV0Bucket | ExportReceiptV0Snap | ExportReceiptV0Tiktok]
323
+ ):
324
+ root: ExportReceiptV0Bucket | ExportReceiptV0Snap | ExportReceiptV0Tiktok
262
325
 
263
326
 
264
327
  class SnapExportReceiptV0(BaseModel):
@@ -268,3 +331,12 @@ class SnapExportReceiptV0(BaseModel):
268
331
  ad_account_id: Annotated[str, Field(alias='adAccountId')]
269
332
  inputs: list[SnapInputReceipt]
270
333
  totals: SegmentTotals
334
+
335
+
336
+ class TiktokExportReceiptV0(BaseModel):
337
+ model_config = ConfigDict(
338
+ populate_by_name=True,
339
+ )
340
+ advertiser_id: Annotated[str, Field(alias='advertiserId')]
341
+ inputs: list[TiktokInputReceipt]
342
+ totals: SegmentTotals
@@ -92,8 +92,17 @@ class ExportParamsSnap(BaseModel):
92
92
  type: Literal['snap']
93
93
 
94
94
 
95
- class ExportParams(RootModel[ExportParamsBucket | ExportParamsSnap]):
96
- root: ExportParamsBucket | ExportParamsSnap
95
+ class ExportParamsTiktok(BaseModel):
96
+ model_config = ConfigDict(
97
+ populate_by_name=True,
98
+ )
99
+ type: Literal['tiktok']
100
+
101
+
102
+ class ExportParams(
103
+ RootModel[ExportParamsBucket | ExportParamsSnap | ExportParamsTiktok]
104
+ ):
105
+ root: ExportParamsBucket | ExportParamsSnap | ExportParamsTiktok
97
106
 
98
107
 
99
108
  class IdentifierType(Enum):
@@ -163,6 +172,37 @@ class SnapInput(BaseModel):
163
172
  ]
164
173
 
165
174
 
175
+ class TiktokInput(BaseModel):
176
+ model_config = ConfigDict(
177
+ populate_by_name=True,
178
+ )
179
+ identifier_type: Annotated[IdentifierType, Field(alias='identifierType')]
180
+ leaf_id: Annotated[str, Field(alias='leafId')]
181
+ merge_mode: Annotated[MergeMode, Field(alias='mergeMode')]
182
+ replace_existing_segment: Annotated[
183
+ bool | None,
184
+ Field(
185
+ alias='replaceExistingSegment',
186
+ description='Lets a `Replace` clear a Segment that already existed. Without it a run\nrefuses to clear one.',
187
+ ),
188
+ ] = False
189
+ retention_days: Annotated[
190
+ int | None,
191
+ Field(
192
+ alias='retentionDays',
193
+ description='Applied when the Segment is created.',
194
+ ge=0,
195
+ ),
196
+ ] = None
197
+ segment_name: Annotated[
198
+ str,
199
+ Field(
200
+ alias='segmentName',
201
+ description='The Segment is found by this name in the advertiser, or created.',
202
+ ),
203
+ ]
204
+
205
+
166
206
  class ValidateConnectionParamsBucket(BaseModel):
167
207
  model_config = ConfigDict(
168
208
  populate_by_name=True,
@@ -220,10 +260,29 @@ class ExportConnectorKindSnap(BaseModel):
220
260
  type: Literal['snap']
221
261
 
222
262
 
263
+ class ExportConnectorKindTiktok(BaseModel):
264
+ model_config = ConfigDict(
265
+ populate_by_name=True,
266
+ )
267
+ advertiser_id: Annotated[
268
+ str | None,
269
+ Field(
270
+ alias='advertiserId',
271
+ description='Digits only. `None` until the owner picks one from the advertisers the\ngrant reaches.',
272
+ ),
273
+ ] = None
274
+ inputs: list[TiktokInput]
275
+ type: Literal['tiktok']
276
+
277
+
223
278
  class ExportConnectorKind(
224
- RootModel[ExportConnectorKindBucket | ExportConnectorKindSnap]
279
+ RootModel[
280
+ ExportConnectorKindBucket | ExportConnectorKindSnap | ExportConnectorKindTiktok
281
+ ]
225
282
  ):
226
- root: ExportConnectorKindBucket | ExportConnectorKindSnap
283
+ root: (
284
+ ExportConnectorKindBucket | ExportConnectorKindSnap | ExportConnectorKindTiktok
285
+ )
227
286
 
228
287
 
229
288
  class SnapExportConfig(BaseModel):
@@ -240,6 +299,20 @@ class SnapExportConfig(BaseModel):
240
299
  inputs: list[SnapInput]
241
300
 
242
301
 
302
+ class TiktokExportConfig(BaseModel):
303
+ model_config = ConfigDict(
304
+ populate_by_name=True,
305
+ )
306
+ advertiser_id: Annotated[
307
+ str | None,
308
+ Field(
309
+ alias='advertiserId',
310
+ description='Digits only. `None` until the owner picks one from the advertisers the\ngrant reaches.',
311
+ ),
312
+ ] = None
313
+ inputs: list[TiktokInput]
314
+
315
+
243
316
  class ValidateConnectionParamsSnap(BaseModel):
244
317
  model_config = ConfigDict(
245
318
  populate_by_name=True,
@@ -248,10 +321,26 @@ class ValidateConnectionParamsSnap(BaseModel):
248
321
  type: Literal['snap']
249
322
 
250
323
 
324
+ class ValidateConnectionParamsTiktok(BaseModel):
325
+ model_config = ConfigDict(
326
+ populate_by_name=True,
327
+ )
328
+ mode: ValidationMode
329
+ type: Literal['tiktok']
330
+
331
+
251
332
  class ValidateConnectionParams(
252
- RootModel[ValidateConnectionParamsBucket | ValidateConnectionParamsSnap]
333
+ RootModel[
334
+ ValidateConnectionParamsBucket
335
+ | ValidateConnectionParamsSnap
336
+ | ValidateConnectionParamsTiktok
337
+ ]
253
338
  ):
254
- root: ValidateConnectionParamsBucket | ValidateConnectionParamsSnap
339
+ root: (
340
+ ValidateConnectionParamsBucket
341
+ | ValidateConnectionParamsSnap
342
+ | ValidateConnectionParamsTiktok
343
+ )
255
344
 
256
345
 
257
346
  class ValidateConnection(BaseModel):
@@ -38,6 +38,9 @@ class Classification:
38
38
  # field, truncated. Never the body.
39
39
  platform_code: str | None = None
40
40
  platform_message: str | None = None
41
+ # The text that becomes the error's `details.providerError`, for a
42
+ # platform error whose own text the owner should see. Redacted when written.
43
+ provider_error: str | None = None
41
44
 
42
45
 
43
46
  Classifier = Callable[[requests.Response], Classification]
@@ -35,6 +35,35 @@ class Authenticator(Protocol):
35
35
  ...
36
36
 
37
37
 
38
+ class Pacer:
39
+ """Spaces requests to the same endpoint template at least `min_interval`
40
+ seconds apart, for per-endpoint limits that backoff alone keeps tripping."""
41
+
42
+ def __init__(
43
+ self,
44
+ min_interval: float,
45
+ endpoints: frozenset[str],
46
+ *,
47
+ sleep: Callable[[float], None] = time.sleep,
48
+ clock: Callable[[], float] = time.monotonic,
49
+ ) -> None:
50
+ self.min_interval = min_interval
51
+ self.endpoints = endpoints
52
+ self.sleep = sleep
53
+ self.clock = clock
54
+ self._last: dict[str, float] = {}
55
+
56
+ def wait(self, endpoint: str) -> None:
57
+ if endpoint not in self.endpoints:
58
+ return
59
+ last = self._last.get(endpoint)
60
+ if last is not None:
61
+ remaining = last + self.min_interval - self.clock()
62
+ if remaining > 0:
63
+ self.sleep(remaining)
64
+ self._last[endpoint] = self.clock()
65
+
66
+
38
67
  def _never_sent(exc: requests.RequestException) -> bool:
39
68
  """True when the connection was never established, so the server can't
40
69
  have seen the request."""
@@ -85,6 +114,7 @@ class HttpClient:
85
114
  jitter: Callable[[], float] = random.random,
86
115
  max_attempts: int = MAX_ATTEMPTS,
87
116
  max_total_retry_seconds: float = MAX_TOTAL_RETRY_SECONDS,
117
+ pacer: Pacer | None = None,
88
118
  ) -> None:
89
119
  self.allowed_hosts = allowed_hosts
90
120
  self.classify = classify
@@ -95,6 +125,7 @@ class HttpClient:
95
125
  self.jitter = jitter
96
126
  self.max_attempts = max_attempts
97
127
  self.max_total_retry_seconds = max_total_retry_seconds
128
+ self.pacer = pacer
98
129
 
99
130
  def check_host(self, url: str) -> None:
100
131
  # Parsed the way requests parses it, so the host checked is the host
@@ -125,6 +156,7 @@ class HttpClient:
125
156
  params: Mapping[str, Any] | None = None,
126
157
  json: Any = None,
127
158
  data: Mapping[str, str] | None = None,
159
+ files: Mapping[str, Any] | None = None,
128
160
  max_attempts: int | None = None,
129
161
  ) -> requests.Response:
130
162
  """Send one logical request. `endpoint` is the URL template that goes into
@@ -147,6 +179,8 @@ class HttpClient:
147
179
  while True:
148
180
  attempt += 1
149
181
  request_headers = dict(headers or {})
182
+ if self.pacer is not None:
183
+ self.pacer.wait(endpoint)
150
184
  if auth is not None:
151
185
  request_headers.update(_authenticate(auth.auth_headers, maybe_applied))
152
186
  try:
@@ -157,6 +191,7 @@ class HttpClient:
157
191
  params=params,
158
192
  json=json,
159
193
  data=data,
194
+ files=files,
160
195
  timeout=(CONNECT_TIMEOUT_SECONDS, READ_TIMEOUT_SECONDS),
161
196
  allow_redirects=False,
162
197
  )
@@ -219,7 +254,7 @@ class HttpClient:
219
254
  detail += f": {result.platform_message}"
220
255
  if result.code is ErrorCode.ACCESS_DENIED:
221
256
  detail += "; give the platform user a role on the ad account that allows this (re-authorising won't help)"
222
- return ExportConnectorError(result.code or ErrorCode.UNKNOWN, detail)
257
+ return ExportConnectorError(result.code or ErrorCode.UNKNOWN, detail, provider_error=result.provider_error)
223
258
 
224
259
  @staticmethod
225
260
  def _log_response(