Update shopify_client.py
Browse files- shopify_client.py +23 -19
shopify_client.py
CHANGED
@@ -3,36 +3,40 @@
|
|
3 |
import os
|
4 |
import shopify
|
5 |
|
6 |
-
#
|
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 =
|
11 |
|
12 |
-
if not
|
13 |
-
raise
|
14 |
|
15 |
-
# Build the
|
16 |
-
|
17 |
-
shopify.ShopifyResource.set_site(
|
18 |
|
19 |
-
|
|
|
20 |
"""
|
21 |
-
|
|
|
|
|
22 |
"""
|
|
|
23 |
product = shopify.Product()
|
24 |
product.title = title
|
25 |
-
product.body_html =
|
26 |
product.variants = [shopify.Variant({"price": price})]
|
27 |
-
|
|
|
|
|
|
|
28 |
success = product.save()
|
29 |
if not success:
|
30 |
-
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
"""
|
37 |
-
orders = shopify.Order.find(limit=limit, status="any")
|
38 |
-
return [o.to_dict() for o in orders]
|
|
|
3 |
import os
|
4 |
import shopify
|
5 |
|
6 |
+
# βββ Configuration βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
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 = "2023-10"
|
11 |
|
12 |
+
if not (API_KEY and ADMIN_TOKEN and STORE_DOMAIN):
|
13 |
+
raise RuntimeError("Shopify credentials not set in environment variables")
|
14 |
|
15 |
+
# Build the base URL for the Admin API
|
16 |
+
SHOP_URL = f"https://{API_KEY}:{ADMIN_TOKEN}@{STORE_DOMAIN}/admin/api/{API_VERSION}"
|
17 |
+
shopify.ShopifyResource.set_site(SHOP_URL)
|
18 |
|
19 |
+
# βββ Public Interface βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
20 |
+
def create_shopify_product(title: str, description: str, price: str, image_url: str = None) -> str:
|
21 |
"""
|
22 |
+
Creates a new product in Shopify with the given title, description, and price.
|
23 |
+
Optionally uploads a single image via URL.
|
24 |
+
Returns the public product URL on your store.
|
25 |
"""
|
26 |
+
# 1) Build the new product object
|
27 |
product = shopify.Product()
|
28 |
product.title = title
|
29 |
+
product.body_html = description
|
30 |
product.variants = [shopify.Variant({"price": price})]
|
31 |
+
if image_url:
|
32 |
+
product.images = [shopify.Image({"src": image_url})]
|
33 |
+
|
34 |
+
# 2) Save to Shopify
|
35 |
success = product.save()
|
36 |
if not success:
|
37 |
+
errors = product.errors.full_messages()
|
38 |
+
raise Exception(f"Shopify error: {errors}")
|
39 |
|
40 |
+
# 3) Construct the storefront URL from the product handle
|
41 |
+
handle = product.handle
|
42 |
+
return f"https://{STORE_DOMAIN}/products/{handle}"
|
|
|
|
|
|