Skip to content

Commit 5f2d456

Browse files
committed
#359 New CallResourceTask for creating tasks that talk to resources
1 parent a2078e6 commit 5f2d456

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.marklogic.gradle.task.client
2+
3+
import com.marklogic.gradle.task.MarkLogicTask
4+
import org.gradle.api.tasks.TaskAction
5+
6+
/**
7+
* Intended to simplify calling a custom resource - e.g. something under /v1/resources/(resourceName).
8+
*/
9+
class CallResourceTask extends MarkLogicTask {
10+
11+
def params = [:]
12+
def method = "GET"
13+
def client = newClient()
14+
def mimeType = "application/json"
15+
String resourceName
16+
String body
17+
String outputFilePath
18+
19+
@TaskAction
20+
void callResource() {
21+
def mgr = new GenericResourceManager()
22+
if (resourceName == null || resourceName.trim().length() == 0) {
23+
throw new IllegalArgumentException("Must set the value of the 'resourceName' property on this task to the name of the resource to connect to")
24+
}
25+
26+
if (client == null) {
27+
client = newClient()
28+
}
29+
30+
client.init(resourceName, mgr)
31+
method = method.toUpperCase()
32+
33+
try {
34+
switch (method) {
35+
case "GET": handleOutput(mgr.get(params)); break
36+
case "POST": handleOutput(mgr.post(params, body, mimeType)); break
37+
case "PUT": handleOutput(mgr.put(params, body, mimeType)); break
38+
case "DELETE": handleOutput(mgr.delete(params)); break
39+
default: throw new IllegalArgumentException("Unsupported method " + method);
40+
}
41+
} finally {
42+
client.release()
43+
}
44+
}
45+
46+
def handleOutput(result) {
47+
if (outputFilePath) {
48+
writeToFile(result)
49+
} else {
50+
println result
51+
}
52+
}
53+
54+
def writeToFile(result) {
55+
def file = new File(outputFilePath)
56+
if (file.parentFile) {
57+
file.parentFile.mkdirs()
58+
}
59+
file.write result
60+
println "Wrote output to " + outputFilePath;
61+
}
62+
63+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.marklogic.gradle.task.client
2+
3+
import com.marklogic.client.extensions.ResourceManager
4+
import com.marklogic.client.io.StringHandle
5+
import com.marklogic.client.util.RequestParameters
6+
7+
class GenericResourceManager extends ResourceManager {
8+
9+
def get(Map<String, ?> params) {
10+
return services.get(buildRequestParameter(params), new StringHandle()).get();
11+
}
12+
13+
def post(params, body, mimeType) {
14+
return services.post(buildRequestParameter(params), new StringHandle(body).withMimetype(mimeType), new StringHandle()).get();
15+
}
16+
17+
def put(params, body, mimeType) {
18+
return services.put(buildRequestParameter(params), new StringHandle(body).withMimetype(mimeType), new StringHandle()).get();
19+
}
20+
21+
def delete(params) {
22+
return services.delete(buildRequestParameter(params), new StringHandle()).get();
23+
}
24+
25+
def buildRequestParameter(Map<String, ?> params) {
26+
def requestParams = new RequestParameters()
27+
for (String key : params.keySet()) {
28+
requestParams.put(key, params.get(key))
29+
}
30+
return requestParams
31+
}
32+
}

0 commit comments

Comments
 (0)