-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtutorial_4_webhooks.py
More file actions
113 lines (91 loc) · 3.52 KB
/
tutorial_4_webhooks.py
File metadata and controls
113 lines (91 loc) · 3.52 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from flask import Flask, render_template, request, redirect, Response, session
from config import Config as cfg
import requests
import json
app = Flask(__name__, template_folder="templates")
app.debug = True
app.secret_key = cfg.SECRET_KEY
def get_registered_webhooks_for_shop():
headers = {
"X-Shopify-Access-Token": session.get("access_token"),
"Content-Type": "application/json"
}
get_webhooks_response = requests.get("https://" + session.get("shop") +
"/admin/webhooks.json",
headers=headers)
if get_webhooks_response.status_code == 200:
webhooks = json.loads(get_webhooks_response.text)
print(webhooks)
return webhooks
else:
return False
@app.route('/webhooks', methods=['GET', 'POST'])
def webhooks():
if request.method == "GET":
return render_template('webhooks.html',
webhooks=get_registered_webhooks_for_shop())
else:
webhook_data = json.loads(request.data)
print("Title: {0}".format(webhook_data.get("title")))
return Response(status=200)
@app.route('/register_webhook', methods=['GET'])
def register_webhook():
headers = {
"X-Shopify-Access-Token": session.get("access_token"),
"Content-Type": "application/json"
}
payload = {
"webhook": {
"topic": "products/update",
"address": "https://{0}/webhooks".format(cfg.HOST),
"format": "json"
}
}
response = requests.post("https://" + session.get("shop")
+ "/admin/webhooks.json",
data=json.dumps(payload), headers=headers)
if response.status_code == 201:
return render_template('register_webhook.html',
webhook_response=json.loads(response.text))
else:
return Response(response="{0} - {1}".format(response.status_code,
response.text), status=200)
@app.route('/install', methods=['GET'])
def install():
"""
Connect a shopify store
"""
if request.args.get('shop'):
shop = request.args.get('shop')
else:
return Response(response="Error:parameter shop not found", status=500)
auth_url = "https://{0}/admin/oauth/authorize?client_id={1}&scope={2}&redirect_uri={3}".format(
shop, cfg.SHOPIFY_CONFIG["API_KEY"], cfg.SHOPIFY_CONFIG["SCOPE"],
cfg.SHOPIFY_CONFIG["REDIRECT_URI"]
)
print("Debug - auth URL: ", auth_url)
return redirect(auth_url)
@app.route('/connect', methods=['GET'])
def connect():
if request.args.get("shop"):
params = {
"client_id": cfg.SHOPIFY_CONFIG["API_KEY"],
"client_secret": cfg.SHOPIFY_CONFIG["API_SECRET"],
"code": request.args.get("code")
}
resp = requests.post(
"https://{0}/admin/oauth/access_token".format(
request.args.get("shop")
),
data=params
)
if 200 == resp.status_code:
resp_json = json.loads(resp.text)
session['access_token'] = resp_json.get("access_token")
session['shop'] = request.args.get("shop")
return render_template('welcome.html', from_shopify=resp_json)
else:
print "Failed to get access token: ", resp.status_code, resp.text
return render_template('error.html')
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)