Download shopify_client.py from mgbam/AICEO: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/mgbam/AICEO/resolve/65bc4b3fef9d0b034ce27a94f90c7dcf6805c0d9/shopify_client.py
- Command line
-
hf download hf://spaces/mgbam/AICEO@65bc4b3fef9d0b034ce27a94f90c7dcf6805c0d9/shopify_client.py
-
curl -L -o shopify_client.py https://huggingface.co/spaces/mgbam/AICEO/resolve/65bc4b3fef9d0b034ce27a94f90c7dcf6805c0d9/shopify_client.py
1.36 kB
| # 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] | |