From 3321ed3519bb7225f47ab97d384ce4aba4cd3cf2 Mon Sep 17 00:00:00 2001 From: stevenzack Date: Fri, 14 Aug 2020 19:19:04 +0800 Subject: [PATCH] multi-http-method-support --- tools/goctl/api/ktgen/funcs.go | 5 ++++ tools/goctl/api/ktgen/gen.go | 45 ++++++++-------------------------- 2 files changed, 15 insertions(+), 35 deletions(-) diff --git a/tools/goctl/api/ktgen/funcs.go b/tools/goctl/api/ktgen/funcs.go index 60c7f53e..84060dd3 100644 --- a/tools/goctl/api/ktgen/funcs.go +++ b/tools/goctl/api/ktgen/funcs.go @@ -13,6 +13,7 @@ var funcsMap = template.FuncMap{ "pathToFuncName": pathToFuncName, "parseType": parseType, "add": add, + "upperCase": upperCase, } func lowCamelCase(s string) string { @@ -69,3 +70,7 @@ func parseType(t string) string { func add(a, i int) int { return a + i } + +func upperCase(s string) string { + return strings.ToUpper(s) +} diff --git a/tools/goctl/api/ktgen/gen.go b/tools/goctl/api/ktgen/gen.go index 45f5eb75..585eeefa 100644 --- a/tools/goctl/api/ktgen/gen.go +++ b/tools/goctl/api/ktgen/gen.go @@ -1,6 +1,7 @@ package ktgen import ( + "fmt" "log" "os" "path/filepath" @@ -24,18 +25,18 @@ import java.net.URL const val SERVER = "http://localhost:8080" -suspend fun apiPost( +suspend fun apiRequest( + method:String, uri: String, - body: Any, + body: Any="", onOk: ((String) -> Unit)? = null, onFail: ((String) -> Unit)? = null, eventually: (() -> Unit)? = null ) = withContext(Dispatchers.IO) { val url = URL(SERVER + uri) with(url.openConnection() as HttpURLConnection) { - requestMethod = "POST" + requestMethod = method headerFields["Content-Type"] = listOf("Application/json") - val data = when (body) { is String -> { body @@ -60,33 +61,6 @@ suspend fun apiPost( } eventually?.invoke() } - -suspend fun apiGet( - uri: String, - onOk: ((String) -> Unit)? = null, - onFail: ((String) -> Unit)? = null, - eventually: (() -> Unit)? = null -) = withContext(Dispatchers.IO) { - val url = URL(SERVER + uri) - with(url.openConnection() as HttpURLConnection) { - requestMethod = "POST" - headerFields["Content-Type"] = listOf("Application/json") - - val wr = OutputStreamWriter(outputStream) - wr.flush() - - //response - BufferedReader(InputStreamReader(inputStream)).use { - val response = it.readText() - if (responseCode == 200) { - onOk?.invoke(response) - } else { - onFail?.invoke(response) - } - } - } - eventually?.invoke() -} ` apiTemplate = `package {{with .Info}}{{.Title}}{{end}} @@ -98,14 +72,14 @@ object Api{ val {{with $item}}{{lowCamelCase .Name}}: {{parseType .Type}}{{end}}{{if ne $i (add $length -1)}},{{end}}{{end}} ){{end}} {{with .Service}} - {{range .Routes}}suspend fun {{pathToFuncName .Path}}({{if ne .Method "get"}} - req:{{with .RequestType}}{{.Name}},{{end}}{{end}} + {{range .Routes}}suspend fun {{pathToFuncName .Path}}({{with .RequestType}}{{if ne .Name ""}} + req:{{.Name}},{{end}}{{end}} onOk: (({{with .ResponseType}}{{.Name}}{{end}}) -> Unit)? = null, onFail: ((String) -> Unit)? = null, eventually: (() -> Unit)? = null ){ - api{{if eq .Method "get"}}Get{{else}}Post{{end}}("{{.Path}}",{{if ne .Method "get"}}req,{{end}} onOk = { - onOk?.invoke(Gson().fromJson(it,{{with .ResponseType}}{{.Name}}{{end}}::class.java)) + apiRequest("{{upperCase .Method}}","{{.Path}}",{{with .RequestType}}{{if ne .Name ""}}body=req,{{end}}{{end}} onOk = { {{with .ResponseType}} + onOk?.invoke({{if ne .Name ""}}Gson().fromJson(it,{{.Name}}::class.java){{end}}){{end}} }, onFail = onFail, eventually =eventually) } {{end}}{{end}} @@ -119,6 +93,7 @@ func genBase(dir, pkg string, api *spec.ApiSpec) error { } path := filepath.Join(dir, "BaseApi.kt") if _, e := os.Stat(path); e == nil { + fmt.Println("BaseApi.kt already exists, skipped it.") return nil }