You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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;
}
}
The text was updated successfully, but these errors were encountered:
derms
changed the title
Simplify calling REST API for rest API extensions
Simplify calling REST API extensions
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
The text was updated successfully, but these errors were encountered: