Shopify 不会停止向 Flask 应用程序发送 webhook
Shopify doesn't stop sending webhook to Flask app
我有一个 Flask 应用程序可以侦听传入的 Shopify webhook,执行某些操作并以 200 进行响应。但是,Shopify 一直发送 webhook,就好像我的应用程序以 200 以外的其他内容进行响应。
当我用 curl 模拟时:
curl -H "Content-Type: application/json" -D /tmp/dump.log -X POST -d @<valid json> <my server>
响应headers是:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 16 Apr 2015 09:25:23 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 0
Content-Type: text/html; charset=utf-8
我认为第一个 100 Continue
是让 Shopify 认为我的应用失败的原因。我在本地实例和生产实例上得到了相同的响应。
这是怎么回事,我该如何解决?
编辑
这是处理请求的 python 代码
import json
from flask_mail import Mail, Message
from pyPdf import PdfFileWriter, PdfFileReader
from flask import Flask
from certificate import Certificate
from flask import request
from purchase import Purchase
from stars import Stars
app = Flask(__name__)
app.config.from_envvar('NAMESTAR_SETTINGS')
app.config["MAIL_SERVER"] = 'smtp.sendgrid.net'
app.config["MAIL_PORT"] = 587
app.config["MAIL_USERNAME"] = app.config.get('EMAIL_LOGIN')
app.config["MAIL_PASSWORD"] = app.config.get('EMAIL_PASSWORD')
app.config["MAIL_DEFAULT_SENDER"] = app.config.get('SENDER')
app.config["PATH"] = app.config.get('PATH')
ADMINS = app.config.get('ADMINS')
mail = Mail(app)
def get_request():
if request.args.get('hyg') is not None:
return int(request.args.get('hyg'))
else:
return request.data
#returns an instance of Purchase class based on the request
def get_purchase():
pass
#creates and saves a PDF created with reportlab
def generate_certificate(purchase):
pass
#sends the generated PDF to the recipient
def send_email(certificate_path, recipient, name=""):
msg = Message("Message", recipients=[recipient])
with app.open_resource(certificate_path) as fp:
msg.attach("certificate.pdf", "application/pdf", fp.read())
f = open(app.config.get('PATH') + "/email_templates/certificate", "r")
html = f.read()
msg.html = html % (name)
mail.send(msg)
@app.route('/', methods=['GET', 'POST'])
def generate():
try:
purchase = get_purchase()
certificate_path = generate_certificate(purchase)
send_email(certificate_path, purchase.customer_email(), name=purchase.customer_name())
except Exception as e:
raise
return ('', 200)
if __name__ == '__main__':
app.debug = False
app.run(debug=False)
服务器是Apache,这里是应用程序的虚拟主机设置
<VirtualHost *:80>
DocumentRoot /var/www/namestar/current
WSGIDaemonProcess namestar user=www-data group=www-data threads=5
WSGIScriptAlias / /var/www/namestar/current/namestar.wsgi
<Directory /var/www/namestar/current>
WSGIScriptReloading On
WSGIProcessGroup namestar
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
SetEnv NAMESTAR_SETTINGS="/var/www/namestar/current/config/production.cfg"
</VirtualHost>
Shopify 表示请求超时。事实上,处理过程需要 10 到 20 秒,所以这是有道理的。我将大部分执行移到了它自己的线程中:
threading.Thread(target=generate, args=[purchase]).start()
一切正常。
我有一个 Flask 应用程序可以侦听传入的 Shopify webhook,执行某些操作并以 200 进行响应。但是,Shopify 一直发送 webhook,就好像我的应用程序以 200 以外的其他内容进行响应。
当我用 curl 模拟时:
curl -H "Content-Type: application/json" -D /tmp/dump.log -X POST -d @<valid json> <my server>
响应headers是:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 16 Apr 2015 09:25:23 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 0
Content-Type: text/html; charset=utf-8
我认为第一个 100 Continue
是让 Shopify 认为我的应用失败的原因。我在本地实例和生产实例上得到了相同的响应。
这是怎么回事,我该如何解决?
编辑
这是处理请求的 python 代码
import json
from flask_mail import Mail, Message
from pyPdf import PdfFileWriter, PdfFileReader
from flask import Flask
from certificate import Certificate
from flask import request
from purchase import Purchase
from stars import Stars
app = Flask(__name__)
app.config.from_envvar('NAMESTAR_SETTINGS')
app.config["MAIL_SERVER"] = 'smtp.sendgrid.net'
app.config["MAIL_PORT"] = 587
app.config["MAIL_USERNAME"] = app.config.get('EMAIL_LOGIN')
app.config["MAIL_PASSWORD"] = app.config.get('EMAIL_PASSWORD')
app.config["MAIL_DEFAULT_SENDER"] = app.config.get('SENDER')
app.config["PATH"] = app.config.get('PATH')
ADMINS = app.config.get('ADMINS')
mail = Mail(app)
def get_request():
if request.args.get('hyg') is not None:
return int(request.args.get('hyg'))
else:
return request.data
#returns an instance of Purchase class based on the request
def get_purchase():
pass
#creates and saves a PDF created with reportlab
def generate_certificate(purchase):
pass
#sends the generated PDF to the recipient
def send_email(certificate_path, recipient, name=""):
msg = Message("Message", recipients=[recipient])
with app.open_resource(certificate_path) as fp:
msg.attach("certificate.pdf", "application/pdf", fp.read())
f = open(app.config.get('PATH') + "/email_templates/certificate", "r")
html = f.read()
msg.html = html % (name)
mail.send(msg)
@app.route('/', methods=['GET', 'POST'])
def generate():
try:
purchase = get_purchase()
certificate_path = generate_certificate(purchase)
send_email(certificate_path, purchase.customer_email(), name=purchase.customer_name())
except Exception as e:
raise
return ('', 200)
if __name__ == '__main__':
app.debug = False
app.run(debug=False)
服务器是Apache,这里是应用程序的虚拟主机设置
<VirtualHost *:80>
DocumentRoot /var/www/namestar/current
WSGIDaemonProcess namestar user=www-data group=www-data threads=5
WSGIScriptAlias / /var/www/namestar/current/namestar.wsgi
<Directory /var/www/namestar/current>
WSGIScriptReloading On
WSGIProcessGroup namestar
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
SetEnv NAMESTAR_SETTINGS="/var/www/namestar/current/config/production.cfg"
</VirtualHost>
Shopify 表示请求超时。事实上,处理过程需要 10 到 20 秒,所以这是有道理的。我将大部分执行移到了它自己的线程中:
threading.Thread(target=generate, args=[purchase]).start()
一切正常。