dq-export-connector 0.1.0.dev33__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 (85) hide show
  1. dq_export_connector-0.1.0.dev35/.gitignore +5 -0
  2. dq_export_connector-0.1.0.dev35/PKG-INFO +237 -0
  3. dq_export_connector-0.1.0.dev35/README.md +222 -0
  4. dq_export_connector-0.1.0.dev35/dq_export_connector/_models/destination_account_listing.py +25 -0
  5. dq_export_connector-0.1.0.dev35/dq_export_connector/_models/export_connector_credentials.py +163 -0
  6. dq_export_connector-0.1.0.dev35/dq_export_connector/_models/export_receipt.py +342 -0
  7. dq_export_connector-0.1.0.dev35/dq_export_connector/_models/export_worker_config.py +392 -0
  8. dq_export_connector-0.1.0.dev35/dq_export_connector/audience/classify.py +84 -0
  9. dq_export_connector-0.1.0.dev35/dq_export_connector/audience/http.py +272 -0
  10. dq_export_connector-0.1.0.dev35/dq_export_connector/audience/identifiers.py +121 -0
  11. dq_export_connector-0.1.0.dev35/dq_export_connector/audience/inputs.py +80 -0
  12. dq_export_connector-0.1.0.dev35/dq_export_connector/audience/sync.py +342 -0
  13. dq_export_connector-0.1.0.dev35/dq_export_connector/bucket/__init__.py +0 -0
  14. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/export.py +1 -1
  15. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/errors.py +40 -4
  16. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/models/__init__.py +7 -0
  17. dq_export_connector-0.1.0.dev35/dq_export_connector/models/segment.py +8 -0
  18. dq_export_connector-0.1.0.dev35/dq_export_connector/models/snap.py +43 -0
  19. dq_export_connector-0.1.0.dev35/dq_export_connector/models/tiktok.py +43 -0
  20. dq_export_connector-0.1.0.dev35/dq_export_connector/oauth.py +236 -0
  21. dq_export_connector-0.1.0.dev35/dq_export_connector/redact.py +128 -0
  22. dq_export_connector-0.1.0.dev35/dq_export_connector/runner.py +306 -0
  23. dq_export_connector-0.1.0.dev35/dq_export_connector/snap/__init__.py +0 -0
  24. dq_export_connector-0.1.0.dev35/dq_export_connector/snap/accounts.py +18 -0
  25. dq_export_connector-0.1.0.dev35/dq_export_connector/snap/client.py +169 -0
  26. dq_export_connector-0.1.0.dev35/dq_export_connector/snap/destination.py +119 -0
  27. dq_export_connector-0.1.0.dev35/dq_export_connector/snap/export.py +127 -0
  28. dq_export_connector-0.1.0.dev35/dq_export_connector/snap/session.py +103 -0
  29. dq_export_connector-0.1.0.dev35/dq_export_connector/snap/validate.py +29 -0
  30. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/__init__.py +0 -0
  31. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/accounts.py +27 -0
  32. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/client.py +251 -0
  33. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/destination.py +247 -0
  34. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/export.py +128 -0
  35. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/session.py +88 -0
  36. dq_export_connector-0.1.0.dev35/dq_export_connector/tiktok/validate.py +38 -0
  37. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/pyproject.toml +14 -3
  38. dq_export_connector-0.1.0.dev35/scripts/_local.py +146 -0
  39. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/scripts/codegen-models.sh +2 -2
  40. dq_export_connector-0.1.0.dev35/scripts/run_local.py +96 -0
  41. dq_export_connector-0.1.0.dev35/tests/__init__.py +0 -0
  42. dq_export_connector-0.1.0.dev35/tests/conftest.py +37 -0
  43. dq_export_connector-0.1.0.dev35/tests/fakes/__init__.py +0 -0
  44. dq_export_connector-0.1.0.dev35/tests/fakes/snap_fake.py +285 -0
  45. dq_export_connector-0.1.0.dev35/tests/fakes/tiktok_fake.py +322 -0
  46. dq_export_connector-0.1.0.dev35/tests/test_audience_http.py +104 -0
  47. dq_export_connector-0.1.0.dev35/tests/test_http_and_oauth.py +364 -0
  48. dq_export_connector-0.1.0.dev35/tests/test_identifiers_and_inputs.py +118 -0
  49. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/tests/test_live_buckets.py +42 -4
  50. dq_export_connector-0.1.0.dev35/tests/test_live_snap.py +137 -0
  51. dq_export_connector-0.1.0.dev35/tests/test_live_tiktok.py +111 -0
  52. dq_export_connector-0.1.0.dev35/tests/test_oauth.py +204 -0
  53. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/tests/test_redact.py +16 -1
  54. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/tests/test_runner.py +23 -0
  55. dq_export_connector-0.1.0.dev35/tests/test_scripts.py +22 -0
  56. dq_export_connector-0.1.0.dev35/tests/test_snap_runner.py +659 -0
  57. dq_export_connector-0.1.0.dev35/tests/test_sync_input.py +203 -0
  58. dq_export_connector-0.1.0.dev35/tests/test_tiktok_runner.py +579 -0
  59. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/uv.lock +201 -13
  60. dq_export_connector-0.1.0.dev33/.gitignore +0 -63
  61. dq_export_connector-0.1.0.dev33/PKG-INFO +0 -64
  62. dq_export_connector-0.1.0.dev33/README.md +0 -53
  63. dq_export_connector-0.1.0.dev33/dq_export_connector/_models/export_connector_credentials.py +0 -74
  64. dq_export_connector-0.1.0.dev33/dq_export_connector/_models/export_receipt.py +0 -147
  65. dq_export_connector-0.1.0.dev33/dq_export_connector/_models/export_worker_config.py +0 -191
  66. dq_export_connector-0.1.0.dev33/dq_export_connector/redact.py +0 -74
  67. dq_export_connector-0.1.0.dev33/dq_export_connector/runner.py +0 -162
  68. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/.python-version +0 -0
  69. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/__init__.py +0 -0
  70. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/__init__.py +0 -0
  71. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/application_error.py +0 -0
  72. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/_models/application_warning.py +0 -0
  73. {dq_export_connector-0.1.0.dev33/dq_export_connector/bucket → dq_export_connector-0.1.0.dev35/dq_export_connector/audience}/__init__.py +0 -0
  74. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/paths.py +0 -0
  75. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/store.py +0 -0
  76. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/upload.py +0 -0
  77. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/bucket/validate.py +0 -0
  78. {dq_export_connector-0.1.0.dev33/dq_export_connector/bucket → dq_export_connector-0.1.0.dev35/dq_export_connector}/datasets.py +0 -0
  79. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/log.py +0 -0
  80. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/models/bucket.py +0 -0
  81. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/dq_export_connector/outcome.py +0 -0
  82. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/tests/test_models_are_current.py +0 -0
  83. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/tests/test_paths.py +0 -0
  84. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/tests/test_store.py +0 -0
  85. {dq_export_connector-0.1.0.dev33 → dq_export_connector-0.1.0.dev35}/tests/test_upload.py +0 -0
