Download platform/harness/amazon_aplus_write.py from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 31.1 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/platform/harness/amazon_aplus_write.py
- Command line
-
hf download hf://spaces/fsanyoto/loopable/platform/harness/amazon_aplus_write.py
-
curl -L -o amazon_aplus_write.py https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/platform/harness/amazon_aplus_write.py
31.1 kB
| """Build confirmed A+ documents and send their approved operations through the listing transport.""" | |
| from __future__ import annotations | |
| import base64 | |
| import copy | |
| import hashlib | |
| import io | |
| import json | |
| import re | |
| import time | |
| import urllib.parse | |
| from harness import amazon_listing_write as listing | |
| BASE = "/aplus/2020-11-01" | |
| RESOURCE = "aplus/2020-11-01/contentDocuments" | |
| UPLOAD_PATH = "/uploads/2020-11-01/uploadDestinations/" + RESOURCE | |
| OPERATIONS = { | |
| "createUploadDestinationForResource": ("POST", UPLOAD_PATH), | |
| "validateContentDocumentAsinRelations": ("POST", BASE + "/contentAsinValidations"), | |
| "createContentDocument": ("POST", BASE + "/contentDocuments"), | |
| "postContentDocumentAsinRelations": ("POST", BASE + "/contentDocuments/{reference}/asins"), | |
| "postContentDocumentApprovalSubmission": ("POST", BASE + "/contentDocuments/{reference}/approvalSubmissions"), | |
| "searchContentPublishRecords": ("GET", BASE + "/contentPublishRecords"), | |
| "getContentDocument": ("GET", BASE + "/contentDocuments/{reference}"), | |
| } | |
| MODULES = { | |
| "A1": ("Standard", "STANDARD_HEADER_IMAGE_TEXT", "standardHeaderImageText", (970, 600)), | |
| "A2": ("Standard", "STANDARD_FOUR_IMAGE_TEXT", "standardFourImageText", (220, 220)), | |
| "A3": ("Standard", "STANDARD_SINGLE_IMAGE_SPECS_DETAIL", "standardSingleImageSpecsDetail", (300, 300)), | |
| "A4": ("Standard", "STANDARD_COMPARISON_TABLE", "standardComparisonTable", (150, 300)), | |
| "A5": ("Standard", "STANDARD_COMPANY_LOGO", "standardCompanyLogo", (600, 180)), | |
| "STORY": ("BrandStory", "BRAND_STORY_IMAGE_WITH_LOGO", "brandStoryImageWithLogo", (1464, 625)), | |
| } | |
| SHAPES = { | |
| "text": ({"value": ("string", 10000)}, {"value"}), | |
| "paragraph": ({"textList": ("list", "text", 1, 100)}, {"textList"}), | |
| "image": ({"imageId": ("string", 1024), "altText": ("string", 100)}, {"imageId", "altText"}), | |
| "imageText": ({"image": "image", "headline": "text", "body": "paragraph"}, set()), | |
| "textBlock": ({"headline": "text", "body": "paragraph"}, set()), | |
| "textItem": ({"position": ("integer", 100), "text": "text"}, {"position", "text"}), | |
| "plainItem": ({"position": ("integer", 100), "value": ("string", 250)}, {"position", "value"}), | |
| "textList": ({"textList": ("list", "textItem", 0, 8)}, {"textList"}), | |
| "headerList": ({"headline": "text", "block": "textList"}, set()), | |
| "product": ({"position": ("integer", 6), "image": "image", "title": ("string", 80), | |
| "asin": ("asin",), "highlight": ("boolean",), "metrics": ("list", "plainItem", 0, 10)}, {"position"}), | |
| "standardHeaderImageText": ({"headline": "text", "block": "imageText"}, {"block"}), | |
| "standardFourImageText": ({"headline": "text", **{"block" + str(i): "imageText" for i in range(1, 5)}}, | |
| {"block" + str(i) for i in range(1, 5)}), | |
| "standardSingleImageSpecsDetail": ({"headline": "text", "image": "image", "descriptionHeadline": "text", | |
| "descriptionBlock1": "textBlock", "descriptionBlock2": "textBlock", "specificationHeadline": "text", | |
| "specificationListBlock": "headerList", "specificationTextBlock": "textBlock"}, {"image"}), | |
| "standardComparisonTable": ({"productColumns": ("list", "product", 1, 6), | |
| "metricRowLabels": ("list", "plainItem", 0, 10)}, {"productColumns"}), | |
| "standardCompanyLogo": ({"companyLogo": "image"}, {"companyLogo"}), | |
| "brandStoryImageWithLogo": ({"title": "text", "description": "paragraph", "desktopImage": "image", | |
| "mobileImage": "image"}, {"desktopImage", "mobileImage"}), | |
| } | |
| STATUS_TTL = 3600 | |
| Failure = listing.ListingWriteFailure | |
| def _asins(values): | |
| if (not isinstance(values, (list, tuple)) or not values | |
| or any(not isinstance(v, str) or not re.fullmatch(r"[A-Z0-9]{10}", v) for v in values)): | |
| raise Failure("aplus", "InvalidAsinSet") | |
| return sorted(set(values)) | |
| def _check_shape(value, rule): | |
| if isinstance(rule, str): | |
| fields, required = SHAPES[rule] | |
| if not isinstance(value, dict) or set(value) - fields.keys() or not required <= value.keys(): | |
| raise Failure("aplus", "InvalidModuleShape") | |
| for key, item in value.items(): | |
| _check_shape(item, fields[key]) | |
| return | |
| kind = rule[0] | |
| if kind == "list": | |
| if not isinstance(value, list) or not rule[2] <= len(value) <= rule[3]: | |
| raise Failure("aplus", "InvalidModuleList") | |
| for item in value: | |
| _check_shape(item, rule[1]) | |
| positions = [item["position"] for item in value if isinstance(item, dict) and "position" in item] | |
| valid = len(positions) == len(set(positions)) | |
| elif kind == "string": | |
| valid = listing._text(value, rule[1]) | |
| elif kind == "integer": | |
| valid = type(value) is int and 1 <= value <= rule[1] | |
| elif kind == "boolean": | |
| valid = type(value) is bool | |
| else: | |
| valid = bool(_asins([value])) | |
| if not valid: | |
| raise Failure("aplus", "InvalidModuleValue") | |
| def operation_request(market, operation, reference, asin, page_token, body, asins, content_md5, content_type): | |
| """Encode only the named model parameters, including token-only search continuations.""" | |
| if operation not in OPERATIONS: | |
| raise Failure(operation, "OperationRefused") | |
| method, path = OPERATIONS[operation] | |
| args = {k: v for k, v in {"reference": reference, "asin": asin, "page_token": page_token, | |
| "body": body, "asins": asins, "content_md5": content_md5, "content_type": content_type}.items() | |
| if v is not None} | |
| params = {"marketplaceId": market} | |
| expected = set() | |
| if "{reference}" in path: | |
| if not listing._text(reference) or reference in {".", ".."}: | |
| raise Failure(operation, "ReferenceRequired") | |
| path = path.format(reference=urllib.parse.quote(reference, safe="")) | |
| expected.add("reference") | |
| if operation == "createUploadDestinationForResource": | |
| expected = {"content_md5", "content_type"} | |
| if (not listing._text(content_md5) or content_type not in {"image/png", "image/jpeg"} | |
| or not re.fullmatch(r"[A-Za-z0-9+/]{22}==", content_md5)): | |
| raise Failure(operation, "InvalidImageDigestOrType") | |
| params = {"marketplaceIds": market, "contentMD5": content_md5, "contentType": content_type} | |
| elif operation == "searchContentPublishRecords": | |
| if page_token is not None: | |
| if not listing._text(page_token, listing._MAX_BYTES): | |
| raise Failure(operation, "InvalidPageToken") | |
| params, expected = {"pageToken": page_token}, {"page_token"} | |
| else: | |
| params["asin"], expected = _asins([asin])[0], {"asin"} | |
| elif operation == "getContentDocument": | |
| params["includedDataSet"] = "CONTENTS,METADATA" | |
| elif operation in {"createContentDocument", "validateContentDocumentAsinRelations"}: | |
| if not isinstance(body, dict) or set(body) != {"contentDocument"}: | |
| raise Failure(operation, "ContentDocumentRequired") | |
| expected.add("body") | |
| if operation.startswith("validate"): | |
| params["asinSet"] = ",".join(_asins(asins)) | |
| expected.add("asins") | |
| elif operation == "postContentDocumentAsinRelations": | |
| if not isinstance(body, dict) or set(body) != {"asinSet"} or body["asinSet"] != _asins(body["asinSet"]): | |
| raise Failure(operation, "InvalidAsinSet") | |
| expected.add("body") | |
| if set(args) != expected: | |
| raise Failure(operation, "ParametersRefused") | |
| return path, params, method | |
| def _s3_url(value): | |
| if not listing._text(value, 32768) or "\\" in value or value != value.strip(): | |
| return False | |
| try: | |
| p = urllib.parse.urlsplit(value) | |
| host = p.hostname or "" | |
| s3 = re.fullmatch(r"(?:[a-z0-9][a-z0-9.-]*\.)?s3(?:[.-][a-z0-9-]+)?\.amazonaws\.com", host) | |
| return bool(s3 and p.scheme == "https" and p.netloc == host and p.port is None | |
| and not p.username and not p.password and not p.fragment and p.path not in {"", "/"}) | |
| except ValueError: | |
| return False | |
| def register_upload(transport, response, digest, mime): | |
| payload = response.get("payload") | |
| if (not isinstance(payload, dict) or response.get("errors") or not _s3_url(payload.get("url")) | |
| or not listing._text(payload.get("uploadDestinationId")) | |
| or payload["uploadDestinationId"] in transport._uploads): | |
| raise Failure("createUploadDestinationForResource", "InvalidUploadDestination") | |
| headers = payload.get("headers", {}) | |
| if (not isinstance(headers, dict) or any(not listing._text(k) or not listing._text(v, 8192) | |
| or k.lower() not in {"content-md5", "x-amz-server-side-encryption", "x-amz-server-side-encryption-aws-kms-key-id"} | |
| for k, v in headers.items()) or any(k.lower() == "content-md5" and v != digest for k, v in headers.items())): | |
| raise Failure("createUploadDestinationForResource", "UploadHeadersRefused") | |
| transport._uploads[payload["uploadDestinationId"]] = { | |
| **copy.deepcopy(payload), "digest": digest, "mime": mime, "created_at": time.time(), "used": False} | |
| def upload_request(transport, destination_id, content): | |
| """Resolve an upload capability from an admitted provider reply without accepting a caller URL.""" | |
| cap = transport._uploads.get(destination_id) | |
| if (not cap or cap["used"] or not 0 <= time.time() - cap["created_at"] < 900 | |
| or type(content) is not bytes or _md5(content) != cap["digest"] or not _s3_url(cap["url"])): | |
| raise Failure("uploadAplusImage", "UploadCapabilityRequired") | |
| parts = urllib.parse.urlsplit(cap["url"]) | |
| fields = urllib.parse.parse_qsl(parts.query, keep_blank_values=True) | |
| boundary = "aplus" + listing._nonce() | |
| chunks = [] | |
| for key, value in fields: | |
| if not listing._text(key) or any(c in key for c in '"\\'): | |
| raise Failure("uploadAplusImage", "InvalidUploadFields") | |
| chunks.append(f'--{boundary}\r\nContent-Disposition: form-data; name="{key}"\r\n\r\n{value}\r\n'.encode()) | |
| chunks.extend([f'--{boundary}\r\nContent-Disposition: form-data; name="FILE"; filename="image"\r\n' | |
| f'Content-Type: {cap["mime"]}\r\n\r\n'.encode(), content, | |
| f'\r\n--{boundary}--\r\n'.encode()]) | |
| headers = {"Content-MD5": cap["digest"], **cap.get("headers", {}), | |
| "Content-Type": "multipart/form-data; boundary=" + boundary} | |
| cap["used"] = True | |
| return urllib.parse.urlunsplit((parts.scheme, parts.netloc, parts.path, "", "")), b"".join(chunks), headers, cap | |
| def guard_request(transport, operation, request, binding): | |
| p = urllib.parse.urlsplit(request.full_url) | |
| params = urllib.parse.parse_qs(p.query, keep_blank_values=True) | |
| if operation == "uploadAplusImage": | |
| cap = binding[5] | |
| destination = urllib.parse.urlsplit(cap["url"]) if cap is not None else None | |
| expected_url = urllib.parse.urlunsplit((destination.scheme, destination.netloc, destination.path, "", "")) if destination else None | |
| allowed = (cap is not None and any(cap is v for v in transport._uploads.values()) | |
| and cap["used"] and _s3_url(request.full_url) and request.method == "POST" | |
| and request.full_url == expected_url and request.get_header("Content-md5") == cap["digest"] | |
| and request.get_header("Content-type", "").startswith("multipart/form-data; boundary=") | |
| and not p.query and not request.has_header("X-amz-access-token") | |
| and not request.has_header("Authorization") and callable(binding[4])) | |
| else: | |
| allowed = (p.scheme == "https" and p.netloc == urllib.parse.urlsplit(listing.ENDPOINTS[transport.region]).netloc | |
| and not p.fragment and p.port is None and request.get_header("X-amz-access-token") == transport.token | |
| and bool(transport.token) and not request.has_header("Authorization")) | |
| method, pattern = OPERATIONS[operation] | |
| path = re.escape(pattern).replace(re.escape("{reference}"), r"[^/]+") | |
| allowed &= bool(re.fullmatch(path, p.path)) and request.method == method | |
| if operation == "createUploadDestinationForResource": | |
| allowed &= (set(params) == {"marketplaceIds", "contentMD5", "contentType"} | |
| and params["marketplaceIds"] == [transport.marketplace] | |
| and len(params["contentMD5"]) == 1 and bool(re.fullmatch(r"[A-Za-z0-9+/]{22}==", params["contentMD5"][0])) | |
| and params["contentType"] in (["image/png"], ["image/jpeg"])) | |
| elif operation == "searchContentPublishRecords" and set(params) == {"pageToken"}: | |
| allowed &= len(params["pageToken"]) == 1 and listing._text(params["pageToken"][0], listing._MAX_BYTES) | |
| else: | |
| extra = {"getContentDocument": "includedDataSet", "searchContentPublishRecords": "asin", | |
| "validateContentDocumentAsinRelations": "asinSet"}.get(operation) | |
| allowed &= params.get("marketplaceId") == [transport.marketplace] | |
| allowed &= set(params) == ({"marketplaceId", extra} if extra else {"marketplaceId"}) | |
| if extra == "includedDataSet": | |
| allowed &= params.get(extra) == ["CONTENTS,METADATA"] | |
| elif extra in {"asin", "asinSet"}: | |
| values = params.get(extra, []) | |
| allowed &= len(values) == 1 and bool(_asins(values[0].split(","))) | |
| if method == "POST": | |
| allowed &= callable(binding[4]) | |
| if operation in {"getContentDocument", "searchContentPublishRecords", "createUploadDestinationForResource", | |
| "postContentDocumentApprovalSubmission"}: | |
| allowed &= request.data is None | |
| else: | |
| body = json.loads(request.data) | |
| expected = "asinSet" if operation == "postContentDocumentAsinRelations" else "contentDocument" | |
| allowed &= isinstance(body, dict) and set(body) == {expected} | |
| if not allowed: | |
| raise Failure(operation, "RequestRefused") | |
| def _md5(content): | |
| return base64.b64encode(hashlib.md5(content, usedforsecurity=False).digest()).decode("ascii") | |
| def _image_info(content): | |
| from PIL import Image, UnidentifiedImageError | |
| if type(content) is not bytes or not content or len(content) > listing._MAX_BYTES: | |
| raise Failure("aplus", "InvalidImage") | |
| try: | |
| with Image.open(io.BytesIO(content)) as image: | |
| width, height = image.size | |
| mime = {"PNG": "image/png", "JPEG": "image/jpeg"}.get(image.format) | |
| image.verify() | |
| except (OSError, ValueError, SyntaxError, UnidentifiedImageError, Image.DecompressionBombError): | |
| raise Failure("aplus", "InvalidImage") from None | |
| if mime is None: | |
| raise Failure("aplus", "ImageTypeRefused") | |
| return {"sha256": hashlib.sha256(content).hexdigest(), "width": width, "height": height, "mime": mime} | |
| def _image_slots(key, payload): | |
| if key == "A1": | |
| return [payload.get("block", {}).get("image")] | |
| if key == "A2": | |
| return [payload.get("block" + str(i), {}).get("image") for i in range(1, 5)] | |
| if key == "A3": | |
| return [payload.get("image")] | |
| if key == "A4": | |
| columns = payload.get("productColumns") | |
| if not isinstance(columns, list) or not 1 <= len(columns) <= 6: | |
| raise Failure("aplus", "InvalidComparisonColumns") | |
| return [column.get("image") for column in columns] | |
| if key == "A5": | |
| return [payload.get("companyLogo")] | |
| return [payload.get("desktopImage"), payload.get("mobileImage")] | |
| def build_documents(parts, images, *, name, locale): | |
| """Map confirmed prototype parts to Standard EBC and BrandStory provider documents.""" | |
| if (not isinstance(parts, list) or not parts or not isinstance(images, dict) | |
| or not listing._text(name, 100) or not re.fullmatch(r"[a-z]{2}-[A-Z]{2}", str(locale))): | |
| raise Failure("aplus", "InvalidDocument") | |
| documents, manifest, seen = {}, {}, set() | |
| for part in parts: | |
| if not isinstance(part, dict) or set(part) != {"key", "confirmed", "module"}: | |
| raise Failure("aplus", "InvalidPart") | |
| key = part["key"] | |
| if not isinstance(key, str) or key not in MODULES or key in seen or part["confirmed"] is not True: | |
| raise Failure("aplus", "ConfirmedPartRequired") | |
| seen.add(key) | |
| kind, module_type, field, minimum = MODULES[key] | |
| if len(name + " " + kind) > 100: | |
| raise Failure("aplus", "DocumentNameTooLong") | |
| module = copy.deepcopy(part["module"]) | |
| if (not isinstance(module, dict) or set(module) != {"contentModuleType", field} | |
| or module["contentModuleType"] != module_type or not isinstance(module[field], dict)): | |
| raise Failure("aplus", "ModuleTypeRefused") | |
| _check_shape(module[field], field) | |
| slots = _image_slots(key, module[field]) | |
| for index, slot in enumerate(slots): | |
| if (not isinstance(slot, dict) or set(slot) != {"imageId", "altText"} | |
| or not listing._text(slot["altText"], 100) or not listing._text(slot["imageId"]) | |
| or slot["imageId"] not in images): | |
| raise Failure("aplus", "ImageReferenceRequired") | |
| image_id = slot["imageId"] | |
| info = manifest.setdefault(image_id, _image_info(images[image_id])) | |
| width, height = (463, 625) if key == "STORY" and index == 1 else minimum | |
| if info["width"] < width or info["height"] < height: | |
| raise Failure("aplus", "ImageBelowModuleMinimum") | |
| if key == "STORY" and len(module[field].get("title", {}).get("value", "")) > 30: | |
| raise Failure("aplus", "StoryTitleTooLong") | |
| document = documents.setdefault(kind, {"name": name + " " + kind, | |
| "contentType": "EBC" if kind == "Standard" else "BrandStory", "locale": locale, "contentModuleList": []}) | |
| document["contentModuleList"].append(module) | |
| if set(images) != set(manifest): | |
| raise Failure("aplus", "UnboundImage") | |
| return documents, manifest | |
| def _state(document): | |
| value = document.setdefault("aplus", {"receipts": {}, "status": {}}) | |
| if not isinstance(value, dict) or any(not isinstance(value.get(k), dict) for k in ("receipts", "status")): | |
| raise Failure("aplus", "InvalidTenantState") | |
| return value | |
| def _materialize(value, destinations, manifest): | |
| if isinstance(value, list): | |
| return [_materialize(v, destinations, manifest) for v in value] | |
| if not isinstance(value, dict): | |
| return value | |
| if set(value) == {"imageId", "altText"}: | |
| image_id = value["imageId"] | |
| dimensions = {k: {"value": manifest[image_id][k], "units": "pixels"} for k in ("width", "height")} | |
| return {"uploadDestinationId": destinations[image_id], "altText": value["altText"], | |
| "imageCropSpecification": {"size": dimensions}} | |
| return {k: _materialize(v, destinations, manifest) for k, v in value.items()} | |
| class APlusPublisher: | |
| """Own A+ plans, receipts and hourly memo state within one tenant runtime.""" | |
| def __init__(self, rt, approval_reader, *, connection_id=None, timeout=30): | |
| self.rt, self.tenant_key, self.approval_reader = rt, listing._tenant(rt), approval_reader | |
| self.transport = listing._Transport(rt, connection_id=connection_id, timeout=timeout) | |
| def _scope(self): | |
| t = self.transport | |
| if listing._tenant(self.rt) != self.tenant_key: | |
| raise Failure("aplus", "TenantChanged") | |
| return {"tenant_key": self.tenant_key, "connection_id": t.connection_id, "region": t.region, | |
| "marketplace_id": t.marketplace, "seller_id": t.fields["seller_id"]} | |
| def _receipt(self, receipt_id): | |
| return _state(listing._read(self.rt))["receipts"].get(receipt_id) | |
| def _save(self, receipt, previous): | |
| def save(document): | |
| rows = _state(document)["receipts"] | |
| if rows.get(receipt["id"]) != previous: | |
| raise Failure("aplus", "ReceiptChanged") | |
| if previous is None and any(row["binding"]["workflow"] == receipt["binding"]["workflow"] | |
| and row["state"] in {"pending", "unknown"} for row in rows.values()): | |
| raise Failure("aplus", "ReconciliationRequired") | |
| rows[receipt["id"]] = copy.deepcopy(receipt) | |
| stored = listing._change(self.rt, save) | |
| if _state(stored)["receipts"].get(receipt["id"]) != receipt: | |
| raise Failure("aplus", "PersistenceUnproven") | |
| def _authorize(self, binding, approval_id, receipt=None): | |
| if any(binding.get(k) != v for k, v in self._scope().items()): | |
| raise Failure("aplus", "ScopeChanged") | |
| approved = listing.read_approval(self.rt, self.approval_reader, approval_id, binding) | |
| if receipt is not None and self._receipt(receipt["id"]) != receipt: | |
| raise Failure("aplus", "PendingReceiptRequired") | |
| if not listing.writes_enabled(self.rt): | |
| raise Failure("aplus", "WritesDisabled") | |
| return approved | |
| def _write(self, receipt, operation, **arguments): | |
| previous = copy.deepcopy(receipt) | |
| receipt["operations"].append({"operation": operation, "request": { | |
| k: hashlib.sha256(v).hexdigest() if type(v) is bytes else copy.deepcopy(v) | |
| for k, v in arguments.items()}, "state": "pending"}) | |
| self._save(receipt, previous) | |
| boundary = lambda: self._authorize(receipt["binding"], receipt["approval_id"], receipt) | |
| if operation == "uploadAplusImage": | |
| response = self.transport.upload_aplus_image(boundary=boundary, **arguments) | |
| else: | |
| response = self.transport.aplus(operation, boundary=boundary, **arguments) | |
| if response.get("errors") and operation != "validateContentDocumentAsinRelations": | |
| raise Failure(operation, "ProviderErrors") | |
| previous = copy.deepcopy(receipt) | |
| receipt["operations"][-1].update(state="complete", response=copy.deepcopy(response)) | |
| self._save(receipt, previous) | |
| return response | |
| def _content(self, reference): | |
| scope = {**self._scope(), "contentReferenceKey": reference} | |
| key, token, now = listing._hash(scope), listing._nonce(), time.time() | |
| def reserve(document): | |
| rows = _state(document)["status"] | |
| cached = rows.get(key) | |
| if cached is None or not 0 <= now - cached["at"] < STATUS_TTL: | |
| rows[key] = {"scope": scope, "at": now, "token": token, "state": "pending"} | |
| stored = listing._change(self.rt, reserve) | |
| row = _state(stored)["status"][key] | |
| if row["scope"] != scope: | |
| raise Failure("getContentDocument", "ScopeChanged") | |
| if row["token"] != token: | |
| if row["state"] != "complete": | |
| raise Failure("getContentDocument", "HourlyReadPendingOrUnknown") | |
| return copy.deepcopy(row["response"]) | |
| response = self.transport.aplus("getContentDocument", reference=reference) | |
| record = response.get("contentRecord", {}) | |
| if (response.get("errors") or record.get("contentReferenceKey") != reference | |
| or record.get("contentMetadata", {}).get("marketplaceId") != self.transport.marketplace): | |
| raise Failure("getContentDocument", "InvalidContentRecord") | |
| def finish(document): | |
| rows = _state(document)["status"] | |
| if rows.get(key) != row: | |
| raise Failure("getContentDocument", "MemoChanged") | |
| rows[key] = {**row, "state": "complete", "response": response} | |
| saved = listing._change(self.rt, finish) | |
| if _state(saved)["status"].get(key) != {**row, "state": "complete", "response": response}: | |
| raise Failure("getContentDocument", "PersistenceUnproven") | |
| return copy.deepcopy(response) | |
| def aplus_status(self, reference): | |
| if not listing._text(reference): | |
| raise Failure("getContentDocument", "ReferenceRequired") | |
| if not listing.writes_enabled(self.rt): | |
| return {"state": "disabled", "contentReferenceKey": reference} | |
| response = self._content(reference) | |
| status = response["contentRecord"]["contentMetadata"].get("status") | |
| if status not in {"DRAFT", "SUBMITTED", "APPROVED", "REJECTED"}: | |
| raise Failure("getContentDocument", "UnknownContentStatus") | |
| return {"state": status, "contentReferenceKey": reference, "reasons": copy.deepcopy(response.get("warnings", []))} | |
| def _records(self, asin): | |
| records, token, seen = [], None, set() | |
| while True: | |
| arguments = {"page_token": token} if token else {"asin": asin} | |
| response = self.transport.aplus("searchContentPublishRecords", **arguments) | |
| rows = response.get("publishRecordList") | |
| if (response.get("errors") or not isinstance(rows, list) or any(not isinstance(r, dict) | |
| or r.get("asin") != asin or r.get("marketplaceId") != self.transport.marketplace | |
| or not listing._text(r.get("contentReferenceKey")) for r in rows)): | |
| raise Failure("searchContentPublishRecords", "InvalidPublishRecords") | |
| records.extend(rows) | |
| token = response.get("nextPageToken") | |
| if token is None: | |
| return records | |
| if not listing._text(token, listing._MAX_BYTES) or token in seen: | |
| raise Failure("searchContentPublishRecords", "InvalidPagination") | |
| seen.add(token) | |
| def _snapshot(self, binding): | |
| records = [r for asin in sorted(set(binding["asins"] + (binding["prior_asins"] or []))) | |
| for r in self._records(asin)] | |
| records = [r for r in records if r.get("locale") == binding["locale"] and | |
| r.get("contentType") in {d["contentType"] for d in binding["documents"].values()}] | |
| if records and binding["prior_asins"] is None: | |
| raise Failure("aplus", "PriorAsinInventoryRequired") | |
| workflow_refs = {r["contentReferenceKey"] for r in records if r["asin"] in binding["asins"]} | |
| if any(not any(r["asin"] == asin and r["contentReferenceKey"] in workflow_refs for r in records) | |
| for asin in binding["prior_asins"] or []): | |
| raise Failure("aplus", "PriorAsinRelationUnproven") | |
| snapshots = {ref: self._content(ref) for ref in sorted({r["contentReferenceKey"] for r in records})} | |
| return {"publishRecordList": records, "documents": snapshots} | |
| def _publish_documents(self, receipt, images): | |
| binding, destinations = receipt["binding"], {} | |
| for image_id, info in binding["images"].items(): | |
| response = self._write(receipt, "createUploadDestinationForResource", | |
| content_md5=_md5(images[image_id]), content_type=info["mime"]) | |
| destination = response["payload"]["uploadDestinationId"] | |
| self._write(receipt, "uploadAplusImage", destination_id=destination, content=images[image_id]) | |
| destinations[image_id] = destination | |
| bodies = {kind: {"contentDocument": _materialize(document, destinations, binding["images"])} | |
| for kind, document in binding["documents"].items()} | |
| asins = sorted(set(binding["asins"] + (binding["prior_asins"] or []))) | |
| for body in bodies.values(): | |
| result = self._write(receipt, "validateContentDocumentAsinRelations", body=body, asins=asins) | |
| if not isinstance(result.get("errors"), list) or result["errors"]: | |
| return "invalid" | |
| for kind, body in bodies.items(): | |
| result = self._write(receipt, "createContentDocument", body=body) | |
| reference = result.get("contentReferenceKey") | |
| if not listing._text(reference) or reference in receipt["snapshot"]["documents"]: | |
| raise Failure("createContentDocument", "NewReferenceRequired") | |
| previous = copy.deepcopy(receipt) | |
| receipt["documents"][kind] = reference | |
| self._save(receipt, previous) | |
| self._write(receipt, "postContentDocumentAsinRelations", reference=reference, body={"asinSet": asins}) | |
| self._write(receipt, "postContentDocumentApprovalSubmission", reference=reference) | |
| return "submitted" | |
| def publish_aplus(self, workflow, asins, parts, images, *, name, locale, approval_id, prior_asins=None): | |
| if isinstance(images, dict): | |
| images = images.copy() | |
| documents, manifest = build_documents(parts, images, name=name, locale=locale) | |
| if not listing._text(workflow): | |
| raise Failure("aplus", "WorkflowRequired") | |
| binding = {**self._scope(), "workflow": workflow, "asins": _asins(asins), "locale": locale, | |
| "prior_asins": None if prior_asins is None else _asins(prior_asins), | |
| "documents": documents, "images": manifest} | |
| if not listing.writes_enabled(self.rt): | |
| return {"state": "disabled", "sent": False, "binding": binding} | |
| approved = self._authorize(binding, approval_id) | |
| key = listing._hash({**self._scope(), "workflow": workflow, "approval_id": approval_id}) | |
| previous = self._receipt(key) | |
| if previous is not None: | |
| if previous["binding"] != binding: | |
| raise Failure("aplus", "ReservedApprovalMismatch") | |
| return copy.deepcopy(previous) | |
| snapshot = self._snapshot(binding) | |
| self._authorize(binding, approval_id) | |
| receipt = {"id": key, "binding": binding, "approval_id": approval_id, "approved_by": approved["approved_by"], | |
| "snapshot": snapshot, "state": "pending", "documents": {}, "operations": [], "at": time.time()} | |
| self._save(receipt, None) | |
| try: | |
| state = self._publish_documents(receipt, images) | |
| except Failure as exc: | |
| stored = self._receipt(key) | |
| if stored is not None: | |
| self._save({**stored, "state": "unknown", "failure_class": exc.failure_class}, stored) | |
| raise | |
| previous = copy.deepcopy(receipt) | |
| receipt.update(state=state, sent=state == "submitted", finished_at=time.time()) | |
| self._save(receipt, previous) | |
| return copy.deepcopy(receipt) | |
| def publish_aplus(rt, workflow, asins, parts, images, *, name, locale, approval_id, approval_reader, | |
| prior_asins=None, connection_id=None): | |
| publisher = APlusPublisher(rt, approval_reader, connection_id=connection_id) | |
| return publisher.publish_aplus(workflow, asins, parts, images, name=name, locale=locale, | |
| approval_id=approval_id, prior_asins=prior_asins) | |
| def aplus_status(rt, reference, *, connection_id=None): | |
| return APlusPublisher(rt, None, connection_id=connection_id).aplus_status(reference) | |