Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Objective: To get current oil price in Thailand via SOAP and response in JSON. prerequisite: Python 3.4, flask, flask-cors, suds-jurko, xmltodict
#!/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')
@msomi22

This comment has been minimized.

Copy link

@msomi22 msomi22 commented Dec 13, 2016

Amazing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment