cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Calling a Groovy script as an External Web Service

Highlighted
Garnet

Calling a Groovy script as an External Web Service

This script illustrates how to call a Groovy script as an external web service.  This example also applies to calling any external web service that relies on a username and password.

Parameters:

  1. external_username
  2. external_password
  3. script_name

import com.axeda.drm.sdk.Context

import com.axeda.drm.sdk.device.DeviceFinder

import com.axeda.drm.sdk.data.CurrentDataFinder

import com.axeda.drm.sdk.device.Device

import com.axeda.drm.sdk.data.HistoricalDataFinder

import com.axeda.drm.sdk.device.DataItem

import net.sf.json.JSONObject

import com.axeda.drm.sdk.device.ModelFinder

import groovyx.net.http.*

import static groovyx.net.http.ContentType.*

import static groovyx.net.http.Method.*

/**

* CallScriptoAsExternalWebService.groovy

*

* This script illustrates how to call a Groovy script as an external web service.

*

* @param external_username       -   (REQ):Str Username for the external web service.

* @param external_password       -   (REQ):Str Password for the external web service.

* @param script_name             -   (REQ):Str Script Name to call.

*

*

*/

def result

try {

validateParameters(actual: parameters, expected: ["external_username", "external_password", "script_name"])

// authentication tokens (username + password)

def auth_tokens = [username: parameters.external_username, password: parameters.external_password]

http = new HTTPBuilder( 'http://platform.axeda.com/services/v1/rest/Scripto/execute/'+parameters.script_name )

// pass in dummy parameters to the script for illustration

def parammap = [key1: "val1", key2: "val2"]

// Call the script

    http.request (GET, JSON) {

      uri.query = auth_tokens + parammap

      response.success = { resp, json ->

        // traverse the wrapped json response

    result = json.wsScriptoExecuteResponse.content.$

  

      }

      response.failure = { resp ->

        result = response.failure

      }

    

}

} catch (Throwable any) {

    logger.error any.localizedMessage

}

return ['Content-Type': 'application/json', 'Content': result]

static def validateParameters(Map args) {

    if (!args.containsKey("actual")) {

        throw new Exception("validateParameters(args) requires 'actual' key.")

    }

    if (!args.containsKey("expected")) {

        throw new Exception("validateParameters(args) requires 'expected' key.")

    }

    def config = [

            require_username: false

    ]

    Map actualParameters = args.actual.clone() as Map

    List expectedParameters = args.expected

    config.each { key, value ->

        if (args.options?.containsKey(key)) {

            config[key] = args.options[key]

        }

    }

    if (!config.require_username) { actualParameters.remove("username") }

    expectedParameters.each { paramName ->

        if (!actualParameters.containsKey(paramName) || !actualParameters[paramName]) {

            throw new IllegalArgumentException(

                    "Parameter '${paramName}' was not found in the query; '${paramName}' is a reqd. parameter.")

        }

    }

}

Tags (1)
Announcements