February 20, 201213 yr 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 }
February 20, 201213 yr Works great! Any chance you'll run into a version that gets you the rate at a specific date?
December 7, 201213 yr 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
December 7, 201213 yr 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 }
December 8, 201213 yr Author 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 }
December 8, 201213 yr Author 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 ) )
Create an account or sign in to comment