rollback grovvy template

This commit is contained in:
wing328 2015-06-09 13:07:45 +08:00
parent 46c5b776ed
commit 900f39686d
4 changed files with 88 additions and 85 deletions

View File

@ -8,43 +8,43 @@ import static java.net.URI.create;
class ApiUtils {
def invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, 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 ) {
uri.path = uriPath
uri.query = queryParams
response.success = { resp, json ->
if (type != null) {
onSuccess(parse(json, container, type))
}
}
response.failure = { resp ->
onFailure(resp.status, resp.statusLine.reasonPhrase)
}
}
}
def invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams, 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 ) {
uri.path = uriPath
uri.query = queryParams
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 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") {
return object.collect {parse(it, "", clazz)}
} else {
return clazz.newInstance(object)
}
}
def parse(object, container, clazz) {
if (container == "List") {
return object.collect {parse(it, "", clazz)}
} else {
return clazz.newInstance(object)
}
}
}

View File

@ -1,6 +1,9 @@
package {{package}};
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
@ -14,40 +17,40 @@ import java.util.*;
@Mixin(ApiUtils)
{{#operations}}
class {{classname}} {
class {{classname}} {
String basePath = "{{basePath}}"
String versionPath = "/api/v1"
{{#operation}}
def {{nickname}} ({{#allParams}} {{{dataType}}} {{paramName}},{{/allParams}} Closure onSuccess, Closure onFailure) {
// create path and map variables
String resourcePath = "{{path}}"
{{#operation}}
def {{nickname}} ({{#allParams}} {{{dataType}}} {{paramName}},{{/allParams}} Closure onSuccess, Closure onFailure) {
// create path and map variables
String resourcePath = "{{path}}"
// query params
def queryParams = [:]
def headerParams = [:]
// query params
def queryParams = [:]
def headerParams = [:]
{{#requiredParamCount}}
// verify required params are set
if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
throw new RuntimeException("missing required params")
}
{{/requiredParamCount}}
{{#queryParams}}if(!"null".equals(String.valueOf({{paramName}})))
queryParams.put("{{paramName}}", String.valueOf({{paramName}}))
{{/queryParams}}
{{#headerParams}}headerParams.put("{{paramName}}", {{paramName}})
{{/headerParams}}
invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams,
"{{httpMethod}}", "{{returnContainer}}",
{{#returnBaseType}}{{{returnBaseType}}}.class {{/returnBaseType}}{{^returnBaseType}}null {{/returnBaseType}})
}
{{/operation}}
{{#requiredParamCount}}
// verify required params are set
if({{/requiredParamCount}}{{#requiredParams}} {{paramName}} == null {{#hasMore}}|| {{/hasMore}}{{/requiredParams}}{{#requiredParamCount}}) {
throw new RuntimeException("missing required params")
}
{{/requiredParamCount}}
{{#queryParams}}if(!"null".equals(String.valueOf({{paramName}})))
queryParams.put("{{paramName}}", String.valueOf({{paramName}}))
{{/queryParams}}
{{#headerParams}}headerParams.put("{{paramName}}", {{paramName}})
{{/headerParams}}
invokeApi(onSuccess, onFailure, basePath, versionPath, resourcePath, queryParams, headerParams,
"{{httpMethod}}", "{{returnContainer}}",
{{#returnBaseType}}{{{returnBaseType}}}.class {{/returnBaseType}}{{^returnBaseType}}null {{/returnBaseType}})
}
{{/operation}}
}
{{/operations}}

View File

@ -6,24 +6,24 @@ archivesBaseName = 'swagger-gen-groovy'
version = '0.1'
buildscript {
repositories {
maven { url 'http://repo.jfrog.org/artifactory/gradle-plugins' }
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.16')
}
repositories {
maven { url 'http://repo.jfrog.org/artifactory/gradle-plugins' }
}
dependencies {
classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.16')
}
}
repositories {
mavenCentral()
mavenLocal()
mavenCentral(artifactUrls: ['http://maven.springframework.org/milestone'])
maven { url "http://$artifactory:8080/artifactory/repo" }
mavenCentral()
mavenLocal()
mavenCentral(artifactUrls: ['http://maven.springframework.org/milestone'])
maven { url "http://$artifactory:8080/artifactory/repo" }
}
dependencies {
groovy "org.codehaus.groovy:groovy-all:2.0.5"
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.6'
groovy "org.codehaus.groovy:groovy-all:2.0.5"
compile 'org.codehaus.groovy.modules.http-builder:http-builder:0.6'
}

View File

@ -4,18 +4,18 @@ import groovy.transform.Canonical
{{#imports}}import {{import}};
{{/imports}}
{{#models}}
{{#model}}
@Canonical
class {{classname}} {
{{#vars}}
{{#model}}
@Canonical
class {{classname}} {
{{#vars}}
{{#description}}/* {{{description}}} */
{{/description}}
{{{datatype}}} {{name}} = {{{defaultValue}}}
{{/vars}}
{{#description}}/* {{{description}}} */
{{/description}}
{{{datatype}}} {{name}} = {{{defaultValue}}}
{{/vars}}
}
}
{{/model}}
{{/model}}
{{/models}}