File size: 31,075 Bytes
3e19b1e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
"""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)