File size: 1,359 Bytes
b0d7bb5
1b757f2
b0d7bb5
1b757f2
 
 
 
 
 
 
b0d7bb5
1b757f2
 
b0d7bb5
1b757f2
 
 
b0d7bb5
 
1b757f2
 
 
 
 
 
 
 
 
b0d7bb5
1b757f2
 
b0d7bb5
1b757f2
 
 
 
 
 
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
# shopify_client.py

import os
import shopify

# Read credentials from env (set these in HF Space Settings → Secrets)
API_KEY      = os.getenv("SHOPIFY_API_KEY")
ADMIN_TOKEN  = os.getenv("SHOPIFY_ADMIN_TOKEN")
STORE_DOMAIN = os.getenv("SHOPIFY_STORE_DOMAIN")  # e.g. "jc23eb-c7.myshopify.com"
API_VERSION  = os.getenv("SHOPIFY_API_VERSION", "2023-10")

if not all([API_KEY, ADMIN_TOKEN, STORE_DOMAIN]):
    raise ValueError("Missing one of SHOPIFY_API_KEY, SHOPIFY_ADMIN_TOKEN, or SHOPIFY_STORE_DOMAIN")

# Build the session URL
session_url = f"https://{API_KEY}:{ADMIN_TOKEN}@{STORE_DOMAIN}/admin/api/{API_VERSION}"
shopify.ShopifyResource.set_site(session_url)

def create_product(title: str, body_html: str, price: str, image_url: str):
    """
    Create a new Shopify product.
    """
    product = shopify.Product()
    product.title = title
    product.body_html = body_html
    product.variants = [shopify.Variant({"price": price})]
    product.images   = [shopify.Image({"src": image_url})]
    success = product.save()
    if not success:
        raise Exception(f"Shopify error: {product.errors.full_messages()}")
    return product.to_dict()

def fetch_recent_orders(limit: int = 5):
    """
    Return a list of the most recent orders.
    """
    orders = shopify.Order.find(limit=limit, status="any")
    return [o.to_dict() for o in orders]