Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify calling REST API extensions #359

Closed
derms opened this issue Jun 18, 2018 · 1 comment
Closed

Simplify calling REST API extensions #359

derms opened this issue Jun 18, 2018 · 1 comment
Assignees
Milestone

Comments

@derms
Copy link
Contributor

derms commented Jun 18, 2018

Currently it is cumbersome to call REST API extensions as part of a gradle task that uses a MarkLogic database client. A lot of boilerplate code needs to be written.

The example below shows the amount of "helper" / boilerplate code that needs to be written in order to get a simplified gradle task definition.

It would be great if a lot of this boilerplate code could be included in the gradle plugin. I'm unsure if any of the "contract first" development work that is currently being done could be of use for this

// test to execute tests
task mlJasmineTests (type: CallRESTExtentionTask) {
    extName = "ml-jasmine-junit"
    outputFile = "build/test-results/marklogic-jasmine-test/junit-results.xml"
}


/**
 * Helper classes required for rest extension calls
 */
import com.marklogic.client.extensions.ResourceManager;
import com.marklogic.client.util.RequestParameters;
import com.marklogic.client.io.StringHandle;
class RESTExtension extends ResourceManager {
    String name ="NONE"
    def requestParams = new RequestParameters()

    def get(params) {
        requestParams.putAll(params)
        return services.get(requestParams, new StringHandle());
    }

    def post(params,body,mimeType) {
        requestParams.putAll(params)
        return services.post(requestParams, new StringHandle(body).withMimetype(mimeType), new StringHandle());
    }

    def put(params,body,mimeType) {
        requestParams.putAll(params)
        return services.put(requestParams, new StringHandle(body).withMimetype(mimeType), new StringHandle());
    }

    def delete(params) {
        requestParams.putAll(params)
        return services.delete(requestParams, new StringHandle());
    }
}

import org.gradle.api.tasks.TaskAction
class CallRESTExtentionTask extends com.marklogic.gradle.task.MarkLogicTask {
    def params = [:]
    def method = "GET"
    def client = newClient()
    def mimeType = "text/plain"
    String extName = "NONE" 
    String body 
    String result
    String outputFile

    @TaskAction
    String call() {
        def ext = new RESTExtension(name: extName)
        client.init(extName, ext);
        switch(method) {
            case "GET": result = ext.get(params); handleOutput(result); break
            case "POST": result = ext.post(params,body,mimeType); handleOutput(result); break
            case "PUT":  result = ext.put(params,body,mimeType); handleOutput(result); break
            case "DELETE":  result = ext.delete(params); handleOutput(result); break
            default: throw new IllegalArgumentException("Unsupported method " + method);
        }
    }

    def handleOutput(result) {
        if (outputFile) {
            writeToFile(result)
        } else {
            println result
        }
    }

    def writeToFile(result) {
        def file = new File(outputFile)
        if (file.parentFile) {
            file.parentFile.mkdirs()
        }
        file.write result
        println "Wrote output to " + outputFile;
    }
}

@derms derms changed the title Simplify calling REST API for rest API extensions Simplify calling REST API extensions Jun 18, 2018
@rjrudin rjrudin modified the milestones: 3.80, 3.8.0 Jun 19, 2018
@rjrudin
Copy link
Contributor

rjrudin commented Jun 19, 2018

Thanks @derms - I have this working, will be in 3.8.0 - example:

task callEcho(type: com.marklogic.gradle.task.client.CallResourceTask) {
  resourceName = "echo"
  method = "GET"
  params = [text: ["Hello world", "And this"]]
}

@rjrudin rjrudin self-assigned this Jun 19, 2018
@rjrudin rjrudin closed this as completed Jun 19, 2018
rjrudin added a commit that referenced this issue Aug 23, 2024
This should have it in sync with ML 9.0-9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants