Newer
Older
drafter-groovy / src / uk / org / gss_data / pmd_drafter / api / ApiUtils.groovy
package uk.org.gss_data.pmd_drafter.api;

@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.2')

import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import static groovyx.net.http.ContentType.JSON
import static java.net.URI.create;

class ApiUtils {

    def invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, auth, method, container, type)  {
        def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
        println "url=$url uriPath=$uriPath"
        def http = new HTTPBuilder(url)
        http.request( Method.valueOf(method), JSON ) { req ->
            uri.path = uriPath
            uri.query = queryParams
            headers.Authorization = auth
            response.success = { resp, json ->
                if (type != null) {
                    onSuccess(parse(json, container, type))
                }
            }
            response.failure = { resp ->
                onFailure(resp.status, resp.statusLine.reasonPhrase)
            }
        }
    }


    def buildUrlAndUriPath(basePath, versionPath, resourcePath) {
        // HTTPBuilder expects to get as its constructor parameter an URL,
        // without any other additions like path, therefore we need to cut the path
        // from the basePath as it is represented by swagger APIs
        // we use java.net.URI to manipulate the basePath
        // then the uriPath will hold the rest of the path
        URI baseUri =  create(basePath)
        def pathOnly = baseUri.getPath()
        [basePath-pathOnly, pathOnly+versionPath+resourcePath]
    }


    def parse(object, container, clazz) {
        if ((container == "List") || (container == "array")) {
            return object.collect {parse(it, "", clazz)}
        }   else {
                return clazz.newInstance(object)
        }
    }

}