john renfrew Posted February 20, 2012 Posted February 20, 2012 Was playing around with the new XMLtemplateEngine in the latest Groovy today and offer the following function for returning currency conversion rates via SOAP //ExRates( from ; to; amount ) // 12_02_20 JR // v1.1 // adapted from Groovy API examples // from, to -> 3 letter currency codes // if amount is null returns rate else returns converted value import groovy.text.SimpleTemplateEngine as STE import groovy.xml.Namespace XMLtext = ''' <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ConversionRate xmlns="http://www.webserviceX.NET/"> <FromCurrency>${from}</FromCurrency> <ToCurrency>${to}</ToCurrency> </ConversionRate> </soap:Body> </soap:Envelope> ''' template = new STE().createTemplate(XMLtext) params = [from:from, to:to] request = template.make(params).toString().getBytes('UTF-8') url = 'http://www.webservicex.net/CurrencyConvertor.asmx' conn = new URL(url).openConnection() reqProps = [ 'Content-Type': 'text/xml; charset=UTF-8', 'SOAPAction' : 'http://www.webserviceX.NET/ConversionRate', 'Accept' : 'application/soap+xml, text/*' ] reqProps.each { key,value -> conn.addRequestProperty(key,value) } conn.requestMethod = 'POST' conn.doOutput = true conn.outputStream << new ByteArrayInputStream(request) if (conn.responseCode != conn.HTTP_OK) { return "Error - HTTP:${conn.responseCode}" } resp = new XmlParser().parse(conn.inputStream) serv = new Namespace('http://www.webserviceX.NET/') result = serv.ConversionRateResult if ( !amount){ return "Current " + params.from + " to " + params.to + " conversion rate: " + resp.depthFirst().find{result == it.name()}.text() } else { rate = resp.depthFirst().find{result == it.name()}.text() convert = amount.toInteger() * rate.toDouble() return 'That is ' + convert.round(2) + ' ' + cTo }
David Wikström Posted February 20, 2012 Posted February 20, 2012 Works great! Any chance you'll run into a version that gets you the rate at a specific date?
JerrySalem Posted December 7, 2012 Posted December 7, 2012 I tried to use this, and obviously I don't know how I used the PostXMLData module in SM Pasted 'http://www.webservicex.net/CurrencyConvertor.asmx' into the URL section Pasted the xml part above into the xml section put ExPrates(USD;CAD;100) into the soapaction. then the filemaker scriptmaster file hung Point me to how to use the function please? TIA Jerry
Karsten Wolf Posted December 7, 2012 Posted December 7, 2012 I tried to use this, and obviously I don't know how I used the PostXMLData module in SM Pasted 'http://www.webservicex.net/CurrencyConvertor.asmx' into the URL section Pasted the xml part above into the xml section put ExPrates(USD;CAD;100) into the soapaction. then the filemaker scriptmaster file hung Point me to how to use the function please? TIA Jerry Same here. After some debugging I came up with these modifications. A warning: sometimes the service gives a 500 error which is not caught here. //ExRates( from ; to; amount ) // 12_02_20 JR // v1.1 // adapted from Groovy API examples // from, to -> 3 letter currency codes // if amount is null returns rate else returns converted value // // kw 2012-12-07 some minor bugs fixed import groovy.text.SimpleTemplateEngine as STE import groovy.xml.Namespace XMLtext = '''<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ConversionRate xmlns="http://www.webserviceX.NET/"> <FromCurrency>${from}</FromCurrency> <ToCurrency>${to}</ToCurrency> </ConversionRate> </soap:Body> </soap:Envelope>''' dAmount = 0.0 if (amount != "") { dAmount = Double.parseDouble( amount ) } template = new STE().createTemplate(XMLtext) params = [from:from, to:to] request = template.make(params).toString().getBytes('UTF-8') url = 'http://www.webservicex.net/CurrencyConvertor.asmx' conn = new URL(url).openConnection() reqProps = [ 'Content-Type': 'text/xml; charset=UTF-8', 'SOAPAction' : 'http://www.webserviceX.NET/ConversionRate', 'Accept' : 'application/soap+xml, text/*' ] reqProps.each { key,value -> conn.addRequestProperty(key,value) } conn.requestMethod = 'POST' conn.doOutput = true conn.outputStream << new ByteArrayInputStream(request) if (conn.responseCode != conn.HTTP_OK) { return "Error - HTTP:${conn.responseCode}" } resp = new XmlParser().parse(conn.inputStream) serv = new Namespace('http://www.webserviceX.NET/') result = serv.ConversionRateResult if ( amount == "0" ){ return ( "Current " + params.from + " to " + params.to + " conversion rate: " + resp.depthFirst().find{result == it.name()}.text()) } else { rate = resp.depthFirst().find{result == it.name()}.text() convert = dAmount * rate.toDouble() return 'That is ' + convert.round(2) + ' ' + to }
john renfrew Posted December 8, 2012 Author Posted December 8, 2012 Thanks Karsten The Double addition is a good catch - here is more Groovy code for it if (amount){ dAmount = amount.toDouble() } else { dAmount = 0.0 } Not sure why you think the 500 error is not being caught... but if there is a need, just wrap conn = ... TO conn.outputStream... in a Try/Catch to add some extra safety try{ //code here } catch (e) { return e }
john renfrew Posted December 8, 2012 Author Posted December 8, 2012 Karsten This is a brand new function for your SM demo file, as is it a post to a SOAP wsdl so need more control than a plain post does. The SOAP action requires you to send <from> and <to>, it returns a rate which the function then uses to either convert the amount as an input variable or returns it as a number Follow the instructions for creating and registering from the 360 help and then you will have a new function to use Copy the code to the body, add three input variables and register the function then: Set Variable ( $result ; ExRates ( USD ; CAD ; 100 ) )
Recommended Posts
This topic is 4437 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now