markproof 0.3.0__tar.gz → 0.4.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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: markproof
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Python client for the Markproof API - EU AI Act Article 50 watermarking and verification.
5
5
  Author-email: Markproof <support@markproof.co>
6
6
  License-Expression: MIT
@@ -48,12 +48,23 @@ print(result.summary)
48
48
  ## API
49
49
 
50
50
  - `Markproof(api_key, base_url=DEFAULT_BASE_URL)` — `base_url` defaults to the production API; override for local dev.
51
- - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format="file", visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (default) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
51
+ - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format=None, visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (the default for small images and audio) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
52
52
  - `client.verify(file, filename=None)` or `client.verify(url=...)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`. Direct uploads are limited to about 4.5 MB; pass a public `url` (up to 25 MB) for bigger files, such as marked audio, which comes back as WAV (e.g. a batch item's `file_url`).
53
53
  - `client.get_asset(asset_id)` → `Asset` — requires an API key on the same project that created the asset.
54
54
 
55
55
  All methods raise `MarkproofError` (with `.status` and `.body`) on a non-2xx response.
56
56
 
57
+ ### Video and large files (launches with video)
58
+
59
+ `mark` and `verify` also take `url=` (https, up to 25 MB) or `upload_id=` instead of `file`. Video, and any
60
+ file over about 4 MB, is sent through a direct upload automatically: the SDK asks the API for a signed
61
+ upload URL, PUTs the file straight to storage (up to 100 MB), then marks or verifies it by id. For these,
62
+ `response_format` defaults to `"url"`, because the marked file is too big to come back inline. Call
63
+ `client.upload(file, filename=None)` → `upload_id` to do the upload yourself; each upload can be used
64
+ once. `result.calls_used` says how many quota calls a mark used: 1 per image or audio file, one per
65
+ started second of video. Until video launches, the upload step fails with a 404 `MarkproofError`
66
+ saying so; pass `url=` for big files meanwhile.
67
+
57
68
  ## Batches and webhooks
58
69
 
59
70
  For volume, queue many files at once instead of calling `mark` per file. Each item counts as one mark.
@@ -29,12 +29,23 @@ print(result.summary)
29
29
  ## API
30
30
 
31
31
  - `Markproof(api_key, base_url=DEFAULT_BASE_URL)` — `base_url` defaults to the production API; override for local dev.
32
- - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format="file", visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (default) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
32
+ - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format=None, visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (the default for small images and audio) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
33
33
  - `client.verify(file, filename=None)` or `client.verify(url=...)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`. Direct uploads are limited to about 4.5 MB; pass a public `url` (up to 25 MB) for bigger files, such as marked audio, which comes back as WAV (e.g. a batch item's `file_url`).
34
34
  - `client.get_asset(asset_id)` → `Asset` — requires an API key on the same project that created the asset.
35
35
 
36
36
  All methods raise `MarkproofError` (with `.status` and `.body`) on a non-2xx response.
37
37
 
38
+ ### Video and large files (launches with video)
39
+
40
+ `mark` and `verify` also take `url=` (https, up to 25 MB) or `upload_id=` instead of `file`. Video, and any
41
+ file over about 4 MB, is sent through a direct upload automatically: the SDK asks the API for a signed
42
+ upload URL, PUTs the file straight to storage (up to 100 MB), then marks or verifies it by id. For these,
43
+ `response_format` defaults to `"url"`, because the marked file is too big to come back inline. Call
44
+ `client.upload(file, filename=None)` → `upload_id` to do the upload yourself; each upload can be used
45
+ once. `result.calls_used` says how many quota calls a mark used: 1 per image or audio file, one per
46
+ started second of video. Until video launches, the upload step fails with a 404 `MarkproofError`
47
+ saying so; pass `url=` for big files meanwhile.
48
+
38
49
  ## Batches and webhooks
39
50
 
40
51
  For volume, queue many files at once instead of calling `mark` per file. Each item counts as one mark.
@@ -16,6 +16,9 @@ from .batches import Batch, CreatedBatch, _batch_from_api
16
16
  from .errors import MarkproofError
17
17
 
18
18
  DEFAULT_BASE_URL = "https://markproof.co/v1"
19
+ # Files bigger than this (and all video) go through a direct upload (POST /uploads) instead of
20
+ # the request body: the API's host caps request bodies at about 4.5 MB, multipart overhead included.
21
+ INLINE_UPLOAD_LIMIT = 4 * 1024 * 1024
19
22
 
20
23
  FileLike = Union[str, "os.PathLike[str]", bytes]
21
24
 
@@ -38,6 +41,9 @@ class MarkResult:
38
41
  # Populated when response_format="url". Short-lived (~1 hour) - download
39
42
  # promptly rather than caching it.
40
43
  file_url: Optional[str] = None
44
+ # Monthly-quota calls this mark used: 1 for an image or audio file, one per
45
+ # started second for video. None from servers that don't report it yet.
46
+ calls_used: Optional[int] = None
41
47
 
42
48
 
43
49
  @dataclass
@@ -93,6 +99,30 @@ def _kind_from_content_type(content_type: str) -> str:
93
99
  return "image"
94
100
 
95
101
 
102
+ _AUDIO_ONLY_BRANDS = {b"M4A ", b"M4B ", b"M4P ", b"F4A ", b"F4B "}
103
+
104
+
105
+ def _looks_like_video(data: bytes, content_type: str) -> bool:
106
+ """Video by MIME type, or by container (MP4/MOV/WebM), like the API's own sniff."""
107
+ if content_type.startswith("video/"):
108
+ return True
109
+ if len(data) < 12:
110
+ return False
111
+ box = data[4:8]
112
+ if box == b"ftyp":
113
+ return data[8:12] not in _AUDIO_ONLY_BRANDS
114
+ if box in (b"moov", b"mdat", b"wide", b"free", b"skip"):
115
+ return True
116
+ return data[:4] == b"\x1a\x45\xdf\xa3"
117
+
118
+
119
+ def _calls_used(value: object) -> Optional[int]:
120
+ try:
121
+ return int(value) if value is not None else None
122
+ except (TypeError, ValueError):
123
+ return None
124
+
125
+
96
126
  def _resolve_file(file: FileLike, filename: Optional[str]) -> tuple[bytes, str, str]:
97
127
  """Returns (data, filename, content_type)."""
98
128
  if isinstance(file, (bytes, bytearray)):
@@ -132,46 +162,99 @@ class Markproof:
132
162
  )
133
163
  raise MarkproofError(message, response.status_code, body)
134
164
 
165
+ def upload(self, file: FileLike, filename: Optional[str] = None) -> str:
166
+ """Uploads a file straight to Markproof's storage and returns its ``upload_id``.
167
+
168
+ For files over the ~4.5 MB request limit, video above all. ``mark()`` and
169
+ ``verify()`` do this automatically; call it yourself to upload once and then
170
+ pass ``upload_id=`` (each upload can be marked or verified once, and unused
171
+ uploads are deleted after 24 hours). Up to 100 MB.
172
+ """
173
+ data, _, content_type = _resolve_file(file, filename)
174
+ return self._upload_bytes(data, content_type)
175
+
176
+ def _upload_bytes(self, data: bytes, content_type: str) -> str:
177
+ response = self._session.post(f"{self._base_url}/uploads", headers=self._headers())
178
+ if response.status_code == 404:
179
+ raise MarkproofError(
180
+ "Direct uploads aren't available on this server yet: send files under ~4.5 MB, or pass url=.",
181
+ 404,
182
+ None,
183
+ )
184
+ self._raise_if_error(response)
185
+ body = response.json()
186
+ put = self._session.put(body["upload_url"], data=data, headers={"Content-Type": content_type})
187
+ if not put.ok:
188
+ raise MarkproofError(f"Upload failed (status {put.status_code}).", put.status_code, put.text)
189
+ return body["upload_id"]
190
+
135
191
  def mark(
136
192
  self,
137
- file: FileLike,
193
+ file: Optional[FileLike] = None,
138
194
  filename: Optional[str] = None,
139
195
  generator_name: Optional[str] = None,
140
196
  model_name: Optional[str] = None,
141
- response_format: str = "file",
197
+ response_format: Optional[str] = None,
142
198
  visible_label: Union[bool, str, None] = None,
143
199
  label_position: Optional[str] = None,
200
+ *,
201
+ url: Optional[str] = None,
202
+ upload_id: Optional[str] = None,
144
203
  ) -> MarkResult:
145
204
  """Watermarks (and, if the server has C2PA signing configured, signs) a file.
146
205
 
147
- response_format="file" (default) returns the marked bytes inline
148
- (result.file/result.content_type). "url" returns a short-lived
149
- signed download URL instead (result.file_url).
206
+ Pass exactly one of ``file`` (a path or bytes), ``url`` (https, up to 25 MB)
207
+ or ``upload_id`` (from ``upload()``). Video, and any file over ~4 MB, is
208
+ sent through a direct upload automatically.
209
+
210
+ response_format="file" returns the marked bytes inline
211
+ (result.file/result.content_type); "url" returns a short-lived signed
212
+ download URL instead (result.file_url). Default: "url" for video and
213
+ uploads (marked video is too big to return inline), "file" otherwise.
150
214
 
151
- visible_label (images only; the API returns 400 for audio): True draws
152
- a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking,
215
+ visible_label (images only; the API returns 400 for audio and video): True
216
+ draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking,
153
217
  or pass your own text (max 40 characters). label_position:
154
218
  "bottom-right" (default), "bottom-left", "top-right" or "top-left".
155
219
  """
156
- data, resolved_filename, content_type = _resolve_file(file, filename)
157
- form_data = {}
220
+ if sum(x is not None for x in (file, url, upload_id)) != 1:
221
+ raise ValueError("Pass exactly one of file, url or upload_id.")
222
+
223
+ fields: dict = {}
158
224
  if generator_name:
159
- form_data["generator_name"] = generator_name
225
+ fields["generator_name"] = generator_name
160
226
  if model_name:
161
- form_data["model_name"] = model_name
162
- if response_format == "url":
163
- form_data["response_format"] = "url"
227
+ fields["model_name"] = model_name
164
228
  if visible_label:
165
- form_data["visible_label"] = "true" if visible_label is True else visible_label
229
+ fields["visible_label"] = "true" if visible_label is True else visible_label
166
230
  if label_position:
167
- form_data["label_position"] = label_position
231
+ fields["label_position"] = label_position
168
232
 
169
- response = self._session.post(
170
- f"{self._base_url}/mark",
171
- headers=self._headers(),
172
- files={"file": (resolved_filename, data, content_type)},
173
- data=form_data,
174
- )
233
+ if file is not None:
234
+ data, resolved_filename, content_type = _resolve_file(file, filename)
235
+ # Video, and anything too big for one request, goes through an upload. Its marked
236
+ # file would be too big to come back inline as well, hence "url" by default.
237
+ direct = _looks_like_video(data, content_type) or len(data) > INLINE_UPLOAD_LIMIT
238
+ response_format = response_format or ("url" if direct else "file")
239
+ if direct:
240
+ upload_id = self._upload_bytes(data, content_type)
241
+ else:
242
+ if response_format == "url":
243
+ fields["response_format"] = "url"
244
+ response = self._session.post(
245
+ f"{self._base_url}/mark",
246
+ headers=self._headers(),
247
+ files={"file": (resolved_filename, data, content_type)},
248
+ data=fields,
249
+ )
250
+ if file is None or upload_id is not None:
251
+ response_format = response_format or ("url" if upload_id is not None else "file")
252
+ source = {"upload_id": upload_id} if upload_id is not None else {"url": url}
253
+ response = self._session.post(
254
+ f"{self._base_url}/mark",
255
+ headers=self._headers(),
256
+ json={**source, **fields, "response_format": response_format},
257
+ )
175
258
  self._raise_if_error(response)
176
259
 
177
260
  if response_format == "url":
@@ -184,6 +267,7 @@ class Markproof:
184
267
  c2pa_manifest_label=body.get("c2pa_manifest_label"),
185
268
  watermark_robust=body.get("watermark_robust"),
186
269
  file_url=body["file_url"],
270
+ calls_used=_calls_used(body.get("calls_used", response.headers.get("x-calls-used"))),
187
271
  )
188
272
 
189
273
  response_content_type = response.headers.get("content-type", "")
@@ -196,28 +280,38 @@ class Markproof:
196
280
  watermark_robust={"true": True, "false": False}.get(response.headers.get("x-watermark-robust", "")),
197
281
  content_type=response_content_type,
198
282
  file=response.content,
283
+ calls_used=_calls_used(response.headers.get("x-calls-used")),
199
284
  )
200
285
 
201
286
  def verify(
202
- self, file: Optional[FileLike] = None, filename: Optional[str] = None, *, url: Optional[str] = None
287
+ self,
288
+ file: Optional[FileLike] = None,
289
+ filename: Optional[str] = None,
290
+ *,
291
+ url: Optional[str] = None,
292
+ upload_id: Optional[str] = None,
203
293
  ) -> VerifyResult:
204
294
  """Checks a file for our watermark and/or a C2PA manifest.
205
295
 
206
- Pass ``url`` instead of ``file`` for anything over ~4.5 MB (the
207
- direct-upload limit), e.g. marked audio, which comes back as WAV;
208
- URLs up to 25 MB work.
296
+ Pass exactly one of ``file``, ``url`` (https, up to 25 MB) or ``upload_id``.
297
+ Video and files over ~4 MB (marked audio, for example, comes back as WAV)
298
+ go through a direct upload automatically.
209
299
  """
210
- if (file is None) == (url is None):
211
- raise ValueError("Pass exactly one of file or url.")
212
- if url is not None:
213
- response = self._session.post(f"{self._base_url}/verify", headers=self._headers(), json={"url": url})
214
- else:
300
+ if sum(x is not None for x in (file, url, upload_id)) != 1:
301
+ raise ValueError("Pass exactly one of file, url or upload_id.")
302
+ if file is not None:
215
303
  data, resolved_filename, content_type = _resolve_file(file, filename)
216
- response = self._session.post(
217
- f"{self._base_url}/verify",
218
- headers=self._headers(),
219
- files={"file": (resolved_filename, data, content_type)},
220
- )
304
+ if _looks_like_video(data, content_type) or len(data) > INLINE_UPLOAD_LIMIT:
305
+ upload_id = self._upload_bytes(data, content_type)
306
+ else:
307
+ response = self._session.post(
308
+ f"{self._base_url}/verify",
309
+ headers=self._headers(),
310
+ files={"file": (resolved_filename, data, content_type)},
311
+ )
312
+ if file is None or upload_id is not None:
313
+ source = {"upload_id": upload_id} if upload_id is not None else {"url": url}
314
+ response = self._session.post(f"{self._base_url}/verify", headers=self._headers(), json=source)
221
315
  self._raise_if_error(response)
222
316
  body = response.json()
223
317
  return VerifyResult(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: markproof
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Python client for the Markproof API - EU AI Act Article 50 watermarking and verification.
5
5
  Author-email: Markproof <support@markproof.co>
6
6
  License-Expression: MIT
@@ -48,12 +48,23 @@ print(result.summary)
48
48
  ## API
49
49
 
50
50
  - `Markproof(api_key, base_url=DEFAULT_BASE_URL)` — `base_url` defaults to the production API; override for local dev.
51
- - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format="file", visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (default) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
51
+ - `client.mark(file, filename=None, generator_name=None, model_name=None, response_format=None, visible_label=None, label_position=None)` → `MarkResult` — `file` is a path (`str`/`os.PathLike`) or raw `bytes` (in which case `filename` is required). `response_format="file"` (the default for small images and audio) returns the marked bytes inline (`result.file`/`result.content_type`); `"url"` returns a short-lived signed download URL instead (`result.file_url`, ~1 hour — download promptly rather than caching it). Either way you also get `asset_id`, `watermark_code`, `verify_url`, `c2pa_manifest_label`. `visible_label` (images only): `True` draws a visible "AI-generated" label (EU AI Act Art. 50(4)) before marking, or pass your own text (max 40 characters); `label_position` picks the corner (`"bottom-right"` default, `"bottom-left"`, `"top-right"`, `"top-left"`).
52
52
  - `client.verify(file, filename=None)` or `client.verify(url=...)` → `VerifyResult` — `watermark`/`c2pa` detection results, `matched_asset` if the watermark matches a record in Markproof's database, and a human-readable `summary`. Direct uploads are limited to about 4.5 MB; pass a public `url` (up to 25 MB) for bigger files, such as marked audio, which comes back as WAV (e.g. a batch item's `file_url`).
53
53
  - `client.get_asset(asset_id)` → `Asset` — requires an API key on the same project that created the asset.
54
54
 
55
55
  All methods raise `MarkproofError` (with `.status` and `.body`) on a non-2xx response.
56
56
 
