Update shopify_client.py
Browse files- shopify_client.py +30 -22
shopify_client.py
CHANGED
|
@@ -1,30 +1,38 @@
|
|
| 1 |
# shopify_client.py
|
| 2 |
-
|
| 3 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
def create_product(title: str, body_html: str, price: str, image_url: str):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
if not success:
|
| 19 |
-
raise Exception(
|
| 20 |
-
return
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
image_url="https://example.com/decal.jpg"
|
| 29 |
-
)
|
| 30 |
-
print("Created product:", prod["id"])
|
|
|
|
| 1 |
# shopify_client.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
+
import shopify
|
| 5 |
+
|
| 6 |
+
# Read credentials from env (set these in HF Space Settings → Secrets)
|
| 7 |
+
API_KEY = os.getenv("SHOPIFY_API_KEY")
|
| 8 |
+
ADMIN_TOKEN = os.getenv("SHOPIFY_ADMIN_TOKEN")
|
| 9 |
+
STORE_DOMAIN = os.getenv("SHOPIFY_STORE_DOMAIN") # e.g. "jc23eb-c7.myshopify.com"
|
| 10 |
+
API_VERSION = os.getenv("SHOPIFY_API_VERSION", "2023-10")
|
| 11 |
|
| 12 |
+
if not all([API_KEY, ADMIN_TOKEN, STORE_DOMAIN]):
|
| 13 |
+
raise ValueError("Missing one of SHOPIFY_API_KEY, SHOPIFY_ADMIN_TOKEN, or SHOPIFY_STORE_DOMAIN")
|
| 14 |
|
| 15 |
+
# Build the session URL
|
| 16 |
+
session_url = f"https://{API_KEY}:{ADMIN_TOKEN}@{STORE_DOMAIN}/admin/api/{API_VERSION}"
|
| 17 |
+
shopify.ShopifyResource.set_site(session_url)
|
| 18 |
|
| 19 |
def create_product(title: str, body_html: str, price: str, image_url: str):
|
| 20 |
+
"""
|
| 21 |
+
Create a new Shopify product.
|
| 22 |
+
"""
|
| 23 |
+
product = shopify.Product()
|
| 24 |
+
product.title = title
|
| 25 |
+
product.body_html = body_html
|
| 26 |
+
product.variants = [shopify.Variant({"price": price})]
|
| 27 |
+
product.images = [shopify.Image({"src": image_url})]
|
| 28 |
+
success = product.save()
|
| 29 |
if not success:
|
| 30 |
+
raise Exception(f"Shopify error: {product.errors.full_messages()}")
|
| 31 |
+
return product.to_dict()
|
| 32 |
|
| 33 |
+
def fetch_recent_orders(limit: int = 5):
|
| 34 |
+
"""
|
| 35 |
+
Return a list of the most recent orders.
|
| 36 |
+
"""
|
| 37 |
+
orders = shopify.Order.find(limit=limit, status="any")
|
| 38 |
+
return [o.to_dict() for o in orders]
|
|
|
|
|
|
|
|
|