Call a Web Service

#This script calls a webservice that parses and populates a Spotfire Document property from the returned JSON format.
from System.Net import WebClient
from System.Collections.Generic import Dictionary
from System.IO import StreamReader
from System.Web.Script.Serialization import JavaScriptSerializer

# get the json data from openexchangerates
url = 'http://openexchangerates.org/latest.json'
wc = WebClient()
wc.Headers.Add('Content-Type', 'application/x-www-form-urlencoded')
requestData= wc.OpenRead(url)
reader = StreamReader (requestData);
response = reader.ReadToEnd()

#Parse the results to get the exchange rates
js = JavaScriptSerializer()
exchangeRateDict = js.Deserialize(response,object)
gbpRate = float(exchangeRateDict["rates"]["GBP"])
eurRate = float(exchangeRateDict["rates"]["EUR"])
cadRate = float(exchangeRateDict["rates"]["CAD"])

#Set the Document Properties in the Spotfire file with the current rates
Document.Properties["PoundConversion"] = gbpRate
Document.Properties["EuroConversion"] = eurRate
Document.Properties["CADconversion"] = cadRate

Previous
Next Post »