57
+ ### Video and large files (launches with video)
58
+
59
+ `mark` and `verify` also take `url=` (https, up to 25 MB) or `upload_id=` instead of `file`. Video, and any
60
+ file over about 4 MB, is sent through a direct upload automatically: the SDK asks the API for a signed
61
+ upload URL, PUTs the file straight to storage (up to 100 MB), then marks or verifies it by id. For these,
62
+ `response_format` defaults to `"url"`, because the marked file is too big to come back inline. Call
63
+ `client.upload(file, filename=None)` → `upload_id` to do the upload yourself; each upload can be used
64
+ once. `result.calls_used` says how many quota calls a mark used: 1 per image or audio file, one per
65
+ started second of video. Until video launches, the upload step fails with a 404 `MarkproofError`
66
+ saying so; pass `url=` for big files meanwhile.
67
+
57
68
  ## Batches and webhooks
58
69
 
59
70
  For volume, queue many files at once instead of calling `mark` per file. Each item counts as one mark.
@@ -13,4 +13,5 @@ markproof.egg-info/requires.txt
13
13
  markproof.egg-info/top_level.txt
14
14
  tests/test_batches.py
15
15
  tests/test_client.py
16
+ tests/test_uploads.py
16
17
  tests/test_webhooks.py
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "markproof"
7
- version = "0.3.0"
7
+ version = "0.4.0"
8
8
  description = "Python client for the Markproof API - EU AI Act Article 50 watermarking and verification."
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -0,0 +1,153 @@
1
+ import json
2
+
3
+ import pytest
4
+ import responses
5
+
6
+ from markproof import Markproof, MarkproofError
7
+ from markproof.client import INLINE_UPLOAD_LIMIT
8
+
9
+ API_KEY = "lt_test_abc123"
10
+ BASE_URL = "http://localhost:3000/api/v1"
11
+ UPLOAD_URL = "https://storage.example/upload/sign/tool1-uploads/p/u?token=t"
12
+ UPLOAD_ID = "3f2b8c1e-9a4d-4c7e-8b21-5d6f7a8b9c0d"
13
+ MP4 = b"\x00\x00\x00\x18ftypisom\x00\x00\x02\x00isomiso2" + b"\x00" * 64
14
+ MARKED_URL_BODY = {
15
+ "asset_id": "asset-1",
16
+ "watermark_code": "ABC12345",
17
+ "kind": "video",
18
+ "verify_url": "https://markproof.co/v/ABC12345",
19
+ "c2pa_manifest_label": "urn:c2pa:xyz",
20
+ "watermark_robust": True,
21
+ "file_url": "https://storage.example/signed",
22
+ "calls_used": 6,
23
+ }
24
+
25
+
26
+ @pytest.fixture
27
+ def client():
28
+ return Markproof(api_key=API_KEY, base_url=BASE_URL)
29
+
30
+
31
+ def _upload_routes():
32
+ responses.add(
33
+ responses.POST,
34
+ f"{BASE_URL}/uploads",
35
+ json={"upload_id": UPLOAD_ID, "upload_url": UPLOAD_URL, "upload_method": "PUT"},
36
+ status=201,
37
+ )
38
+ responses.add(responses.PUT, UPLOAD_URL, status=200)
39
+
40
+
41
+ @responses.activate
42
+ def test_upload_starts_puts_and_returns_the_id(client):
43
+ _upload_routes()
44
+ assert client.upload(MP4, filename="clip.mp4") == UPLOAD_ID
45
+
46
+ start, put = responses.calls[0].request, responses.calls[1].request
47
+ assert start.headers["Authorization"] == f"Bearer {API_KEY}"
48
+ assert put.body == MP4
49
+ assert put.headers["Content-Type"] == "video/mp4"
50
+ assert "Authorization" not in put.headers # the signed URL is the credential
51
+
52
+
53
+ @responses.activate
54
+ def test_mark_sends_video_through_an_upload_and_defaults_to_url(client):
55
+ _upload_routes()
56
+ responses.add(responses.POST, f"{BASE_URL}/mark", json=MARKED_URL_BODY, status=200)
57
+
58
+ result = client.mark(MP4, filename="clip.mp4", generator_name="kling")
59
+
60
+ assert result.kind == "video"
61
+ assert result.file_url == "https://storage.example/signed"
62
+ assert result.calls_used == 6
63
+ body = json.loads(responses.calls[2].request.body)
64
+ assert body == {"upload_id": UPLOAD_ID, "generator_name": "kling", "response_format": "url"}
65
+
66
+
67
+ @responses.activate
68
+ def test_video_is_sniffed_even_without_a_video_filename(client):
69
+ _upload_routes()
70
+ responses.add(responses.POST, f"{BASE_URL}/mark", json=MARKED_URL_BODY, status=200)
71
+ client.mark(MP4, filename="render.bin")
72
+ assert [c.request.method for c in responses.calls] == ["POST", "PUT", "POST"]
73
+
74
+
75
+ @responses.activate
76
+ def test_large_images_go_through_an_upload_too(client):
77
+ _upload_routes()
78
+ responses.add(responses.POST, f"{BASE_URL}/mark", json={**MARKED_URL_BODY, "kind": "image", "calls_used": 1}, status=200)
79
+ big = b"\x89PNG\r\n\x1a\n" + b"\x00" * (INLINE_UPLOAD_LIMIT + 1)
80
+
81
+ result = client.mark(big, filename="big.png")
82
+
83
+ assert result.calls_used == 1
84
+ assert json.loads(responses.calls[2].request.body)["response_format"] == "url"
85
+
86
+
87
+ @responses.activate
88
+ def test_small_images_stay_inline_and_read_x_calls_used(client):
89
+ responses.add(
90
+ responses.POST,
91
+ f"{BASE_URL}/mark",
92
+ body=b"\x00",
93
+ status=200,
94
+ content_type="image/png",
95
+ headers={"x-asset-id": "a", "x-watermark-code": "b", "x-verify-url": "c", "x-calls-used": "1"},
96
+ )
97
+ result = client.mark(b"\x89PNG\r\n\x1a\n\x00\x00\x00\x00", filename="a.png")
98
+ assert result.calls_used == 1
99
+ assert len(responses.calls) == 1
100
+
101
+
102
+ @responses.activate
103
+ def test_mark_by_url_and_by_upload_id(client):
104
+ responses.add(responses.POST, f"{BASE_URL}/mark", json=MARKED_URL_BODY, status=200)
105
+ responses.add(responses.POST, f"{BASE_URL}/mark", json=MARKED_URL_BODY, status=200)
106
+
107
+ client.mark(url="https://cdn.example.com/clip.mp4", response_format="url")
108
+ client.mark(upload_id=UPLOAD_ID)
109
+
110
+ assert json.loads(responses.calls[0].request.body) == {"url": "https://cdn.example.com/clip.mp4", "response_format": "url"}
111
+ assert json.loads(responses.calls[1].request.body) == {"upload_id": UPLOAD_ID, "response_format": "url"}
112
+
113
+
114
+ def test_mark_needs_exactly_one_source(client):
115
+ with pytest.raises(ValueError, match="exactly one"):
116
+ client.mark()
117
+ with pytest.raises(ValueError, match="exactly one"):
118
+ client.mark(b"x", filename="a.png", url="https://x")
119
+
120
+
121
+ @responses.activate
122
+ def test_uploads_not_live_yet_gives_a_clear_error(client):
123
+ responses.add(responses.POST, f"{BASE_URL}/uploads", json={"error": "Not found."}, status=404)
124
+ with pytest.raises(MarkproofError, match="aren't available") as err:
125
+ client.mark(MP4, filename="clip.mp4")
126
+ assert err.value.status == 404
127
+
128
+
129
+ @responses.activate
130
+ def test_a_failed_put_raises(client):
131
+ responses.add(responses.POST, f"{BASE_URL}/uploads", json={"upload_id": UPLOAD_ID, "upload_url": UPLOAD_URL}, status=201)
132
+ responses.add(responses.PUT, UPLOAD_URL, status=400, body="too big")
133
+ with pytest.raises(MarkproofError, match="Upload failed"):
134
+ client.upload(MP4, filename="clip.mp4")
135
+
136
+
137
+ @responses.activate
138
+ def test_verify_uploads_video_and_verifies_by_id(client):
139
+ _upload_routes()
140
+ responses.add(
141
+ responses.POST,
142
+ f"{BASE_URL}/verify",
143
+ json={
144
+ "watermark": {"detected": True, "watermark_code": "ABC12345", "confidence": 0.98},
145
+ "c2pa": {"present": False, "signature_valid": None, "trusted": None, "manifest_label": None},
146
+ "matched_asset": None,
147
+ "summary": "Watermark detected code ABC12345.",
148
+ },
149
+ status=200,
150
+ )
151
+ result = client.verify(MP4, filename="clip.mp4")
152
+ assert result.watermark.watermark_code == "ABC12345"
153
+ assert json.loads(responses.calls[2].request.body) == {"upload_id": UPLOAD_ID}
File without changes
File without changes
File without changes