Forked from mildronize/Simple soap client and simple server via flask
Created
February 12, 2016 15:13
-
-
Save mswell/7c5ed84ca8b587cd39b2 to your computer and use it in GitHub Desktop.
Objective: To get current oil price in Thailand via SOAP and response in JSON. prerequisite: Python 3.4, flask, flask-cors, suds-jurko, xmltodict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
from flask import Flask | |
from flask import request, jsonify | |
from flask.ext.cors import CORS | |
from suds.client import Client | |
import xmltodict | |
app = Flask(__name__) | |
cors = CORS(app) | |
@app.route("/apis/get-oil-price" , methods=['POST']) | |
def get_oil_price(): | |
# Get variable from http POST | |
date_string = str(request.form['date']) | |
date_string = date_string.split('-') | |
day = date_string[2] | |
month = date_string[1] | |
year = date_string[0] | |
# Get SOAP Service via suds | |
url = 'http://www.pttplc.com/webservice/pttinfo.asmx?WSDL' | |
client = Client(url) | |
# Execute GetOilPrice method of SOAP | |
xml = client.service.GetOilPrice("EN", day, month, year) | |
# Convert XML to dict | |
res_dict = xmltodict.parse(xml) | |
result = {} | |
result['result'] = res_dict['PTT_DS']['DataAccess'] | |
# Convert dict to JSON | |
return jsonify(**result) | |
@app.route("/apis/current-oil-price" , methods=['GET']) | |
def oil_current_price(): | |
# Get SOAP Service via suds | |
url = 'http://www.pttplc.com/webservice/pttinfo.asmx?WSDL' | |
client = Client(url) | |
# Execute CurrentOilPrice method of SOAP | |
xml = client.service.CurrentOilPrice("EN") | |
# Convert XML to dict | |
res_dict = xmltodict.parse(xml) | |
result = {} | |
result['result'] = res_dict['PTT_DS']['DataAccess'] | |
# Convert dict to JSON | |
return jsonify(**result) | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment