Rename shopify_example.py to shopify_client.py
Browse files- shopify_client.py +30 -0
- shopify_example.py +0 -32
shopify_client.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# shopify_client.py
|
2 |
+
import shopify
|
3 |
+
import os
|
4 |
+
|
5 |
+
SHOP_URL = "https://863c42fc4fc6eb9dd18c84dacb21374e:[email protected]/admin"
|
6 |
+
API_VERSION = "2023-10"
|
7 |
+
|
8 |
+
# Configure
|
9 |
+
shopify.ShopifyResource.set_site(f"{SHOP_URL}/api/{API_VERSION}")
|
10 |
+
|
11 |
+
def create_product(title: str, body_html: str, price: str, image_url: str):
|
12 |
+
new_product = shopify.Product()
|
13 |
+
new_product.title = title
|
14 |
+
new_product.body_html = body_html
|
15 |
+
new_product.variants = [shopify.Variant({'price': price})]
|
16 |
+
new_product.images = [shopify.Image({'src': image_url})]
|
17 |
+
success = new_product.save()
|
18 |
+
if not success:
|
19 |
+
raise Exception(new_product.errors.full_messages())
|
20 |
+
return new_product.to_dict()
|
21 |
+
|
22 |
+
if __name__ == "__main__":
|
23 |
+
# Example usage:
|
24 |
+
prod = create_product(
|
25 |
+
title="AI Fitness Mirror Decal",
|
26 |
+
body_html="<strong>AI‑powered form correction in your own mirror.</strong>",
|
27 |
+
price="49.99",
|
28 |
+
image_url="https://example.com/decal.jpg"
|
29 |
+
)
|
30 |
+
print("Created product:", prod["id"])
|
shopify_example.py
DELETED
@@ -1,32 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import requests
|
3 |
-
|
4 |
-
# 1) Read from env
|
5 |
-
SHOP = os.getenv("SHOPIFY_STORE")
|
6 |
-
TOKEN = os.getenv("SHOPIFY_TOKEN")
|
7 |
-
if not SHOP or not TOKEN:
|
8 |
-
raise RuntimeError("Set SHOPIFY_STORE and SHOPIFY_TOKEN environment variables")
|
9 |
-
|
10 |
-
# 2) Prepare headers
|
11 |
-
headers = {
|
12 |
-
"Content-Type": "application/json",
|
13 |
-
"X-Shopify-Access-Token": TOKEN
|
14 |
-
}
|
15 |
-
|
16 |
-
# 3) Choose your API version & resource
|
17 |
-
API_VERSION = "2025-04"
|
18 |
-
resource = "products" # could also be "orders", "customers", etc.
|
19 |
-
|
20 |
-
# 4) Build URL
|
21 |
-
url = f"https://{SHOP}/admin/api/{API_VERSION}/{resource}.json"
|
22 |
-
|
23 |
-
# 5) Make the request
|
24 |
-
response = requests.get(url, headers=headers)
|
25 |
-
response.raise_for_status() # will raise if status != 200
|
26 |
-
|
27 |
-
# 6) Parse and print
|
28 |
-
data = response.json()
|
29 |
-
print(f"Fetched {len(data.get(resource, []))} {resource}:")
|
30 |
-
for item in data.get(resource, []):
|
31 |
-
# For products, print id and title
|
32 |
-
print(f" • {item['id']}: {item.get('title')}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|