asindotdev 0.1.0__py3-none-any.whl

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.
asin/_operations.py ADDED
@@ -0,0 +1,616 @@
1
+ """Generated from the Asin OpenAPI document. Do not edit by hand."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from typing import Any, Literal, cast
6
+
7
+ from .models import APIResponse, JsonResponse, RequestOptions
8
+
9
+
10
+ class _SyncOperations:
11
+ """Generated Asin endpoint methods."""
12
+
13
+ __slots__ = ()
14
+
15
+ def _call(
16
+ self, name: str, params: dict[str, Any], options: RequestOptions | None
17
+ ) -> APIResponse:
18
+ raise NotImplementedError
19
+
20
+ def check_product(
21
+ self,
22
+ *,
23
+ id: str,
24
+ wait: bool | None = None,
25
+ request_options: RequestOptions | None = None,
26
+ ) -> JsonResponse:
27
+ """Check the price now. Asks for a check right away (1 credit), at most once per 5 minutes; earlier calls return the product unchanged. A paused product has to be resumed first. The check runs in the background; with wait=true the answer waits for it. Read last_e…
28
+
29
+ ``POST /products/{id}/check``
30
+
31
+ Args:
32
+ id: product_id returned when the product was added
33
+ wait: Answer once the check is done (up to 45 seconds)
34
+ """
35
+ return cast(
36
+ JsonResponse, self._call("check_product", {"id": id, "wait": wait}, request_options)
37
+ )
38
+
39
+ def delete_product(
40
+ self,
41
+ *,
42
+ id: str,
43
+ request_options: RequestOptions | None = None,
44
+ ) -> JsonResponse:
45
+ """Stop tracking a product. Removes the product and its price history.
46
+
47
+ ``DELETE /products/{id}``
48
+
49
+ Args:
50
+ id: product_id returned when the product was added
51
+ """
52
+ return cast(JsonResponse, self._call("delete_product", {"id": id}, request_options))
53
+
54
+ def fetch_offers(
55
+ self,
56
+ *,
57
+ asin: str,
58
+ marketplace: Literal[
59
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
60
+ ]
61
+ | str
62
+ | None = None,
63
+ refresh: bool | None = None,
64
+ request_options: RequestOptions | None = None,
65
+ ) -> JsonResponse:
66
+ """Read every offer from Amazon. Reads the offers on Amazon for 1 credit, refunded if it doesn't answer. A read made in the last 3 hours, by anyone, is returned instead, for free (charged: false). The answer is the same as GET /offers.
67
+
68
+ ``POST /offers``
69
+
70
+ Args:
71
+ asin: 10-character ASIN
72
+ marketplace: Amazon store, e.g. com for amazon.com
73
+ refresh: Read Amazon again unless the last read is under 5 minutes old (1 credit)
74
+ """
75
+ return cast(
76
+ JsonResponse,
77
+ self._call(
78
+ "fetch_offers",
79
+ {"asin": asin, "marketplace": marketplace, "refresh": refresh},
80
+ request_options,
81
+ ),
82
+ )
83
+
84
+ def get_account(
85
+ self,
86
+ *,
87
+ request_options: RequestOptions | None = None,
88
+ ) -> JsonResponse:
89
+ """Plan and remaining credits. Your plan, the credits left (monthly allowance and packs), how many products you track and the credits they use per month.
90
+
91
+ ``GET /account``
92
+ """
93
+ return cast(JsonResponse, self._call("get_account", {}, request_options))
94
+
95
+ def get_asin_history(
96
+ self,
97
+ *,
98
+ asin: str,
99
+ marketplace: Literal[
100
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
101
+ ]
102
+ | str
103
+ | None = None,
104
+ days: int | None = None,
105
+ request_options: RequestOptions | None = None,
106
+ ) -> JsonResponse:
107
+ """Price history of any ASIN. Every price Asin read for an ASIN on a marketplace over the period, whoever tracks it, oldest first, with the lowest, highest and average of those prices. A history of more than 5000 points is thinned evenly, keeping its first and last poi…
108
+
109
+ ``GET /history``
110
+
111
+ Args:
112
+ asin: 10-character ASIN
113
+ marketplace: Amazon store, e.g. com for amazon.com
114
+ days: History period in days
115
+ """
116
+ return cast(
117
+ JsonResponse,
118
+ self._call(
119
+ "get_asin_history",
120
+ {"asin": asin, "marketplace": marketplace, "days": days},
121
+ request_options,
122
+ ),
123
+ )
124
+
125
+ def get_offers(
126
+ self,
127
+ *,
128
+ asin: str,
129
+ marketplace: Literal[
130
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
131
+ ]
132
+ | str
133
+ | None = None,
134
+ request_options: RequestOptions | None = None,
135
+ ) -> JsonResponse:
136
+ """Offers already known. Who sells the product and at what price, as last read, cheapest first, with Amazon's own price and the cheapest new and used offers (used is only known for Amazon's own outlet). Free: this never reads Amazon or spends a credit. When nobody…
137
+
138
+ ``GET /offers``
139
+
140
+ Args:
141
+ asin: 10-character ASIN
142
+ marketplace: Amazon store, e.g. com for amazon.com
143
+ """
144
+ return cast(
145
+ JsonResponse,
146
+ self._call("get_offers", {"asin": asin, "marketplace": marketplace}, request_options),
147
+ )
148
+
149
+ def get_product(
150
+ self,
151
+ *,
152
+ id: str,
153
+ days: int | None = None,
154
+ request_options: RequestOptions | None = None,
155
+ ) -> JsonResponse:
156
+ """Get a product and its price history. The product's latest state and every price read of the period, oldest first. The history covers every read of this ASIN on this marketplace, including those made before you added it, so a product added today can come with months of prices;…
157
+
158
+ ``GET /products/{id}``
159
+
160
+ Args:
161
+ id: product_id returned when the product was added
162
+ days: History period in days
163
+ """
164
+ return cast(
165
+ JsonResponse, self._call("get_product", {"id": id, "days": days}, request_options)
166
+ )
167
+
168
+ def list_products(
169
+ self,
170
+ *,
171
+ asin: str | None = None,
172
+ marketplace: Literal[
173
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
174
+ ]
175
+ | str
176
+ | None = None,
177
+ filter: Literal["all", "drops", "target", "out_of_stock", "attention", "paused"]
178
+ | str
179
+ | None = None,
180
+ page: int | None = None,
181
+ limit: int | None = None,
182
+ request_options: RequestOptions | None = None,
183
+ ) -> JsonResponse:
184
+ """List tracked products. Your watchlist, newest first, with each product's latest state and its recent prices (trend). Filter with asin (and marketplace) to know if a product is tracked, or with filter to get the same lists as the app's tabs.
185
+
186
+ ``GET /products``
187
+
188
+ Args:
189
+ asin: Only this ASIN
190
+ marketplace: Only this Amazon store
191
+ filter: Only the products of one of the app's watchlist filters. all: Every tracked product (default). drops: Cheaper now than when the product was added. target: At or below its target price. out_of_stock:…
192
+ page: Page number
193
+ limit: Products per page
194
+ """
195
+ return cast(
196
+ JsonResponse,
197
+ self._call(
198
+ "list_products",
199
+ {
200
+ "asin": asin,
201
+ "marketplace": marketplace,
202
+ "filter": filter,
203
+ "page": page,
204
+ "limit": limit,
205
+ },
206
+ request_options,
207
+ ),
208
+ )
209
+
210
+ def search_amazon(
211
+ self,
212
+ *,
213
+ query: str,
214
+ marketplace: Literal[
215
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
216
+ ]
217
+ | str
218
+ | None = None,
219
+ request_options: RequestOptions | None = None,
220
+ ) -> JsonResponse:
221
+ """Search Amazon. Amazon search results (up to 60, sponsored included), to find the ASIN of a product. Free, limited to 30 searches per hour.
222
+
223
+ ``GET /search``
224
+
225
+ Args:
226
+ query: Keywords
227
+ marketplace: Amazon store, e.g. com for amazon.com
228
+ """
229
+ return cast(
230
+ JsonResponse,
231
+ self._call(
232
+ "search_amazon", {"query": query, "marketplace": marketplace}, request_options
233
+ ),
234
+ )
235
+
236
+ def track_product(
237
+ self,
238
+ *,
239
+ product: str,
240
+ marketplace: Literal[
241
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
242
+ ]
243
+ | str
244
+ | None = None,
245
+ target_price: int | float | None = None,
246
+ interval_minutes: int | None = None,
247
+ wait: bool | None = None,
248
+ request_options: RequestOptions | None = None,
249
+ ) -> JsonResponse:
250
+ """Track a product. Adds an ASIN, an Amazon link or a short link (amzn.to, a.co…) to your watchlist. The first check runs in the background within seconds (checking is true until then): poll GET /products/{id}, or send wait: true to get the answer once it's d…
251
+
252
+ ``POST /products``
253
+
254
+ Args:
255
+ product: ASIN (B09DFCB66S) or Amazon product link
256
+ marketplace: Amazon store, e.g. com for amazon.com. A product link sets it on its own.
257
+ target_price: Email the owner when the price goes at or below this amount. null removes it.
258
+ interval_minutes: Minutes between two checks (1440 = daily). Checks faster than daily need a plan or a credit pack: every 60 minutes with a pack or Starter, 30 with Pro, 5 with Business.
259
+ wait: Answer once the first check is done (up to 45 seconds)
260
+ """
261
+ return cast(
262
+ JsonResponse,
263
+ self._call(
264
+ "track_product",
265
+ {
266
+ "product": product,
267
+ "marketplace": marketplace,
268
+ "target_price": target_price,
269
+ "interval_minutes": interval_minutes,
270
+ "wait": wait,
271
+ },
272
+ request_options,
273
+ ),
274
+ )
275
+
276
+ def update_product(
277
+ self,
278
+ *,
279
+ id: str,
280
+ target_price: int | float | None = None,
281
+ interval_minutes: int | None = None,
282
+ alert_on_drop: bool | None = None,
283
+ paused: bool | None = None,
284
+ request_options: RequestOptions | None = None,
285
+ ) -> JsonResponse:
286
+ """Change alerts and checks. Target price, drop alerts, check interval, or pause. Send only what changes.
287
+
288
+ ``PATCH /products/{id}``
289
+
290
+ Args:
291
+ id: product_id returned when the product was added
292
+ target_price: Email the owner when the price goes at or below this amount. null removes it.
293
+ interval_minutes: Minutes between two checks (1440 = daily). Checks faster than daily need a plan or a credit pack: every 60 minutes with a pack or Starter, 30 with Pro, 5 with Business.
294
+ alert_on_drop: Email when the price drops or the product is back in stock
295
+ paused: Stop checking (and charging) for now; false resumes
296
+ """
297
+ return cast(
298
+ JsonResponse,
299
+ self._call(
300
+ "update_product",
301
+ {
302
+ "id": id,
303
+ "target_price": target_price,
304
+ "interval_minutes": interval_minutes,
305
+ "alert_on_drop": alert_on_drop,
306
+ "paused": paused,
307
+ },
308
+ request_options,
309
+ ),
310
+ )
311
+
312
+
313
+ class _AsyncOperations:
314
+ """Generated Asin endpoint methods."""
315
+
316
+ __slots__ = ()
317
+
318
+ async def _call(
319
+ self, name: str, params: dict[str, Any], options: RequestOptions | None
320
+ ) -> APIResponse:
321
+ raise NotImplementedError
322
+
323
+ async def check_product(
324
+ self,
325
+ *,
326
+ id: str,
327
+ wait: bool | None = None,
328
+ request_options: RequestOptions | None = None,
329
+ ) -> JsonResponse:
330
+ """Check the price now. Asks for a check right away (1 credit), at most once per 5 minutes; earlier calls return the product unchanged. A paused product has to be resumed first. The check runs in the background; with wait=true the answer waits for it. Read last_e…
331
+
332
+ ``POST /products/{id}/check``
333
+
334
+ Args:
335
+ id: product_id returned when the product was added
336
+ wait: Answer once the check is done (up to 45 seconds)
337
+ """
338
+ return cast(
339
+ JsonResponse,
340
+ await self._call("check_product", {"id": id, "wait": wait}, request_options),
341
+ )
342
+
343
+ async def delete_product(
344
+ self,
345
+ *,
346
+ id: str,
347
+ request_options: RequestOptions | None = None,
348
+ ) -> JsonResponse:
349
+ """Stop tracking a product. Removes the product and its price history.
350
+
351
+ ``DELETE /products/{id}``
352
+
353
+ Args:
354
+ id: product_id returned when the product was added
355
+ """
356
+ return cast(JsonResponse, await self._call("delete_product", {"id": id}, request_options))
357
+
358
+ async def fetch_offers(
359
+ self,
360
+ *,
361
+ asin: str,
362
+ marketplace: Literal[
363
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
364
+ ]
365
+ | str
366
+ | None = None,
367
+ refresh: bool | None = None,
368
+ request_options: RequestOptions | None = None,
369
+ ) -> JsonResponse:
370
+ """Read every offer from Amazon. Reads the offers on Amazon for 1 credit, refunded if it doesn't answer. A read made in the last 3 hours, by anyone, is returned instead, for free (charged: false). The answer is the same as GET /offers.
371
+
372
+ ``POST /offers``
373
+
374
+ Args:
375
+ asin: 10-character ASIN
376
+ marketplace: Amazon store, e.g. com for amazon.com
377
+ refresh: Read Amazon again unless the last read is under 5 minutes old (1 credit)
378
+ """
379
+ return cast(
380
+ JsonResponse,
381
+ await self._call(
382
+ "fetch_offers",
383
+ {"asin": asin, "marketplace": marketplace, "refresh": refresh},
384
+ request_options,
385
+ ),
386
+ )
387
+
388
+ async def get_account(
389
+ self,
390
+ *,
391
+ request_options: RequestOptions | None = None,
392
+ ) -> JsonResponse:
393
+ """Plan and remaining credits. Your plan, the credits left (monthly allowance and packs), how many products you track and the credits they use per month.
394
+
395
+ ``GET /account``
396
+ """
397
+ return cast(JsonResponse, await self._call("get_account", {}, request_options))
398
+
399
+ async def get_asin_history(
400
+ self,
401
+ *,
402
+ asin: str,
403
+ marketplace: Literal[
404
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
405
+ ]
406
+ | str
407
+ | None = None,
408
+ days: int | None = None,
409
+ request_options: RequestOptions | None = None,
410
+ ) -> JsonResponse:
411
+ """Price history of any ASIN. Every price Asin read for an ASIN on a marketplace over the period, whoever tracks it, oldest first, with the lowest, highest and average of those prices. A history of more than 5000 points is thinned evenly, keeping its first and last poi…
412
+
413
+ ``GET /history``
414
+
415
+ Args:
416
+ asin: 10-character ASIN
417
+ marketplace: Amazon store, e.g. com for amazon.com
418
+ days: History period in days
419
+ """
420
+ return cast(
421
+ JsonResponse,
422
+ await self._call(
423
+ "get_asin_history",
424
+ {"asin": asin, "marketplace": marketplace, "days": days},
425
+ request_options,
426
+ ),
427
+ )
428
+
429
+ async def get_offers(
430
+ self,
431
+ *,
432
+ asin: str,
433
+ marketplace: Literal[
434
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
435
+ ]
436
+ | str
437
+ | None = None,
438
+ request_options: RequestOptions | None = None,
439
+ ) -> JsonResponse:
440
+ """Offers already known. Who sells the product and at what price, as last read, cheapest first, with Amazon's own price and the cheapest new and used offers (used is only known for Amazon's own outlet). Free: this never reads Amazon or spends a credit. When nobody…
441
+
442
+ ``GET /offers``
443
+
444
+ Args:
445
+ asin: 10-character ASIN
446
+ marketplace: Amazon store, e.g. com for amazon.com
447
+ """
448
+ return cast(
449
+ JsonResponse,
450
+ await self._call(
451
+ "get_offers", {"asin": asin, "marketplace": marketplace}, request_options
452
+ ),
453
+ )
454
+
455
+ async def get_product(
456
+ self,
457
+ *,
458
+ id: str,
459
+ days: int | None = None,
460
+ request_options: RequestOptions | None = None,
461
+ ) -> JsonResponse:
462
+ """Get a product and its price history. The product's latest state and every price read of the period, oldest first. The history covers every read of this ASIN on this marketplace, including those made before you added it, so a product added today can come with months of prices;…
463
+
464
+ ``GET /products/{id}``
465
+
466
+ Args:
467
+ id: product_id returned when the product was added
468
+ days: History period in days
469
+ """
470
+ return cast(
471
+ JsonResponse, await self._call("get_product", {"id": id, "days": days}, request_options)
472
+ )
473
+
474
+ async def list_products(
475
+ self,
476
+ *,
477
+ asin: str | None = None,
478
+ marketplace: Literal[
479
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
480
+ ]
481
+ | str
482
+ | None = None,
483
+ filter: Literal["all", "drops", "target", "out_of_stock", "attention", "paused"]
484
+ | str
485
+ | None = None,
486
+ page: int | None = None,
487
+ limit: int | None = None,
488
+ request_options: RequestOptions | None = None,
489
+ ) -> JsonResponse:
490
+ """List tracked products. Your watchlist, newest first, with each product's latest state and its recent prices (trend). Filter with asin (and marketplace) to know if a product is tracked, or with filter to get the same lists as the app's tabs.
491
+
492
+ ``GET /products``
493
+
494
+ Args:
495
+ asin: Only this ASIN
496
+ marketplace: Only this Amazon store
497
+ filter: Only the products of one of the app's watchlist filters. all: Every tracked product (default). drops: Cheaper now than when the product was added. target: At or below its target price. out_of_stock:…
498
+ page: Page number
499
+ limit: Products per page
500
+ """
501
+ return cast(
502
+ JsonResponse,
503
+ await self._call(
504
+ "list_products",
505
+ {
506
+ "asin": asin,
507
+ "marketplace": marketplace,
508
+ "filter": filter,
509
+ "page": page,
510
+ "limit": limit,
511
+ },
512
+ request_options,
513
+ ),
514
+ )
515
+
516
+ async def search_amazon(
517
+ self,
518
+ *,
519
+ query: str,
520
+ marketplace: Literal[
521
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
522
+ ]
523
+ | str
524
+ | None = None,
525
+ request_options: RequestOptions | None = None,
526
+ ) -> JsonResponse:
527
+ """Search Amazon. Amazon search results (up to 60, sponsored included), to find the ASIN of a product. Free, limited to 30 searches per hour.
528
+
529
+ ``GET /search``
530
+
531
+ Args:
532
+ query: Keywords
533
+ marketplace: Amazon store, e.g. com for amazon.com
534
+ """
535
+ return cast(
536
+ JsonResponse,
537
+ await self._call(
538
+ "search_amazon", {"query": query, "marketplace": marketplace}, request_options
539
+ ),
540
+ )
541
+
542
+ async def track_product(
543
+ self,
544
+ *,
545
+ product: str,
546
+ marketplace: Literal[
547
+ "com", "co.uk", "de", "fr", "it", "es", "ca", "co.jp", "com.br", "com.mx", "in"
548
+ ]
549
+ | str
550
+ | None = None,
551
+ target_price: int | float | None = None,
552
+ interval_minutes: int | None = None,
553
+ wait: bool | None = None,
554
+ request_options: RequestOptions | None = None,
555
+ ) -> JsonResponse:
556
+ """Track a product. Adds an ASIN, an Amazon link or a short link (amzn.to, a.co…) to your watchlist. The first check runs in the background within seconds (checking is true until then): poll GET /products/{id}, or send wait: true to get the answer once it's d…
557
+
558
+ ``POST /products``
559
+
560
+ Args:
561
+ product: ASIN (B09DFCB66S) or Amazon product link
562
+ marketplace: Amazon store, e.g. com for amazon.com. A product link sets it on its own.
563
+ target_price: Email the owner when the price goes at or below this amount. null removes it.
564
+ interval_minutes: Minutes between two checks (1440 = daily). Checks faster than daily need a plan or a credit pack: every 60 minutes with a pack or Starter, 30 with Pro, 5 with Business.
565
+ wait: Answer once the first check is done (up to 45 seconds)
566
+ """
567
+ return cast(
568
+ JsonResponse,
569
+ await self._call(
570
+ "track_product",
571
+ {
572
+ "product": product,
573
+ "marketplace": marketplace,
574
+ "target_price": target_price,
575
+ "interval_minutes": interval_minutes,
576
+ "wait": wait,
577
+ },
578
+ request_options,
579
+ ),
580
+ )
581
+
582
+ async def update_product(
583
+ self,
584
+ *,
585
+ id: str,
586
+ target_price: int | float | None = None,
587
+ interval_minutes: int | None = None,
588
+ alert_on_drop: bool | None = None,
589
+ paused: bool | None = None,
590
+ request_options: RequestOptions | None = None,
591
+ ) -> JsonResponse:
592
+ """Change alerts and checks. Target price, drop alerts, check interval, or pause. Send only what changes.
593
+
594
+ ``PATCH /products/{id}``
595
+
596
+ Args:
597
+ id: product_id returned when the product was added
598
+ target_price: Email the owner when the price goes at or below this amount. null removes it.
599
+ interval_minutes: Minutes between two checks (1440 = daily). Checks faster than daily need a plan or a credit pack: every 60 minutes with a pack or Starter, 30 with Pro, 5 with Business.
600
+ alert_on_drop: Email when the price drops or the product is back in stock
601
+ paused: Stop checking (and charging) for now; false resumes
602
+ """
603
+ return cast(
604
+ JsonResponse,
605
+ await self._call(
606
+ "update_product",
607
+ {
608
+ "id": id,
609
+ "target_price": target_price,
610
+ "interval_minutes": interval_minutes,
611
+ "alert_on_drop": alert_on_drop,
612
+ "paused": paused,
613
+ },
614
+ request_options,
615
+ ),
616
+ )
asin/_response.py ADDED
@@ -0,0 +1,86 @@
1
+ """Buffered HTTP response preservation and decoding."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from types import MappingProxyType
7
+ from typing import Any, Literal
8
+
9
+ import httpx
10
+
11
+ from .errors import AsinAPIError, AsinResponseDecodingError
12
+ from .models import APIResponse, BinaryResponse, JsonResponse, RawResponse
13
+
14
+ _ERROR_MESSAGE_KEYS = ("error", "message", "error_message", "detail")
15
+
16
+
17
+ def create_raw_response(response: httpx.Response, body: bytes) -> RawResponse:
18
+ grouped: dict[str, list[str]] = {}
19
+ for name, value in response.headers.multi_items():
20
+ grouped.setdefault(name.lower(), []).append(value)
21
+ return RawResponse(
22
+ status=response.status_code,
23
+ status_text=response.reason_phrase,
24
+ headers=MappingProxyType({name: tuple(values) for name, values in grouped.items()}),
25
+ content_type=response.headers.get("content-type"),
26
+ body=body,
27
+ )
28
+
29
+
30
+ def create_api_error(raw_response: RawResponse) -> AsinAPIError:
31
+ parsed, value = _try_parse_json(raw_response.body)
32
+ message = None
33
+ if parsed and isinstance(value, dict):
34
+ # The API answers {"error": {"code", "message"}}.
35
+ nested = value.get("error")
36
+ if isinstance(nested, dict) and isinstance(nested.get("message"), str):
37
+ message = nested["message"]
38
+ for key in _ERROR_MESSAGE_KEYS:
39
+ candidate = value.get(key)
40
+ if isinstance(candidate, str) and candidate:
41
+ message = candidate
42
+ break
43
+ return AsinAPIError(
44
+ raw_response=raw_response,
45
+ response_body=value if parsed else None,
46
+ error_message=message,
47
+ )
48
+
49
+
50
+ def decode_success_response(
51
+ raw_response: RawResponse, expected: Literal["json", "any"]
52
+ ) -> APIResponse:
53
+ if not _is_json_media_type(raw_response.content_type):
54
+ if expected == "json":
55
+ raise AsinResponseDecodingError(
56
+ "Asin returned a successful non-JSON response for a JSON operation.",
57
+ raw_response,
58
+ TypeError(f"Unexpected content type: {raw_response.content_type}."),
59
+ )
60
+ return BinaryResponse(
61
+ bytes=raw_response.body,
62
+ content_type=raw_response.content_type,
63
+ raw_response=raw_response,
64
+ )
65
+ parsed, value = _try_parse_json(raw_response.body)
66
+ if not parsed:
67
+ raise AsinResponseDecodingError(
68
+ "Asin returned a successful JSON response that could not be decoded.",
69
+ raw_response,
70
+ value if isinstance(value, Exception) else None,
71
+ )
72
+ return JsonResponse(data=value, raw_response=raw_response)
73
+
74
+
75
+ def _is_json_media_type(content_type: str | None) -> bool:
76
+ if content_type is None:
77
+ return False
78
+ media_type = content_type.split(";", 1)[0].strip().lower()
79
+ return media_type == "application/json" or media_type.endswith("+json")
80
+
81
+
82
+ def _try_parse_json(body: bytes) -> tuple[bool, Any]:
83
+ try:
84
+ return True, json.loads(body)
85
+ except (UnicodeDecodeError, json.JSONDecodeError) as cause:
86
+ return False, cause