소스 검색

multi-http-method-support

stevenzack 4 년 전
부모
커밋
3321ed3519
2개의 변경된 파일15개의 추가작업 그리고 35개의 파일을 삭제
  1. 5 0
      tools/goctl/api/ktgen/funcs.go
  2. 10 35
      tools/goctl/api/ktgen/gen.go

+ 5 - 0
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)
+}

+ 10 - 35
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
 	}