11package { {invokerPackage} }
22
3+ { {#withXml} }
4+ import java.io.StringReader
5+ import { {javaxPackage} }.xml.bind.JAXB
6+ { {/withXml} }
37import groovy.json.JsonBuilder
48import groovy.json.JsonGenerator
59import groovyx.net.http.ChainedHttpConfig
610import groovyx.net.http.ContentTypes
711import groovyx.net.http.NativeHandlers
12+ import groovyx.net.http.FromServer
813import groovyx.net.http.ToServer
914
1015import static groovyx.net.http.HttpBuilder.configure
@@ -18,36 +23,59 @@ class ApiUtils {
1823 }
1924 .build()
2025
21- void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, contentType, method, container, type) {
26+ void invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, bodyParams, accept, contentType, method, container, type) {
2227 def (url, uriPath) = buildUrlAndUriPath(basePath, versionPath, resourcePath)
2328 println " url=$url uriPath=$uriPath"
2429 def http = configure {
2530 request.uri = url
2631 request.uri.path = uriPath
2732 request.encoder(ContentTypes.JSON, { final ChainedHttpConfig config, final ToServer ts ->
28- final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest();
33+ final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
2934 if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
30- return;
35+ return
3136 }
3237
33- final Object body = NativeHandlers.Encoders.checkNull(request.actualBody());
38+ final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
3439 final String json = ((body instanceof String || body instanceof GString)
3540 ? body.toString()
36- : new JsonBuilder(body, jsonGenerator).toString());
37- ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()));
41+ : new JsonBuilder(body, jsonGenerator).toString())
42+ ts.toServer(NativeHandlers.Encoders.stringToStream(json, request.actualCharset()))
3843 })
44+ { {#withXml} }
45+ request.encoder(ContentTypes.XML, { final ChainedHttpConfig config, final ToServer ts ->
46+ final ChainedHttpConfig.ChainedRequest request = config.getChainedRequest()
47+ if (NativeHandlers.Encoders.handleRawUpload(config, ts)) {
48+ return
49+ }
50+
51+ final Object body = NativeHandlers.Encoders.checkNull(request.actualBody())
52+ String xml
53+ if (body instanceof String || body instanceof GString) {
54+ xml = body.toString()
55+ } else {
56+ StringWriter writer = new StringWriter()
57+ JAXB.marshal(body, writer)
58+ xml = writer.toString()
59+ }
60+ ts.toServer(NativeHandlers.Encoders.stringToStream(xml, request.actualCharset()))
61+ }){ {/withXml} }
3962 }
4063 .invokeMethod(String.valueOf(method).toLowerCase()) {
4164 request.uri.query = queryParams
4265 request.headers = headerParams
4366 if (bodyParams != null) {
4467 request.body = bodyParams
4568 }
69+ request.accept = accept
4670 request.contentType = contentType
47-
48- response.success { resp, json ->
71+ { {#withXml} }
72+ response.parser(ContentTypes.XML) { final ChainedHttpConfig cfg, final FromServer fs ->
73+ fs.inputStream.text
74+ }
75+ { {/withXml} }
76+ response.success { resp, body ->
4977 if (type != null) {
50- onSuccess(parse(json , container, type))
78+ onSuccess(parse(resp, body , container, type))
5179 }
5280 }
5381 response.failure { resp ->
@@ -68,12 +96,24 @@ class ApiUtils {
6896 [basePath-pathOnly, pathOnly+versionPath+resourcePath]
6997 }
7098
71- private def parse(object, container, clazz) {
99+ private def parse(response, object, container, clazz) {
100+ {{#withXml} }
101+ if (response.getContentType().toLowerCase().contains("xml")) {
102+ return JAXB.unmarshal(new StringReader(object), clazz)
103+ }
104+ { {/withXml} }
72105 if (container == "array") {
73- return object.collect {parse(it, " " , clazz)}
74- } else {
106+ return object.collect { parse(response, it, " " , clazz) }
107+ } else {
75108 return clazz.newInstance(object)
76109 }
77110 }
78111
112+ private def selectHeaderAccept(accepts) {
113+ def jsonMime = ' application/json'
114+ if (accepts.find { it.toLowerCase().startsWith(jsonMime) } ) {
115+ return [jsonMime]
116+ }
117+ return accepts
118+ }
79119}
0 commit comments