Download platform/harness/amazon_listing_write.py from fsanyoto/loopable: direct link, hf CLI and curl.
- Browser
- Download file 44.6 kB
-
https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/platform/harness/amazon_listing_write.py
- Command line
-
hf download hf://spaces/fsanyoto/loopable/platform/harness/amazon_listing_write.py
-
curl -L -o amazon_listing_write.py https://huggingface.co/spaces/fsanyoto/loopable/resolve/main/platform/harness/amazon_listing_write.py
44.6 kB
| """Publish exact approved listing patches through a tenant-bound, non-retrying transport.""" | |
| from __future__ import annotations | |
| import base64 | |
| import copy | |
| import hashlib | |
| import json | |
| import re | |
| import time | |
| import urllib.error | |
| import urllib.parse | |
| import urllib.request | |
| from core import amazon_listing_attributes as details | |
| ENDPOINTS = { | |
| "na": "https://sellingpartnerapi-na.amazon.com", | |
| "eu": "https://sellingpartnerapi-eu.amazon.com", | |
| "fe": "https://sellingpartnerapi-fe.amazon.com", | |
| } | |
| PUBLIC_MEDIA_HOST = "fsanyoto-loopable.hf.space" | |
| MEDIA_HOSTS = frozenset({PUBLIC_MEDIA_HOST, "m.media-amazon.com"}) | |
| IMAGE_ATTRIBUTES = frozenset({"main_product_image_locator"} | { | |
| f"other_product_image_locator_{index}" for index in range(1, 9)}) | |
| COPY_ATTRIBUTES = frozenset({ | |
| "item_name", "title_differentiation", "bullet_point", "product_description", "generic_keyword"}) | |
| DETAIL_ATTRIBUTES = details.DETAIL_ATTRIBUTES | |
| REPLACE_ATTRIBUTES = COPY_ATTRIBUTES | IMAGE_ATTRIBUTES | DETAIL_ATTRIBUTES | |
| SCHEMA_HOSTS = frozenset({ | |
| "selling-partner-definitions-prod-iad.s3.amazonaws.com", | |
| "selling-partner-definitions-prod-iad.s3.us-east-1.amazonaws.com", | |
| "selling-partner-definitions-prod-dub.s3.amazonaws.com", | |
| "selling-partner-definitions-prod-dub.s3.eu-west-1.amazonaws.com", | |
| "selling-partner-definitions-prod-pdx.s3.amazonaws.com", | |
| "selling-partner-definitions-prod-pdx.s3.us-west-2.amazonaws.com", | |
| }) | |
| STATE_KEY = "amazon_listing_writes" | |
| PREVIEW_TTL = 15 * 60 | |
| _MAX_BYTES = 16 * 1024 * 1024 | |
| _NAME = re.compile(r"[A-Za-z0-9_][A-Za-z0-9_.:-]{0,255}\Z") | |
| class ListingWriteFailure(RuntimeError): | |
| """Expose fixed failure metadata without provider URLs, payloads or credentials.""" | |
| def __init__(self, operation, failure_class="Refused", status=None, *, field=None, allowed_units=()): | |
| self.operation = operation | |
| self.failure_class = failure_class | |
| self.status = status if type(status) is int else None | |
| self.field, self.allowed_units = field, list(allowed_units) | |
| super().__init__(f"{operation}: {self.status}: {failure_class}") | |
| def _text(value, maximum=1024): | |
| return (isinstance(value, str) and 0 < len(value) <= maximum and bool(value.strip()) | |
| and not any(ord(char) < 32 for char in value)) | |
| def _copy_text(value): | |
| return (isinstance(value, str) and 0 < len(value) <= _MAX_BYTES and bool(value.strip()) | |
| and not any(ord(char) < 32 and char not in "\n\r\t" for char in value)) | |
| def _name(value): | |
| if not isinstance(value, str) or not _NAME.fullmatch(value): | |
| raise ListingWriteFailure("validate", "InvalidIdentifier") | |
| return value | |
| def _json(value): | |
| try: | |
| return json.dumps(value, sort_keys=True, separators=(",", ":"), allow_nan=False).encode("utf-8") | |
| except (TypeError, ValueError, UnicodeError): | |
| raise ListingWriteFailure("validate", "InvalidJSON") from None | |
| def _hash(value): | |
| return hashlib.sha256(_json(value)).hexdigest() | |
| def _tenant(rt): | |
| key = getattr(rt, "key", None) | |
| if not isinstance(key, str) or not key or key != key.strip().lower(): | |
| raise ListingWriteFailure("authorize", "TenantRequired") | |
| return key | |
| def _document(value, tenant): | |
| if value is None or value == {}: | |
| return {"tenant_key": tenant, "writes_enabled": False, "switch_history": [], | |
| "previews": {}, "receipts": {}} | |
| if (not isinstance(value, dict) or value.get("tenant_key") != tenant | |
| or type(value.get("writes_enabled")) is not bool | |
| or not isinstance(value.get("switch_history"), list) | |
| or any(not isinstance(value.get(key), dict) for key in ("previews", "receipts"))): | |
| raise ListingWriteFailure("store", "InvalidTenantState") | |
| return copy.deepcopy(value) | |
| def _read(rt): | |
| try: | |
| return _document(rt.get(STATE_KEY, fresh=True), _tenant(rt)) | |
| except ListingWriteFailure: | |
| raise | |
| except Exception: | |
| raise ListingWriteFailure("store", "StateUnreadable") from None | |
| def _change(rt, mutate): | |
| tenant = _tenant(rt) | |
| def change(value): | |
| document = _document(value, tenant) | |
| mutate(document) | |
| return document | |
| try: | |
| rt.update(STATE_KEY, change, flush="sync") | |
| return _read(rt) | |
| except ListingWriteFailure: | |
| raise | |
| except Exception: | |
| # Persistence uncertainty never grants permission to send. | |
| raise ListingWriteFailure("store", "PersistenceUnproven") from None | |
| def writes_enabled(rt): | |
| """Read the tenant's current switch without a cache and fail closed on any read fault.""" | |
| try: | |
| return _read(rt)["writes_enabled"] is True | |
| except Exception: | |
| return False | |
| def set_writes_enabled(rt, enabled, by, words): | |
| """Record a platform administrator's switch setting and exact instruction atomically.""" | |
| from core import platform_admin | |
| if (type(enabled) is not bool or not platform_admin.is_platform_admin(by) | |
| or not _text(by.get("username")) or not _text(words, 16384)): | |
| raise ListingWriteFailure("switch", "PlatformAdminAndWordsRequired") | |
| event = {"id": _nonce(), "tenant_key": _tenant(rt), "enabled": enabled, | |
| "by": by["username"], "words": words, "at": time.time()} | |
| def flip(document): | |
| document["writes_enabled"] = enabled | |
| document["switch_history"].append(event) | |
| stored = _change(rt, flip) | |
| if stored["writes_enabled"] is not enabled or event not in stored["switch_history"]: | |
| raise ListingWriteFailure("switch", "PersistenceUnproven") | |
| return copy.deepcopy(event) | |
| def _nonce(): | |
| import uuid | |
| return uuid.uuid4().hex | |
| def allowed_media_url(value): | |
| """Admit only HTTPS images on the published Space or Amazon's exact image host.""" | |
| if not _text(value, 8192) or value != value.strip() or "\\" in value: | |
| return False | |
| try: | |
| parts = urllib.parse.urlsplit(value) | |
| return (parts.scheme == "https" and parts.netloc in MEDIA_HOSTS | |
| and parts.port is None and not parts.username and not parts.password | |
| and not parts.fragment and parts.path.startswith("/") and parts.path != "/") | |
| except ValueError: | |
| return False | |
| def validate_patch(body, marketplace, *, schema=None): | |
| """Return detached JSON containing only admitted attribute operations and value shapes.""" | |
| body = json.loads(_json(body)) | |
| _name(marketplace) | |
| if not isinstance(body, dict) or set(body) != {"productType", "patches"}: | |
| raise ListingWriteFailure("patchListingsItem", "InvalidPatch") | |
| _name(body["productType"]) | |
| patches, seen = body["patches"], set() | |
| if not isinstance(patches, list) or not patches: | |
| raise ListingWriteFailure("patchListingsItem", "InvalidPatch") | |
| for patch in patches: | |
| if not isinstance(patch, dict): | |
| raise ListingWriteFailure("patchListingsItem", "InvalidPatch") | |
| attribute = str(patch.get("path", "")).removeprefix("/attributes/") | |
| op = patch.get("op") | |
| if (patch.get("path") != "/attributes/" + attribute or attribute in seen | |
| or attribute not in REPLACE_ATTRIBUTES or not isinstance(op, str) or op not in {"replace", "delete"} | |
| or (op == "delete" and attribute not in IMAGE_ATTRIBUTES) | |
| or set(patch) not in ({"op", "path", "value"}, {"op", "path"}) | |
| or (op == "replace" and "value" not in patch)): | |
| raise ListingWriteFailure("patchListingsItem", "AttributeOrOperationRefused") | |
| seen.add(attribute) | |
| if "value" in patch: | |
| if attribute in DETAIL_ATTRIBUTES: | |
| if not isinstance(schema, details.DetailsSchema): | |
| raise ListingWriteFailure("patchListingsItem", "ProductTypeSchemaRequired") | |
| if schema.product_type != body["productType"] or schema.marketplace != marketplace: | |
| raise ListingWriteFailure("patchListingsItem", "ProductTypeSchemaMismatch") | |
| try: | |
| schema.validate_attribute(attribute, patch["value"]) | |
| except details.DetailFailure as exc: | |
| raise ListingWriteFailure("patchListingsItem", exc.reason, field=exc.field, | |
| allowed_units=exc.allowed_units) from None | |
| else: | |
| _attribute_values(attribute, patch["value"], marketplace, deleting=op == "delete") | |
| return body | |
| def _attribute_values(attribute, values, marketplace, *, deleting): | |
| if not isinstance(values, list) or not values: | |
| raise ListingWriteFailure("patchListingsItem", "InvalidAttributeValue") | |
| for value in values: | |
| if not isinstance(value, dict) or value.get("marketplace_id") != marketplace: | |
| raise ListingWriteFailure("patchListingsItem", "AttributeMarketplaceMismatch") | |
| if attribute in IMAGE_ATTRIBUTES: | |
| expected = {"marketplace_id"} if deleting else {"marketplace_id", "media_location"} | |
| if set(value) != expected or (not deleting and not allowed_media_url(value.get("media_location"))): | |
| raise ListingWriteFailure("patchListingsItem", "MediaLocationRefused") | |
| elif (set(value) - {"marketplace_id", "language_tag", "value"} | |
| or not _copy_text(value.get("value")) | |
| or ("language_tag" in value and not _text(value["language_tag"], 32))): | |
| raise ListingWriteFailure("patchListingsItem", "InvalidAttributeValue") | |
| def read_approval(rt, reader, approval_id, binding): | |
| """Require a trusted reader's approved record to contain every exact binding field.""" | |
| if not callable(reader) or not _text(approval_id) or binding.get("tenant_key") != _tenant(rt): | |
| raise ListingWriteFailure("approve", "ApprovalRequired") | |
| try: | |
| record = reader(_tenant(rt), approval_id) | |
| matches = (isinstance(record, dict) and record.get("decision") == "approved" | |
| and _text(record.get("approved_by")) | |
| and all(key in record and _json(record[key]) == _json(value) | |
| for key, value in binding.items())) | |
| except Exception: | |
| raise ListingWriteFailure("approve", "ApprovalUnreadable") from None | |
| if not matches: | |
| raise ListingWriteFailure("approve", "ExactApprovalRequired") | |
| return copy.deepcopy(record) | |
| class _NoRedirect(urllib.request.HTTPRedirectHandler): | |
| def redirect_request(self, req, fp, code, msg, headers, newurl): | |
| return None | |
| def _schema_capability(definition): | |
| if not isinstance(definition, dict) or not isinstance(definition.get("schema"), dict): | |
| raise ListingWriteFailure("downloadSchema", "SchemaDefinitionRequired") | |
| descriptor = definition["schema"] | |
| link = descriptor.get("link") | |
| url = link.get("resource") if isinstance(link, dict) else None | |
| try: | |
| parts = urllib.parse.urlsplit(url) | |
| allowed = (_text(url, 32768) and url == url.strip() and "\\" not in url | |
| and parts.scheme == "https" and parts.hostname in SCHEMA_HOSTS | |
| and parts.netloc == parts.hostname and parts.port is None | |
| and not parts.username and not parts.password and not parts.fragment | |
| and parts.path.startswith("/") and parts.path != "/") | |
| except (TypeError, ValueError): | |
| allowed = False | |
| if not allowed or link.get("verb") != "GET": | |
| raise ListingWriteFailure("downloadSchema", "SchemaHostRefused") | |
| checksum = descriptor.get("checksum") | |
| try: | |
| valid = isinstance(checksum, str) and len(base64.b64decode(checksum, validate=True)) == 16 | |
| except ValueError: | |
| valid = False | |
| if not valid: | |
| raise ListingWriteFailure("downloadSchema", "SchemaChecksumRequired") | |
| return {"url": url, "checksum": checksum} | |
| class _KeychainSnapshot: | |
| def __init__(self, rt, keychain): | |
| self.key = _tenant(rt) | |
| self.rows = {name: rt.get(name, fresh=True) or {} for name in (keychain.KEY, keychain.SCOPE_KEY)} | |
| if any(not isinstance(value, dict) for value in self.rows.values()): | |
| raise ListingWriteFailure("keychain", "InvalidCredentialStore") | |
| def get(self, name, fresh=False): | |
| return self.rows[name] | |
| class _Transport: | |
| def __init__(self, rt, *, connection_id=None, timeout=30): | |
| from core import keychain | |
| from harness import amazon_client | |
| self.rt, self.tenant_key = rt, _tenant(rt) | |
| self.keychain, self.client = keychain, amazon_client | |
| if connection_id is not None and not _text(connection_id): | |
| raise ListingWriteFailure("keychain", "ConnectionRequired") | |
| self.requested_connection = connection_id | |
| self.connection_id, self.fields = self._credential() | |
| self.fields = copy.deepcopy(self.fields) | |
| self.region = self.fields.get("region", "na") | |
| self.marketplace = _name(self.fields.get("marketplace_id")) | |
| if self.region not in ENDPOINTS or amazon_client.is_sandbox(self.fields): | |
| raise ListingWriteFailure("keychain", "ProductionRegionRequired") | |
| self.seller = urllib.parse.quote(self.fields["seller_id"], safe="") | |
| self.timeout, self.token, self.issued = timeout, None, 0.0 | |
| self.urlopen = urllib.request.build_opener(_NoRedirect()).open | |
| self._binding = None | |
| self._uploads = {} | |
| self._schemas = {} | |
| def _credential(self): | |
| if _tenant(self.rt) != self.tenant_key: | |
| raise ListingWriteFailure("keychain", "TenantChanged") | |
| try: | |
| snapshot = _KeychainSnapshot(self.rt, self.keychain) | |
| connection = self.requested_connection | |
| if connection is None: | |
| connection = self.keychain.default_connection_id(snapshot, "amazon_sp") | |
| fields = self.keychain.amazon_creds(snapshot, connection_id=connection) if connection else None | |
| if not isinstance(fields, dict) or not all(_text(fields.get(key)) for key in ( | |
| "seller_id", "marketplace_id", "client_id", "client_secret", "refresh_token")): | |
| raise ListingWriteFailure("keychain", "CredentialMissing") | |
| return connection, fields | |
| except ListingWriteFailure: | |
| raise | |
| except Exception: | |
| raise ListingWriteFailure("keychain", "CredentialUnreadable") from None | |
| def _current(self): | |
| connection, fields = self._credential() | |
| if connection != self.connection_id or fields != self.fields: | |
| raise ListingWriteFailure("keychain", "CredentialChangedOrRevoked") | |
| def _access_token(self): | |
| self._current() | |
| if self.token is None or time.monotonic() - self.issued >= 2700: | |
| try: | |
| self.token = self.client.access_token(self.fields, timeout=self.timeout) | |
| except Exception: | |
| raise ListingWriteFailure("accessToken", "TokenUnavailable") from None | |
| self.issued = time.monotonic() | |
| if not _text(self.token, 16384): | |
| raise ListingWriteFailure("accessToken", "TokenUnavailable") | |
| return self.token | |
| def _guard_request(self, operation, request): | |
| binding = self._binding | |
| if (not binding or request is not binding[0] or operation != binding[1] | |
| or request.full_url != binding[2] or request.data != binding[3]): | |
| raise ListingWriteFailure(operation, "FreshRequestBindingRequired") | |
| from harness import amazon_aplus_write as aplus | |
| if operation in aplus.OPERATIONS or operation == "uploadAplusImage": | |
| aplus.guard_request(self, operation, request, binding) | |
| return | |
| if operation == "downloadSchema": | |
| cap = binding[5] | |
| if (not isinstance(cap, dict) or cap != _schema_capability({"schema": { | |
| "link": {"verb": "GET", "resource": request.full_url}, "checksum": cap.get("checksum")}}) | |
| or request.method != "GET" or request.data is not None or binding[4] is not None | |
| or request.has_header("X-amz-access-token") or request.has_header("Authorization")): | |
| raise ListingWriteFailure(operation, "RequestRefused") | |
| return | |
| parts = urllib.parse.urlsplit(request.full_url) | |
| params = urllib.parse.parse_qs(parts.query, keep_blank_values=True) | |
| allowed = (parts.scheme == "https" and parts.netloc == urllib.parse.urlsplit(ENDPOINTS[self.region]).netloc | |
| and not parts.username and not parts.password and not parts.fragment and parts.port is None | |
| and request.get_header("X-amz-access-token") == self.token and bool(self.token) | |
| and not request.has_header("Authorization") | |
| and params.get("marketplaceIds") == [self.marketplace]) | |
| prefix = "/listings/2021-08-01/items/" + self.seller + "/" | |
| suffix = parts.path.removeprefix(prefix) | |
| listing = (parts.path.startswith(prefix) and bool(suffix) and "/" not in suffix | |
| and urllib.parse.unquote(suffix) not in {".", ".."}) | |
| if operation == "getListingsItem": | |
| allowed &= (listing and request.method == "GET" and request.data is None | |
| and set(params) == {"marketplaceIds", "includedData"} | |
| and params["includedData"] == ["attributes,productTypes,summaries"]) | |
| elif operation == "getDefinitionsProductType": | |
| definition = parts.path.removeprefix("/definitions/2020-09-01/productTypes/") | |
| allowed &= (parts.path.startswith("/definitions/2020-09-01/productTypes/") | |
| and bool(_NAME.fullmatch(definition)) and request.method == "GET" and request.data is None | |
| and params == {"marketplaceIds": [self.marketplace], "sellerId": [self.fields["seller_id"]], | |
| "requirements": ["LISTING"], "requirementsEnforced": ["ENFORCED"], | |
| "productTypeVersion": ["LATEST"]}) | |
| elif operation == "patchListingsItem": | |
| expected = {"marketplaceIds": [self.marketplace]} | |
| if binding[4] is None: | |
| expected["mode"] = ["VALIDATION_PREVIEW"] | |
| elif not callable(binding[4]): | |
| allowed = False | |
| allowed &= listing and request.method == "PATCH" and params == expected | |
| self._validate_patch(json.loads(request.data)) | |
| else: | |
| allowed = False | |
| if not allowed: | |
| raise ListingWriteFailure(operation, "RequestRefused") | |
| def _perform(self, operation, request): | |
| try: | |
| self._guard_request(operation, request) | |
| except ListingWriteFailure: | |
| self._binding = None | |
| raise | |
| except Exception: | |
| self._binding = None | |
| raise ListingWriteFailure(operation, "RequestRefused") from None | |
| boundary, cap, self._binding = self._binding[4], self._binding[5], None | |
| self._current() | |
| from harness import amazon_aplus_write as aplus | |
| if (operation in aplus.OPERATIONS or operation == "uploadAplusImage") and not writes_enabled(self.rt): | |
| raise ListingWriteFailure(operation, "WritesDisabled") | |
| if boundary is not None: | |
| boundary() | |
| status = None | |
| try: | |
| # This is the only wire owner and its opener refuses redirects and retries. | |
| with self.urlopen(request, timeout=self.timeout) as response: | |
| status = response.status | |
| raw = response.read(_MAX_BYTES + 1) | |
| if len(raw) > _MAX_BYTES or not 200 <= status < 300: | |
| raise ListingWriteFailure(operation, "InvalidHTTPResponse", status) | |
| if (operation == "downloadSchema" | |
| and base64.b64encode(hashlib.md5(raw).digest()).decode("ascii") != cap["checksum"]): | |
| raise ListingWriteFailure(operation, "SchemaChecksumMismatch", status) | |
| got = {"uploaded": True} if operation == "uploadAplusImage" else json.loads(raw) | |
| if not isinstance(got, dict): | |
| raise ListingWriteFailure(operation, "InvalidResponse", status) | |
| return got | |
| except ListingWriteFailure: | |
| raise | |
| except urllib.error.HTTPError as exc: | |
| exc.close() | |
| raise ListingWriteFailure(operation, "HTTPError", exc.code) from None | |
| except Exception: | |
| raise ListingWriteFailure(operation, "OutcomeUnknown", status) from None | |
| def _request(self, operation, path, params, *, body=None, boundary=None, method=None, image=None, schema=None): | |
| from harness import amazon_aplus_write as aplus | |
| method = method or ("GET" if body is None else "PATCH") | |
| cap = None | |
| if operation == "uploadAplusImage": | |
| url, raw, headers, cap = aplus.upload_request(self, path, image) | |
| elif operation == "downloadSchema": | |
| url, raw, headers, cap = path, None, {"Accept": "application/json"}, schema | |
| else: | |
| raw = None if body is None else _json(body) | |
| url = ENDPOINTS[self.region] + path + "?" + urllib.parse.urlencode(params) | |
| headers = {"x-amz-access-token": self._access_token(), "Content-Type": "application/json", | |
| "Accept": "application/json"} | |
| request = urllib.request.Request(url, data=raw, method=method, headers=headers) | |
| self._binding = (request, operation, url, raw, boundary, cap) | |
| return self._perform(operation, request) | |
| def aplus(self, operation, *, reference=None, asin=None, page_token=None, body=None, | |
| asins=None, content_md5=None, content_type=None, boundary=None): | |
| from harness import amazon_aplus_write as aplus | |
| if not writes_enabled(self.rt): | |
| raise ListingWriteFailure(operation, "WritesDisabled") | |
| path, params, method = aplus.operation_request( | |
| self.marketplace, operation, reference, asin, page_token, body, asins, content_md5, content_type) | |
| response = self._request(operation, path, params, body=body, boundary=boundary, method=method) | |
| if operation == "createUploadDestinationForResource": | |
| aplus.register_upload(self, response, content_md5, content_type) | |
| return response | |
| def upload_aplus_image(self, destination_id, content, *, boundary): | |
| return self._request("uploadAplusImage", destination_id, {}, method="POST", | |
| boundary=boundary, image=content) | |
| def read(self, operation, *, sku=None, product_type=None): | |
| params = {"marketplaceIds": self.marketplace} | |
| if operation == "getListingsItem" and _text(sku) and sku not in {".", ".."} and product_type is None: | |
| path = "/listings/2021-08-01/items/" + self.seller + "/" + urllib.parse.quote(sku, safe="") | |
| params["includedData"] = "attributes,productTypes,summaries" | |
| elif operation == "getDefinitionsProductType" and sku is None: | |
| path = "/definitions/2020-09-01/productTypes/" + _name(product_type) | |
| params.update(sellerId=self.fields["seller_id"], requirements="LISTING", | |
| requirementsEnforced="ENFORCED", productTypeVersion="LATEST") | |
| else: | |
| raise ListingWriteFailure("read", "OperationRefused") | |
| return self._request(operation, path, params) | |
| def _patch(self, sku, body, *, boundary=None): | |
| if not _text(sku) or sku in {".", ".."}: | |
| raise ListingWriteFailure("patchListingsItem", "InvalidSKU") | |
| body = self._validate_patch(body) | |
| path = "/listings/2021-08-01/items/" + self.seller + "/" + urllib.parse.quote(sku, safe="") | |
| params = {"marketplaceIds": self.marketplace} | |
| if boundary is None: | |
| params["mode"] = "VALIDATION_PREVIEW" | |
| return self._request("patchListingsItem", path, params, body=body, boundary=boundary) | |
| def _validate_patch(self, body): | |
| return validate_patch(body, self.marketplace, schema=self._schemas.get( | |
| body.get("productType") if isinstance(body, dict) else None)) | |
| def schema(self, definition): | |
| cap = _schema_capability(definition) | |
| return self._request("downloadSchema", cap["url"], {}, schema=cap) | |
| def product_schema(self, product_type): | |
| product_type = _name(product_type) | |
| self._current() | |
| if product_type not in self._schemas: | |
| definition = self.read("getDefinitionsProductType", product_type=product_type) | |
| if (definition.get("productType") != product_type | |
| or definition.get("marketplaceIds") != [self.marketplace]): | |
| raise ListingWriteFailure("getDefinitionsProductType", "DefinitionScopeMismatch") | |
| raw = self.schema(definition) | |
| self._schemas[product_type] = details.DetailsSchema(product_type, self.marketplace, raw) | |
| return self._schemas[product_type] | |
| class ListingPublisher: | |
| """Use a trusted tenant runtime and a server-owned reader of exact per-listing approvals.""" | |
| def __init__(self, rt, approval_reader, *, connection_id=None, timeout=30): | |
| self.rt, self.tenant_key, self.approval_reader = rt, _tenant(rt), approval_reader | |
| self.transport = _Transport(rt, connection_id=connection_id, timeout=timeout) | |
| def read_item(self, sku): | |
| return self.transport.read("getListingsItem", sku=sku) | |
| def read_definition(self, product_type): | |
| return self.transport.read("getDefinitionsProductType", product_type=product_type) | |
| def read_schema(self, product_type): | |
| return self.transport.product_schema(product_type) | |
| def _binding(self, workflow, asin, sku, body): | |
| if _tenant(self.rt) != self.tenant_key or not _text(workflow) or not _text(asin) or not _text(sku): | |
| raise ListingWriteFailure("publish", "ListingIdentityRequired") | |
| body = json.loads(_json(body)) | |
| # Refuse unknown paths before a details patch may fetch its named product type schema. | |
| paths = {"/attributes/" + name for name in REPLACE_ATTRIBUTES} | |
| if (isinstance(body, dict) and isinstance(body.get("patches"), list) | |
| and all(isinstance(p, dict) and p.get("path") in paths for p in body["patches"]) | |
| and any(p.get("path", "").removeprefix("/attributes/") in DETAIL_ATTRIBUTES for p in body["patches"])): | |
| self.read_schema(body.get("productType")) | |
| return {"tenant_key": self.tenant_key, "workflow": workflow, "asin": asin, "sku": sku, | |
| "body": self.transport._validate_patch(body)} | |
| def _scope(self, binding): | |
| return {**binding, "connection_id": self.transport.connection_id, "region": self.transport.region, | |
| "marketplace_id": self.transport.marketplace, "seller_id": self.transport.fields["seller_id"]} | |
| def preview(self, workflow, asin, sku, body): | |
| """Persist the exact body's observed preview result without requiring the LIVE switch.""" | |
| binding = self._binding(workflow, asin, sku, body) | |
| scope = self._scope(binding) | |
| key = _hash(scope) | |
| _change(self.rt, lambda document: document["previews"].pop(key, None)) | |
| response = self.transport._patch(sku, binding["body"]) | |
| result = {"binding": scope, "body_sha256": _hash(binding["body"]), | |
| "status": response.get("status"), "at": time.time(), "response": response} | |
| stored = _change(self.rt, lambda document: document["previews"].update({key: result})) | |
| if stored["previews"].get(key) != result: | |
| raise ListingWriteFailure("preview", "PersistenceUnproven") | |
| return copy.deepcopy(result) | |
| def _valid_preview(self, document, binding): | |
| scope = self._scope(binding) | |
| preview = document["previews"].get(_hash(scope)) | |
| now = time.time() | |
| if (not isinstance(preview, dict) or preview.get("binding") != scope | |
| or preview.get("body_sha256") != _hash(binding["body"]) or preview.get("status") != "VALID" | |
| or type(preview.get("at")) not in (int, float) or not 0 <= now - preview["at"] <= PREVIEW_TTL): | |
| raise ListingWriteFailure("publish", "RecentIdenticalValidPreviewRequired") | |
| def _live_boundary(self, binding, approval_id, receipt=None): | |
| approved = read_approval(self.rt, self.approval_reader, approval_id, binding) | |
| document = _read(self.rt) | |
| self._valid_preview(document, binding) | |
| if receipt is not None and document["receipts"].get(receipt["id"]) != receipt: | |
| raise ListingWriteFailure("publish", "PendingReceiptRequired") | |
| if not writes_enabled(self.rt): | |
| raise ListingWriteFailure("publish", "WritesDisabled") | |
| return approved | |
| def _snapshot(self, binding): | |
| item = self.read_item(binding["sku"]) | |
| market = self.transport.marketplace | |
| summaries, types, attributes = item.get("summaries"), item.get("productTypes"), item.get("attributes") | |
| if (not isinstance(attributes, dict) or not isinstance(summaries, list) or not isinstance(types, list) | |
| or not any(isinstance(row, dict) and row.get("marketplaceId") == market | |
| and row.get("asin") == binding["asin"] for row in summaries) | |
| or not any(isinstance(row, dict) and row.get("marketplaceId") == market | |
| and row.get("productType") == binding["body"]["productType"] for row in types)): | |
| raise ListingWriteFailure("publish", "FreshListingIdentityRequired") | |
| return copy.deepcopy(attributes) | |
| def _reserve(self, receipt, binding): | |
| def reserve(document): | |
| if receipt["id"] in document["receipts"]: | |
| raise ListingWriteFailure("publish", "AlreadyReserved") | |
| identity = ("tenant_key", "workflow", "asin", "sku") | |
| if any(row.get("state") in {"pending", "unknown"} | |
| and all(row.get("binding", {}).get(key) == binding[key] for key in identity) | |
| for row in document["receipts"].values()): | |
| raise ListingWriteFailure("publish", "ReconciliationRequired") | |
| if document["writes_enabled"] is not True: | |
| raise ListingWriteFailure("publish", "WritesDisabled") | |
| self._valid_preview(document, binding) | |
| document["receipts"][receipt["id"]] = receipt | |
| stored = _change(self.rt, reserve) | |
| if stored["receipts"].get(receipt["id"]) != receipt: | |
| raise ListingWriteFailure("publish", "PersistenceUnproven") | |
| def _finish(self, pending, **outcome): | |
| receipt = {**pending, **outcome, "finished_at": time.time()} | |
| def finish(document): | |
| if document["receipts"].get(pending["id"]) != pending: | |
| raise ListingWriteFailure("publish", "ReceiptChanged") | |
| document["receipts"][pending["id"]] = receipt | |
| stored = _change(self.rt, finish) | |
| if stored["receipts"].get(pending["id"]) != receipt: | |
| raise ListingWriteFailure("publish", "PersistenceUnproven") | |
| return copy.deepcopy(receipt) | |
| def publish(self, workflow, asin, sku, body, *, approval_id): | |
| """Send once after a valid preview, returning an existing receipt on any later invocation.""" | |
| binding = self._binding(workflow, asin, sku, body) | |
| if not _text(approval_id): | |
| raise ListingWriteFailure("approve", "ApprovalRequired") | |
| identity = {key: value for key, value in binding.items() if key != "body"} | |
| key = _hash({**identity, "approval_id": approval_id}) | |
| previous = _read(self.rt)["receipts"].get(key) | |
| if previous is not None: | |
| if previous.get("binding") != self._scope(binding): | |
| raise ListingWriteFailure("publish", "ReservedApprovalMismatch") | |
| return copy.deepcopy(previous) | |
| approved = self._live_boundary(binding, approval_id) | |
| snapshot = self._snapshot(binding) | |
| pending = {"id": key, "binding": self._scope(binding), "approval_id": approval_id, | |
| "approved_by": approved["approved_by"], "body_sha256": _hash(binding["body"]), | |
| "prior_attributes": snapshot, "state": "pending", "sent": False, "at": time.time()} | |
| self._reserve(pending, binding) | |
| try: | |
| response = self.transport._patch(sku, binding["body"], boundary=lambda: | |
| self._live_boundary(binding, approval_id, pending)) | |
| except ListingWriteFailure as exc: | |
| self._finish(pending, state="unknown", failure_class=exc.failure_class) | |
| raise | |
| status = response.get("status") | |
| state = {"ACCEPTED": "accepted", "INVALID": "rejected"}.get(status, "unknown") | |
| return self._finish(pending, state=state, sent=state == "accepted", response=response) | |
| def publish_listing(rt, workflow, asin, sku, body, *, approval_id, approval_reader, connection_id=None): | |
| """Preview first, report disabled sending, or publish the identical approved patch once.""" | |
| publisher = ListingPublisher(rt, approval_reader, connection_id=connection_id) | |
| preview = publisher.preview(workflow, asin, sku, body) | |
| if preview["status"] != "VALID": | |
| return {"sent": False, "state": "invalid", "preview": preview} | |
| if not writes_enabled(rt): | |
| return {"sent": False, "state": "disabled", "preview": preview} | |
| return publisher.publish(workflow, asin, sku, body, approval_id=approval_id) | |
| class _OfflineFixture: | |
| def __init__(self): | |
| from types import SimpleNamespace | |
| self.key = "listing-fixture" | |
| self.data, self.requests, self.reads = {}, [], [] | |
| self.fail_read = self.fail_write = self.unclear = self.redirect = False | |
| self.preview_status = "VALID" | |
| self.before_live = None | |
| self.fields = {"seller_id": "fixture-seller", "marketplace_id": "MARKET1", "region": "na", | |
| "client_id": "fixture-client", "client_secret": "fixture-secret", "refresh_token": "fixture-refresh"} | |
| self.body = {"productType": "VASE", "patches": [{"op": "replace", "path": "/attributes/item_name", | |
| "value": [{"value": "fixture title", "language_tag": "en_US", "marketplace_id": "MARKET1"}]}]} | |
| self.approval = {"tenant_key": self.key, "workflow": "workflow-1", "asin": "ASIN1", "sku": "sku-1", | |
| "body": copy.deepcopy(self.body), "decision": "approved", "approved_by": "fixture-person"} | |
| self.admin = {"tenant": "loopable", "platform_admin": True, "username": "fixture-operator"} | |
| self.original = {"item_name": [{"value": "original", "marketplace_id": "MARKET1"}], "color": [{"value": "blue"}]} | |
| self.rt = SimpleNamespace(key=self.key, get=self.get, update=self.update) | |
| def get(self, name, fresh=False): | |
| self.reads.append((name, fresh)) | |
| if self.fail_read and name == STATE_KEY: | |
| raise OSError("fixture store unavailable") | |
| return copy.deepcopy(self.data.get(name)) | |
| def update(self, name, fn, flush): | |
| if self.fail_write: | |
| raise OSError("fixture persistence unavailable") | |
| if flush != "sync": | |
| raise AssertionError("synchronous persistence required") | |
| self.data[name] = copy.deepcopy(fn(copy.deepcopy(self.data.get(name)))) | |
| def reader(self, tenant, approval_id): | |
| if tenant != self.key or not approval_id: | |
| raise AssertionError("tenant approval reader") | |
| return copy.deepcopy(self.approval) | |
| def __enter__(self): | |
| from contextlib import ExitStack | |
| from types import SimpleNamespace | |
| from unittest.mock import patch | |
| from core import keychain | |
| from harness import amazon_client | |
| self.stack = ExitStack() | |
| self.stack.enter_context(patch.object(keychain, "default_connection_id", return_value="fixture-connection")) | |
| self.stack.enter_context(patch.object(keychain, "amazon_creds", side_effect=lambda *a, **k: copy.deepcopy(self.fields))) | |
| self.stack.enter_context(patch.object(amazon_client, "access_token", return_value="fixture-token")) | |
| self.stack.enter_context(patch.object(urllib.request, "build_opener", return_value=SimpleNamespace(open=self.wire))) | |
| self.stack.enter_context(patch.object(urllib.request, "urlopen", side_effect=AssertionError("real network forbidden"))) | |
| self.publisher = ListingPublisher(self.rt, self.reader) | |
| return self | |
| def __exit__(self, *args): | |
| return self.stack.__exit__(*args) | |
| def wire(self, request, timeout): | |
| import io | |
| self.requests.append(request) | |
| params = urllib.parse.parse_qs(urllib.parse.urlsplit(request.full_url).query, keep_blank_values=True) | |
| if "mode" in params and params["mode"] != ["VALIDATION_PREVIEW"]: | |
| raise AssertionError("unsupported listing PATCH mode") | |
| if self.redirect: | |
| raise urllib.error.HTTPError(request.full_url, 302, "fixture redirect", {}, io.BytesIO()) | |
| if request.method == "PATCH": | |
| if "mode" not in params: | |
| pending = list(self.data[STATE_KEY]["receipts"].values()) | |
| if not pending or pending[-1]["prior_attributes"] != self.original or pending[-1]["state"] != "pending": | |
| raise AssertionError("durable snapshot and pending receipt before wire") | |
| if self.unclear: | |
| raise TimeoutError("fixture unclear response") | |
| got = {"status": "ACCEPTED"} | |
| else: | |
| got = {"status": self.preview_status, "issues": []} | |
| elif "/definitions/" in request.full_url: | |
| got = {"productType": "VASE", "marketplaceIds": ["MARKET1"]} | |
| else: | |
| got = {"attributes": self.original, "summaries": [{"marketplaceId": "MARKET1", "asin": "ASIN1"}], | |
| "productTypes": [{"marketplaceId": "MARKET1", "productType": "VASE"}]} | |
| if self.before_live: | |
| self.before_live() | |
| response = io.BytesIO(_json(got)) | |
| response.status = 200 | |
| return response | |
| def preview(self): | |
| return self.publisher.preview("workflow-1", "ASIN1", "sku-1", self.body) | |
| def publish(self, approval_id="approval-1"): | |
| return self.publisher.publish("workflow-1", "ASIN1", "sku-1", self.body, approval_id=approval_id) | |
| def enable(self): | |
| return set_writes_enabled(self.rt, True, self.admin, "fixture approved switch instruction") | |
| def live_count(self): | |
| return sum(request.method == "PATCH" | |
| and "mode" not in urllib.parse.parse_qs(urllib.parse.urlsplit(request.full_url).query, | |
| keep_blank_values=True) | |
| for request in self.requests) | |
| def _refused(call): | |
| try: | |
| call() | |
| except ListingWriteFailure: | |
| return True | |
| return False | |
| def _selftest_refusals(): | |
| checks = {} | |
| with _OfflineFixture() as fixture: | |
| fixture.preview() | |
| checks["switch_off"] = _refused(fixture.publish) and fixture.live_count() == 0 | |
| checks["preview_with_switch_off"] = len(fixture.requests) == 1 | |
| for operation in ("putListingsItem", "deleteListingsItem", "createFeed"): | |
| before = len(fixture.requests) | |
| checks[operation] = (_refused(lambda: fixture.publisher.transport.read(operation)) | |
| and len(fixture.requests) == before) | |
| fixture.enable() | |
| fixture.body["patches"][0]["path"] = "/attributes/price" | |
| before = len(fixture.requests) | |
| checks["attribute_allowlist"] = _refused(fixture.preview) and len(fixture.requests) == before | |
| for case in ("no_preview", "stale_preview", "different_body", "different_approval", "media_host", "invalid_preview"): | |
| with _OfflineFixture() as fixture: | |
| fixture.enable() | |
| if case != "no_preview": | |
| fixture.preview() | |
| if case == "stale_preview": | |
| for row in fixture.data[STATE_KEY]["previews"].values(): | |
| row["at"] = time.time() - PREVIEW_TTL - 1 | |
| elif case == "different_body": | |
| fixture.body["patches"][0]["value"][0]["value"] = "changed" | |
| fixture.approval["body"] = copy.deepcopy(fixture.body) | |
| elif case == "different_approval": | |
| fixture.approval["body"]["patches"][0]["value"][0]["value"] = "not approved" | |
| elif case == "media_host": | |
| fixture.body["patches"] = [{"op": "replace", "path": "/attributes/main_product_image_locator", | |
| "value": [{"marketplace_id": "MARKET1", "media_location": "https://evil.example/image.jpg"}]}] | |
| elif case == "invalid_preview": | |
| fixture.preview_status = "INVALID" | |
| fixture.preview() | |
| before = len(fixture.requests) | |
| checks[case] = _refused(fixture.publish) and len(fixture.requests) == before | |
| return checks | |
| def _selftest_persistence(): | |
| checks = {} | |
| with _OfflineFixture() as fixture: | |
| flip = fixture.enable() | |
| checks["switch_audit"] = flip in fixture.data[STATE_KEY]["switch_history"] and bool(flip["words"]) | |
| fixture.preview() | |
| first = fixture.publish() | |
| checks["accepted_snapshot"] = (first["state"] == "accepted" and first["sent"] is True | |
| and first["prior_attributes"] == fixture.original and fixture.live_count() == 1) | |
| checks["accepted_no_resend"] = fixture.publish() == first and fixture.live_count() == 1 | |
| checks["fresh_reads"] = bool(fixture.reads) and all(fresh for _, fresh in fixture.reads) | |
| with _OfflineFixture() as fixture: | |
| fixture.enable() | |
| fixture.preview() | |
| fixture.unclear = True | |
| checks["unknown_failure"] = _refused(fixture.publish) and fixture.live_count() == 1 | |
| recovered = fixture.publish() | |
| checks["unknown_no_resend"] = recovered["state"] == "unknown" and fixture.live_count() == 1 | |
| checks["unknown_new_approval"] = _refused(lambda: fixture.publish("approval-2")) and fixture.live_count() == 1 | |
| for fault in ("fail_read", "fail_write"): | |
| with _OfflineFixture() as fixture: | |
| fixture.enable() | |
| fixture.preview() | |
| setattr(fixture, fault, True) | |
| checks[fault] = _refused(fixture.publish) and fixture.live_count() == 0 | |
| if fault == "fail_read": | |
| checks["switch_read_failure"] = writes_enabled(fixture.rt) is False | |
| with _OfflineFixture() as fixture: | |
| fixture.enable() | |
| fixture.preview() | |
| fixture.before_live = lambda: set_writes_enabled(fixture.rt, False, fixture.admin, "fixture revocation") | |
| checks["switch_boundary"] = _refused(fixture.publish) and fixture.live_count() == 0 | |
| return checks | |
| def selftest(): | |
| """Return focused offline behavior controls using in-memory stores and a provider double.""" | |
| checks = {**_selftest_refusals(), **_selftest_persistence()} | |
| with _OfflineFixture() as fixture: | |
| fixture.redirect = True | |
| checks["redirect_response"] = _refused(fixture.preview) and len(fixture.requests) == 1 | |
| checks["redirect_handler"] = _NoRedirect().redirect_request(None, None, 302, "", {}, "https://evil.example") is None | |
| before = len(fixture.requests) | |
| forged = urllib.request.Request(ENDPOINTS["na"] + "/unbound", method="PATCH", data=b"{}") | |
| checks["unbound_request"] = (_refused(lambda: fixture.publisher.transport._perform("patchListingsItem", forged)) | |
| and len(fixture.requests) == before) | |
| by = {"username": "tenant-admin", "role": "admin", "tenant": fixture.key} | |
| checks["platform_admin_only"] = _refused(lambda: set_writes_enabled(fixture.rt, True, by, "fixture instruction")) | |
| return {"selftest": "PASS" if all(checks.values()) else "FAIL", "checks": checks, | |
| "network": False, "store_writes": False} | |
| if __name__ == "__main__": | |
| import sys | |
| from pathlib import Path | |
| sys.path.insert(0, str(Path(__file__).resolve().parents[1])) | |
| if sys.argv[1:] != ["--selftest"]: | |
| raise SystemExit("Only --selftest is supported. Use ListingPublisher from the trusted runtime.") | |
| result = selftest() | |
| print(json.dumps(result)) | |
| raise SystemExit(0 if result["selftest"] == "PASS" else 1) | |