Theflame47 commited on
Commit
0f5e782
·
verified ·
1 Parent(s): ae62b83

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -3
app.py CHANGED
@@ -8,6 +8,7 @@ import requests
8
  import gradio as gr
9
  import base64
10
  import re
 
11
 
12
  PRINTIFY_BASE = "https://api.printify.com"
13
  DEFAULT_BASE_PRICE = 0.001
@@ -32,6 +33,18 @@ def _auth_headers() -> Dict[str, str]:
32
  return {"Authorization": f"Bearer {token}"}
33
 
34
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  def _parse_retry_after_seconds(r: requests.Response) -> Optional[float]:
36
  ra = r.headers.get("Retry-After")
37
  if ra:
@@ -65,9 +78,15 @@ def _req(
65
  path: str,
66
  json_body: Optional[Dict[str, Any]] = None,
67
  logs: Optional[List[str]] = None,
 
68
  ) -> Any:
 
 
 
 
 
69
  kwargs: Dict[str, Any] = {
70
- "headers": _auth_headers(),
71
  "timeout": 60,
72
  }
73
  if json_body is not None:
@@ -466,6 +485,8 @@ def _create_product_all_variants_with_grid(
466
  except Exception:
467
  continue
468
 
 
 
469
  img_id = upload.get("id")
470
  img_w = float(upload.get("width") or 0)
471
  img_h = float(upload.get("height") or 0)
@@ -551,7 +572,7 @@ def _create_product_all_variants_with_grid(
551
  provider_name = provider_details.get("title") or provider_details.get("name") or str(provider_id)
552
 
553
  title = (product_info.get("name") or "Printify Grid Test") + f" — {provider_name}"
554
- description = product_info.get("description") or ""
555
 
556
  payload = {
557
  "title": title,
@@ -565,7 +586,8 @@ def _create_product_all_variants_with_grid(
565
  _log(
566
  logs,
567
  f"PHASE_B_CREATE shop_id={shop_id} blueprint_id={bp_id} provider_id={provider_id} "
568
- f"variants_payload_total={len(variants_payload)} enabled={enabled_count} print_areas_payload={len(print_areas_payload)}",
 
569
  )
570
 
571
  created = _req(
@@ -573,6 +595,7 @@ def _create_product_all_variants_with_grid(
573
  f"/v1/shops/{shop_id}/products.json",
574
  json_body=payload,
575
  logs=logs,
 
576
  )
577
  if not isinstance(created, Dict):
578
  raise RuntimeError(f"Unexpected create response: {str(created)[:500]}")
 
8
  import gradio as gr
9
  import base64
10
  import re
11
+ import hashlib
12
 
13
  PRINTIFY_BASE = "https://api.printify.com"
14
  DEFAULT_BASE_PRICE = 0.001
 
33
  return {"Authorization": f"Bearer {token}"}
34
 
35
 
36
+ def _batch_fingerprint(shop_id: str, blueprint_id: int, provider_id: int, enabled_variant_ids: List[int]) -> str:
37
+ ids = sorted(int(x) for x in enabled_variant_ids)
38
+ payload = {
39
+ "shop_id": str(shop_id),
40
+ "blueprint_id": int(blueprint_id),
41
+ "provider_id": int(provider_id),
42
+ "enabled_variant_ids": ids,
43
+ }
44
+ raw = json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("utf-8")
45
+ return hashlib.sha1(raw).hexdigest()
46
+
47
+
48
  def _parse_retry_after_seconds(r: requests.Response) -> Optional[float]:
49
  ra = r.headers.get("Retry-After")
50
  if ra:
 
78
  path: str,
79
  json_body: Optional[Dict[str, Any]] = None,
80
  logs: Optional[List[str]] = None,
81
+ extra_headers: Optional[Dict[str, str]] = None,
82
  ) -> Any:
83
+ headers = _auth_headers()
84
+ if extra_headers:
85
+ for k, v in extra_headers.items():
86
+ headers[str(k)] = str(v)
87
+
88
  kwargs: Dict[str, Any] = {
89
+ "headers": headers,
90
  "timeout": 60,
91
  }
92
  if json_body is not None:
 
485
  except Exception:
486
  continue
487
 
488
+ fp = _batch_fingerprint(str(shop_id), int(bp_id), int(provider_id), sorted(list(enabled_ids)))
489
+
490
  img_id = upload.get("id")
491
  img_w = float(upload.get("width") or 0)
492
  img_h = float(upload.get("height") or 0)
 
572
  provider_name = provider_details.get("title") or provider_details.get("name") or str(provider_id)
573
 
574
  title = (product_info.get("name") or "Printify Grid Test") + f" — {provider_name}"
575
+ description = (product_info.get("description") or "") + f"\n\nBATCH_FP={fp}"
576
 
577
  payload = {
578
  "title": title,
 
586
  _log(
587
  logs,
588
  f"PHASE_B_CREATE shop_id={shop_id} blueprint_id={bp_id} provider_id={provider_id} "
589
+ f"variants_payload_total={len(variants_payload)} enabled={enabled_count} print_areas_payload={len(print_areas_payload)} "
590
+ f"idempotency_key={fp}",
591
  )
592
 
593
  created = _req(
 
595
  f"/v1/shops/{shop_id}/products.json",
596
  json_body=payload,
597
  logs=logs,
598
+ extra_headers={"Idempotency-Key": fp},
599
  )
600
  if not isinstance(created, Dict):
601
  raise RuntimeError(f"Unexpected create response: {str(created)[:500]}")