# 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]