GET endpoint looking up ISBNs and B* ASINs via the affiliate server. URL Parameters: - high_priority='true' or 'false': whether to wait and return result. - stage_import='true' or 'false': whether to stage result for import. By default this is
(self, identifier: str)
| 529 | |
| 530 | class Submit: |
| 531 | def GET(self, identifier: str) -> str: |
| 532 | """ |
| 533 | GET endpoint looking up ISBNs and B* ASINs via the affiliate server. |
| 534 | |
| 535 | URL Parameters: |
| 536 | - high_priority='true' or 'false': whether to wait and return result. |
| 537 | - stage_import='true' or 'false': whether to stage result for import. |
| 538 | By default this is 'true'. Setting this to 'false' is useful when you |
| 539 | want to return AMZ metadata but don't want to import; therefore |
| 540 | high_priority=true must also be 'true', or this returns nothing and |
| 541 | stages nothing (unless the result is cached). |
| 542 | |
| 543 | If `identifier` is in memcache, then return the `hit` (which is marshalled |
| 544 | into a format appropriate for import on Open Library if `?high_priority=true`). |
| 545 | |
| 546 | By default `stage_import=true`, and results will be staged for import if they have |
| 547 | requisite fields. Disable staging with `stage_import=false`. |
| 548 | |
| 549 | If no hit, then queue the identifier for look up and either attempt to return |
| 550 | a promise as `submitted`, or if `?high_priority=true`, return marshalled data |
| 551 | from the cache. |
| 552 | |
| 553 | `Priority.HIGH` is set when `?high_priority=true` and is the highest priority. |
| 554 | It is used when the caller is waiting for a response with the AMZ data, if |
| 555 | available. See `PrioritizedIdentifier` for more on prioritization. |
| 556 | |
| 557 | NOTE: For this API, "ASINs" are ISBN 10s when valid ISBN 10s, and otherwise |
| 558 | they are Amazon-specific identifiers starting with "B". |
| 559 | """ |
| 560 | # cache could be None if reached before initialized (mypy) |
| 561 | if not web.amazon_api: |
| 562 | return json.dumps({"error": "not_configured"}) |
| 563 | |
| 564 | # Handle URL query parameters. |
| 565 | input = web.input(high_priority=False, stage_import=True) |
| 566 | priority = Priority.HIGH if input.get("high_priority") == "true" else Priority.LOW |
| 567 | stage_import = input.get("stage_import") != "false" |
| 568 | |
| 569 | b_asin, isbn_10, isbn_13 = normalize_identifier(identifier) |
| 570 | key = isbn_10 or b_asin |
| 571 | |
| 572 | # For ISBN 13, conditionally go straight to Google Books. |
| 573 | if not key and isbn_13 and priority == Priority.HIGH and stage_import: |
| 574 | return json.dumps({"status": "success"}) if stage_from_google_books(isbn=isbn_13) else json.dumps({"status": "not found"}) |
| 575 | |
| 576 | if not (key := isbn_10 or b_asin): |
| 577 | return json.dumps({"error": "rejected_isbn", "identifier": identifier}) |
| 578 | |
| 579 | # Cache lookup by isbn_13 or b_asin. If there's a hit return the product to |
| 580 | # the caller. |
| 581 | if product := cache.memcache_cache.get(f"amazon_product_{isbn_13 or b_asin}"): |
| 582 | return json.dumps( |
| 583 | { |
| 584 | "status": "success", |
| 585 | "hit": clean_amazon_metadata_for_load(product), |
| 586 | } |
| 587 | ) |
| 588 |
nothing calls this directly
no test coverage detected