@@ -0,0 +1,5 @@
1
+ # Local secrets for the local runner.
2
+ credentials*.json
3
+ platform_secrets*.json
4
+ local/
5
+ /config*.json
@@ -0,0 +1,237 @@
1
+ Metadata-Version: 2.5
2
+ Name: dq-export-connector
3
+ Version: 0.1.0.dev35
4
+ Summary: Worker logic for the Export Connector DCR.
5
+ Author: Decentriq
6
+ Requires-Python: >=3.12
7
+ Requires-Dist: pydantic<3,>=2.14
8
+ Provides-Extra: bucket
9
+ Requires-Dist: obstore<0.12,>=0.11.1; extra == 'bucket'
10
+ Provides-Extra: snap
11
+ Requires-Dist: requests<3,>=2.32; extra == 'snap'
12
+ Provides-Extra: tiktok
13
+ Requires-Dist: requests<3,>=2.32; extra == 'tiktok'
14
+ Description-Content-Type: text/markdown
15
+
16
+ # dq-export-connector
17
+
18
+ Worker logic for the **Export Connector DCR**: one package for every
19
+ Connector Kind, each in its own sub-package with an extra of the same name for
20
+ its dependencies.
21
+
22
+ - The **bucket** kind uploads each Input's dataset to S3, Azure Blob or GCS
23
+ through [`obstore`](https://developmentseed.org/obstore/).
24
+ - The **snap** kind fills one Snap Segment per Input with hashed identifiers,
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.
28
+
29
+ ddc pins this package by hash in a fully locked `requirements.txt` and runs
30
+ `dq_export_connector.runner.main` in the task's Python environment. It is
31
+ published to PyPI, since the compiled graph installs it from there.
32
+
33
+ - `dq_export_connector.runner`: the entry point. Reads `/input/config.json`,
34
+ `/input/credentials.json` and, for a kind that uses OAuth, the OAuth App at
35
+ `/input/oauth_app.json`, sets up logging, and dispatches on the Connector
36
+ Kind and the action. It writes a `list` mode connection test's Destination
37
+ Account Listing into `/output`, and under `/internal` an export's receipt,
38
+ and either action's warnings and error, as the task's Task Report,
39
+ Application Warnings and Application Error. An action with an error also
40
+ writes it as `/internal/safe_error`, `CODE: message`, which the container
41
+ worker shows as the task's `error`, and exits with 1.
42
+ - `dq_export_connector.errors`: the error and warning codes the worker
43
+ reports, `ExportConnectorError`, which stops an action with an error code,
44
+ and `ExportConnectorWarning`. Each becomes an Application Error or Warning
45
+ with its credentials masked, its message cut to 1,000 characters and a
46
+ provider's own error to 4,000. Codes are named for the situation, not the
47
+ kind, so any kind can report them.
48
+ - `dq_export_connector.outcome`: collects a run's error, warnings and receipt
49
+ while it runs.
50
+ - `dq_export_connector.redact`: keeps every value from the sealed half, and
51
+ URL query strings, out of log lines and result messages. For the ad
52
+ platforms it also masks tokens, `key=secret` pairs, JWTs and SHA-256 hex.
53
+ - `dq_export_connector.datasets`: how an Input's dataset maps to files (a
54
+ stored ZIP, or one raw file).
55
+ - `dq_export_connector.bucket`: the bucket kind (`ValidateConnection` and
56
+ `Export`).
57
+ - `dq_export_connector.audience`: what the ad-platform kinds share: HTTPS to an
58
+ allowlist with retries by idempotency class, OAuth, identifier
59
+ normalisation, and the per-Input sync flow.
60
+ - `dq_export_connector.snap`: the snap kind.
61
+ - `dq_export_connector.tiktok`: the tiktok kind.
62
+ - `dq_export_connector.models`: the types the worker reads and writes, those
63
+ of every Connector Kind directly in it and a kind's own in its submodule,
64
+ such as `models.bucket`. They are pydantic models generated in
65
+ `dq_export_connector._models` from ddc's JSON schemas, so the config the
66
+ worker reads and the receipt, warnings and errors it writes match
67
+ ddc's Rust types. Regenerate them with `scripts/codegen-models.sh` after the
68
+ types change and `decentriq_dcr_compiler` has been rebuilt; a test fails
69
+ while they are stale.
70
+
71
+ ```sh
72
+ uv run pytest # unit tests, against obstore's in-memory store and fake Snap and TikTok APIs
73
+ uv run pytest -m live # against real buckets and Snap, credentials from the environment
74
+ uv run mypy dq_export_connector tests scripts
75
+ ```
76
+
77
+ ## Snap
78
+
79
+ A Snap connector's config names the ad account (set after consent, from a
80
+ `ValidateConnection` in `list` mode) and, per Input, a Segment, the identifier type its
81
+ lines hold and a Merge Mode. ddc validates it. The credentials are the OAuth
82
+ Grant of the Snap user who consented; each run turns it into an Access Token
83
+ with `dq_export_connector.oauth`. Decentriq's Snap app is the
84
+ `export_connector_snap_app` platform secret (`{"clientId", "clientSecret"}`),
85
+ mounted at `/input/oauth_app.json`. `ValidateConnection` in `list` mode lists
86
+ the ad accounts of every organization the grant reaches; in `check` mode it
87
+ reads the picked one.
88
+
89
+ A run looks each Segment up by name in the ad account and creates it when
90
+ there is none, marked `Managed by Decentriq`. It writes into a same-named
91
+ Segment whoever made it, with a `SEGMENT_NOT_MANAGED` warning when Decentriq
92
+ didn't. ADD adds the Input's values; REPLACE empties the Segment first, so it
93
+ is briefly empty. A run stops at the first Input that fails. When the failure
94
+ came after the Segment changed, the error says so; a rerun is safe in both
95
+ modes. Don't run one connector twice at the same time.
96
+
97
+ An Input is one file of values, or a stored ZIP holding one: one value per
98
+ line, UTF-8, optional BOM, LF or CRLF, surrounding double quotes removed,
99
+ empty lines skipped, no header.
100
+
101
+ ### Identifiers
102
+
103
+ | Type | Normalisation before SHA-256 | Snap schema |
104
+ |---|---|---|
105
+ | `EMAIL` | trim, lowercase; exactly one `@` with text on both sides | `EMAIL_SHA256` |
106
+ | `PHONE` | drop a `(0)` right after the country code (`+44 (0)20…`, `+44-(0)20…`), keep digits (any script's, as ASCII; drops `+`), drop a leading `00`; 8–15 digits, not starting with `0` | `PHONE_SHA256` |
107
+ | `IDFA`, `GAID` | UUID form with hyphens, lowercased | `MOBILE_AD_ID_SHA256` |
108
+ | `*_SHA256` | must be 64 hex characters; lowercased, otherwise unchanged | as above |
109
+
110
+ A pre-hashed value only matches if it was hashed from the same form: for
111
+ `PHONE_SHA256`, the digits with country code and no `+` (`41791234567`).
112
+
113
+ Up to 25% invalid rows gives an `INVALID_ROWS` warning; more fails the run
114
+ before the Segment changes. An empty Input gives an `EMPTY_DATASET` warning
115
+ and leaves its Segment as it was, in both modes.
116
+
117
+ The receipt counts invalid rows by reason (`counts.invalidReasons`, and in the
118
+ warning or error message), never with the values themselves:
119
+
120
+ | Reason | Meaning |
121
+ |---|---|
122
+ | `EMAIL_NO_AT_SIGN`, `EMAIL_MULTIPLE_AT_SIGNS` | not exactly one `@` |
123
+ | `EMAIL_EMPTY_LOCAL_PART`, `EMAIL_EMPTY_DOMAIN` | nothing before or after the `@` |
124
+ | `PHONE_NO_DIGITS` | no digits at all |
125
+ | `PHONE_NATIONAL_FORMAT` | starts with `0` after dropping `00`, i.e. no country code |
126
+ | `PHONE_TOO_SHORT`, `PHONE_TOO_LONG` | fewer than 8 or more than 15 digits |
127
+ | `NOT_A_MOBILE_AD_ID` | not a UUID with hyphens |
128
+ | `NOT_SHA256_HEX` | a `*_SHA256` value that isn't 64 hex characters |
129
+ | `NOT_UTF8` | the line isn't valid UTF-8 |
130
+ | `CONTROL_CHARACTERS` | the value holds control characters or a byte order mark, usually because the file is UTF-16 |
131
+
132
+ ### Testing against Snap
133
+
134
+ The local files are gitignored in this directory:
135
+
136
+ - `platform_secrets.json`: `{"export_connector_snap_app": {"clientId": …, "clientSecret": …}}`, the dev app.
137
+ - `credentials.json`: `{"type": "snap", "credentials": {"refreshToken": …}}`, a refresh token the owner got through consent on a dev environment. Snap doesn't rotate it, so it can be reused.
138
+ - `config.json`: `{"connector": {"type": "snap", "adAccountId": …, "inputs": [{"leafId": "segA", "segmentName": …, "identifierType": "EMAIL", "mergeMode": "ADD", "retentionDays": 180}]}}`.
139
+
140
+ 1. Run:
141
+
142
+ ```sh
143
+ mkdir -p local/inputs && printf 'a@example.com\nb@example.com\n' > local/inputs/segA
144
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
145
+ --platform-secrets platform_secrets.json --mode validate
146
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
147
+ --platform-secrets platform_secrets.json
148
+ ```
149
+ 2. Live tests, including the check that Snap doesn't rotate refresh tokens:
150
+
151
+ ```sh
152
+ DQ_EXPORT_LIVE_SNAP_DIR=$PWD uv run pytest -m live -s tests/test_live_snap.py
153
+ ```
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
+
229
+ ## Releasing
230
+
231
+ Bump the version with `uv version <version> --no-sync`, which also updates
232
+ `uv.lock`, commit, and push a `dq-export-connector-v<version>` tag on that
233
+ commit; the `Publish dq-export-connector to PyPI` workflow tests, builds and
234
+ uploads it. Use `.devN` versions while developing; PyPI releases can't be replaced.
235
+ ddc then pins the new version, as
236
+ [ddc's export worker README](../../dcr-compiler/ddc/src/archv2/data_room/export_connector/README.md)
237
+ describes.
@@ -0,0 +1,222 @@
1
+ # dq-export-connector
2
+
3
+ Worker logic for the **Export Connector DCR**: one package for every
4
+ Connector Kind, each in its own sub-package with an extra of the same name for
5
+ its dependencies.
6
+
7
+ - The **bucket** kind uploads each Input's dataset to S3, Azure Blob or GCS
8
+ through [`obstore`](https://developmentseed.org/obstore/).
9
+ - The **snap** kind fills one Snap Segment per Input with hashed identifiers,
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.
13
+
14
+ ddc pins this package by hash in a fully locked `requirements.txt` and runs
15
+ `dq_export_connector.runner.main` in the task's Python environment. It is
16
+ published to PyPI, since the compiled graph installs it from there.
17
+
18
+ - `dq_export_connector.runner`: the entry point. Reads `/input/config.json`,
19
+ `/input/credentials.json` and, for a kind that uses OAuth, the OAuth App at
20
+ `/input/oauth_app.json`, sets up logging, and dispatches on the Connector
21
+ Kind and the action. It writes a `list` mode connection test's Destination
22
+ Account Listing into `/output`, and under `/internal` an export's receipt,
23
+ and either action's warnings and error, as the task's Task Report,
24
+ Application Warnings and Application Error. An action with an error also
25
+ writes it as `/internal/safe_error`, `CODE: message`, which the container
26
+ worker shows as the task's `error`, and exits with 1.
27
+ - `dq_export_connector.errors`: the error and warning codes the worker
28
+ reports, `ExportConnectorError`, which stops an action with an error code,
29
+ and `ExportConnectorWarning`. Each becomes an Application Error or Warning
30
+ with its credentials masked, its message cut to 1,000 characters and a
31
+ provider's own error to 4,000. Codes are named for the situation, not the
32
+ kind, so any kind can report them.
33
+ - `dq_export_connector.outcome`: collects a run's error, warnings and receipt
34
+ while it runs.
35
+ - `dq_export_connector.redact`: keeps every value from the sealed half, and
36
+ URL query strings, out of log lines and result messages. For the ad
37
+ platforms it also masks tokens, `key=secret` pairs, JWTs and SHA-256 hex.
38
+ - `dq_export_connector.datasets`: how an Input's dataset maps to files (a
39
+ stored ZIP, or one raw file).
40
+ - `dq_export_connector.bucket`: the bucket kind (`ValidateConnection` and
41
+ `Export`).
42
+ - `dq_export_connector.audience`: what the ad-platform kinds share: HTTPS to an
43
+ allowlist with retries by idempotency class, OAuth, identifier
44
+ normalisation, and the per-Input sync flow.
45
+ - `dq_export_connector.snap`: the snap kind.
46
+ - `dq_export_connector.tiktok`: the tiktok kind.
47
+ - `dq_export_connector.models`: the types the worker reads and writes, those
48
+ of every Connector Kind directly in it and a kind's own in its submodule,
49
+ such as `models.bucket`. They are pydantic models generated in
50
+ `dq_export_connector._models` from ddc's JSON schemas, so the config the
51
+ worker reads and the receipt, warnings and errors it writes match
52
+ ddc's Rust types. Regenerate them with `scripts/codegen-models.sh` after the
53
+ types change and `decentriq_dcr_compiler` has been rebuilt; a test fails
54
+ while they are stale.
55
+
56
+ ```sh
57
+ uv run pytest # unit tests, against obstore's in-memory store and fake Snap and TikTok APIs
58
+ uv run pytest -m live # against real buckets and Snap, credentials from the environment
59
+ uv run mypy dq_export_connector tests scripts
60
+ ```
61
+
62
+ ## Snap
63
+
64
+ A Snap connector's config names the ad account (set after consent, from a
65
+ `ValidateConnection` in `list` mode) and, per Input, a Segment, the identifier type its
66
+ lines hold and a Merge Mode. ddc validates it. The credentials are the OAuth
67
+ Grant of the Snap user who consented; each run turns it into an Access Token
68
+ with `dq_export_connector.oauth`. Decentriq's Snap app is the
69
+ `export_connector_snap_app` platform secret (`{"clientId", "clientSecret"}`),
70
+ mounted at `/input/oauth_app.json`. `ValidateConnection` in `list` mode lists
71
+ the ad accounts of every organization the grant reaches; in `check` mode it
72
+ reads the picked one.
73
+
74
+ A run looks each Segment up by name in the ad account and creates it when
75
+ there is none, marked `Managed by Decentriq`. It writes into a same-named
76
+ Segment whoever made it, with a `SEGMENT_NOT_MANAGED` warning when Decentriq
77
+ didn't. ADD adds the Input's values; REPLACE empties the Segment first, so it
78
+ is briefly empty. A run stops at the first Input that fails. When the failure
79
+ came after the Segment changed, the error says so; a rerun is safe in both
80
+ modes. Don't run one connector twice at the same time.
81
+
82
+ An Input is one file of values, or a stored ZIP holding one: one value per
83
+ line, UTF-8, optional BOM, LF or CRLF, surrounding double quotes removed,
84
+ empty lines skipped, no header.
85
+
86
+ ### Identifiers
87
+
88
+ | Type | Normalisation before SHA-256 | Snap schema |
89
+ |---|---|---|
90
+ | `EMAIL` | trim, lowercase; exactly one `@` with text on both sides | `EMAIL_SHA256` |
91
+ | `PHONE` | drop a `(0)` right after the country code (`+44 (0)20…`, `+44-(0)20…`), keep digits (any script's, as ASCII; drops `+`), drop a leading `00`; 8–15 digits, not starting with `0` | `PHONE_SHA256` |
92
+ | `IDFA`, `GAID` | UUID form with hyphens, lowercased | `MOBILE_AD_ID_SHA256` |
93
+ | `*_SHA256` | must be 64 hex characters; lowercased, otherwise unchanged | as above |
94
+
95
+ A pre-hashed value only matches if it was hashed from the same form: for
96
+ `PHONE_SHA256`, the digits with country code and no `+` (`41791234567`).
97
+
98
+ Up to 25% invalid rows gives an `INVALID_ROWS` warning; more fails the run
99
+ before the Segment changes. An empty Input gives an `EMPTY_DATASET` warning
100
+ and leaves its Segment as it was, in both modes.
101
+
102
+ The receipt counts invalid rows by reason (`counts.invalidReasons`, and in the
103
+ warning or error message), never with the values themselves:
104
+
105
+ | Reason | Meaning |
106
+ |---|---|
107
+ | `EMAIL_NO_AT_SIGN`, `EMAIL_MULTIPLE_AT_SIGNS` | not exactly one `@` |
108
+ | `EMAIL_EMPTY_LOCAL_PART`, `EMAIL_EMPTY_DOMAIN` | nothing before or after the `@` |
109
+ | `PHONE_NO_DIGITS` | no digits at all |
110
+ | `PHONE_NATIONAL_FORMAT` | starts with `0` after dropping `00`, i.e. no country code |
111
+ | `PHONE_TOO_SHORT`, `PHONE_TOO_LONG` | fewer than 8 or more than 15 digits |
112
+ | `NOT_A_MOBILE_AD_ID` | not a UUID with hyphens |
113
+ | `NOT_SHA256_HEX` | a `*_SHA256` value that isn't 64 hex characters |
114
+ | `NOT_UTF8` | the line isn't valid UTF-8 |
115
+ | `CONTROL_CHARACTERS` | the value holds control characters or a byte order mark, usually because the file is UTF-16 |
116
+
117
+ ### Testing against Snap
118
+
119
+ The local files are gitignored in this directory:
120
+
121
+ - `platform_secrets.json`: `{"export_connector_snap_app": {"clientId": …, "clientSecret": …}}`, the dev app.
122
+ - `credentials.json`: `{"type": "snap", "credentials": {"refreshToken": …}}`, a refresh token the owner got through consent on a dev environment. Snap doesn't rotate it, so it can be reused.
123
+ - `config.json`: `{"connector": {"type": "snap", "adAccountId": …, "inputs": [{"leafId": "segA", "segmentName": …, "identifierType": "EMAIL", "mergeMode": "ADD", "retentionDays": 180}]}}`.
124
+
125
+ 1. Run:
126
+
127
+ ```sh
128
+ mkdir -p local/inputs && printf 'a@example.com\nb@example.com\n' > local/inputs/segA
129
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
130
+ --platform-secrets platform_secrets.json --mode validate
131
+ uv run python scripts/run_local.py --config config.json --credentials credentials.json \
132
+ --platform-secrets platform_secrets.json
133
+ ```
134
+ 2. Live tests, including the check that Snap doesn't rotate refresh tokens:
135
+
136
+ ```sh
137
+ DQ_EXPORT_LIVE_SNAP_DIR=$PWD uv run pytest -m live -s tests/test_live_snap.py
138
+ ```
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
+
214
+ ## Releasing
215
+
216
+ Bump the version with `uv version <version> --no-sync`, which also updates
217
+ `uv.lock`, commit, and push a `dq-export-connector-v<version>` tag on that
218
+ commit; the `Publish dq-export-connector to PyPI` workflow tests, builds and
219
+ uploads it. Use `.devN` versions while developing; PyPI releases can't be replaced.
220
+ ddc then pins the new version, as
221
+ [ddc's export worker README](../../dcr-compiler/ddc/src/archv2/data_room/export_connector/README.md)
222
+ describes.
@@ -0,0 +1,25 @@
1
+ # generated by datamodel-codegen:
2
+ # filename: destination_account_listing.json
3
+
4
+ from __future__ import annotations
5
+
6
+ from typing import Annotated
7
+
8
+ from pydantic import BaseModel, ConfigDict, Field
9
+
10
+
11
+ class DestinationAccount(BaseModel):
12
+ model_config = ConfigDict(
13
+ populate_by_name=True,
14
+ )
15
+ id: str
16
+ name: str
17
+
18
+
19
+ class DestinationAccountListing(BaseModel):
20
+ model_config = ConfigDict(
21
+ populate_by_name=True,
22
+ )
23
+ destination_accounts: Annotated[
24
+ list[DestinationAccount], Field(alias='destinationAccounts')
25
+ ]
@@ -0,0 +1,163 @@
1
+ # generated by datamodel-codegen:
2
+ # filename: export_connector_credentials.json
3
+
4
+ from __future__ import annotations
5
+
6
+ from typing import Annotated, Literal
7
+
8
+ from pydantic import BaseModel, ConfigDict, Field, RootModel
9
+
10
+
11
+ class BucketExportCredentialsS3(BaseModel):
12
+ model_config = ConfigDict(
13
+ populate_by_name=True,
14
+ )
15
+ access_key: Annotated[str, Field(alias='accessKey')]
16
+ secret_key: Annotated[str, Field(alias='secretKey')]
17
+ type: Literal['s3']
18
+
19
+
20
+ class BucketExportCredentialsAzureBlob(BaseModel):
21
+ model_config = ConfigDict(
22
+ populate_by_name=True,
23
+ )
24
+ sas_token: Annotated[
25
+ str,
26
+ Field(
27
+ alias='sasToken',
28
+ description='A container SAS token. Only the grant: the account and container\nit applies to are pinned by the signed `BucketConnection`.',
29
+ ),
30
+ ]
31
+ type: Literal['azureBlob']
32
+
33
+
34
+ class BucketExportCredentialsGcs(BaseModel):
35
+ model_config = ConfigDict(
36
+ populate_by_name=True,
37
+ )
38
+ service_account_json: Annotated[str, Field(alias='serviceAccountJson')]
39
+ type: Literal['gcs']
40
+
41
+
42
+ class BucketExportCredentials(
43
+ RootModel[
44
+ BucketExportCredentialsS3
45
+ | BucketExportCredentialsAzureBlob
46
+ | BucketExportCredentialsGcs
47
+ ]
48
+ ):
49
+ root: Annotated[
50
+ BucketExportCredentialsS3
51
+ | BucketExportCredentialsAzureBlob
52
+ | BucketExportCredentialsGcs,
53
+ Field(
54
+ description='The credentials of a Bucket connection, keyed by Bucket Provider like\n[`BucketConnection`]. Secret leaf fields are `Secret`-wrapped so they mask\nin a `redacted()` view but serialize transparently everywhere else.'
55
+ ),
56
+ ]
57
+
58
+
59
+ class GrantTokenRefreshToken(BaseModel):
60
+ model_config = ConfigDict(
61
+ populate_by_name=True,
62
+ )
63
+ type: Literal['refreshToken']
64
+ value: str
65
+
66
+
67
+ class GrantTokenAccessToken(BaseModel):
68
+ model_config = ConfigDict(
69
+ populate_by_name=True,
70
+ )
71
+ type: Literal['accessToken']
72
+ value: str
73
+
74
+
75
+ class GrantToken(RootModel[GrantTokenRefreshToken | GrantTokenAccessToken]):
76
+ root: GrantTokenRefreshToken | GrantTokenAccessToken
77
+
78
+
79
+ class OAuthGrant(BaseModel):
80
+ model_config = ConfigDict(
81
+ populate_by_name=True,
82
+ )
83
+ client_id: Annotated[
84
+ str,
85
+ Field(
86
+ alias='clientId',
87
+ description='The OAuth App the grant was issued to; it only works with that app.',
88
+ ),
89
+ ]
90
+ obtained_at: Annotated[
91
+ str, Field(alias='obtainedAt', description='RFC 3339, in UTC.')
92
+ ]
93
+ token: GrantToken
94
+
95
+
96
+ class SnapExportCredentialsOauth(BaseModel):
97
+ model_config = ConfigDict(
98
+ populate_by_name=True,
99
+ )
100
+ grant: OAuthGrant
101
+ type: Literal['oauth']
102
+
103
+
104
+ class SnapExportCredentials(RootModel[SnapExportCredentialsOauth]):
105
+ root: Annotated[
106
+ SnapExportCredentialsOauth, Field(description='What is sealed for Snap.')
107
+ ]
108
+
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
+
124
+ class ExportConnectorCredentialsBucket(BaseModel):
125
+ model_config = ConfigDict(
126
+ populate_by_name=True,
127
+ )
128
+ credentials: BucketExportCredentials
129
+ type: Literal['bucket']
130
+
131
+
132
+ class ExportConnectorCredentialsSnap(BaseModel):
133
+ model_config = ConfigDict(
134
+ populate_by_name=True,
135
+ )
136
+ credentials: SnapExportCredentials
137
+ type: Literal['snap']
138
+
139
+
140
+ class ExportConnectorCredentialsTiktok(BaseModel):
141
+ model_config = ConfigDict(
142
+ populate_by_name=True,
143
+ )
144
+ credentials: TiktokExportCredentials
145
+ type: Literal['tiktok']
146
+
147
+
148
+ class ExportConnectorCredentials(
149
+ RootModel[
150
+ ExportConnectorCredentialsBucket
151
+ | ExportConnectorCredentialsSnap
152
+ | ExportConnectorCredentialsTiktok
153
+ ]
154
+ ):
155
+ root: Annotated[
156
+ ExportConnectorCredentialsBucket
157
+ | ExportConnectorCredentialsSnap
158
+ | ExportConnectorCredentialsTiktok,
159
+ Field(
160
+ description='Keyed by Connector Kind, in step with [`ExportConnectorKind`]. The worker\nreads it at `/input/credentials.json`.',
161
+ title='ExportConnectorCredentials',
162
+ ),
163
+ ]