3.3 API 開發案例講解

本節要點

  • API開發案例(資料處理)
  • API開發案例(函數調研)

課前準備

課程內容

API開發案例(資料處理)

from flask import Flask, request
import json
app = Flask(__name__)     
@app.route('/test/', methods=['POST'])    #因為 Jodoo 只接收POST請求
def hello_world():
    alldata = json.loads(request.data)
    x = alldata['data']['x']
    y = alldata['data']['y']
    z = alldata['data']['z']
    
    if x==10:
        print(x*y*z)
    return 'success',200
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=3100)

API開發案例(函數調用)

from flask import Flask, request
import json
import requests
import threading
app = Flask(__name__)     
def voice_message(mobile,tpl):
    url = "http://yuyintz.market.alicloudapi.com/ts/voiceNotifySms?mobile=" + mobile + "&tpl_id=" + tpl
    payload = {}
    headers = {
	  'Authorization': 'APPCODE c467a7fd7cf4400a8ac6b6b43504fa75'
    }
    response = requests.request("POST", url, headers=headers, data = payload)
    print(response.text)
@app.route('/test/', methods=['POST'])    #因為 Jodoo 只接收POST請求
def hello_world():
    alldata = json.loads(request.data)
    x = alldata['data']['x']
    y = alldata['data']['y']
    threading.Thread(target=voice_message,args=(x,y,)).start()	
    return 'success',200
if __name__ == '__main__':
    app.run(host='0.0.0.0',port=3100)

這項資訊有幫助嗎?
需要更多協助嗎?聯繋技術支援。