From e136deb3a7d36b1f3ea28a062360fe1fe68fbf53 Mon Sep 17 00:00:00 2001 From: Snake Date: Wed, 8 Mar 2023 23:40:55 +0800 Subject: [PATCH] Update goclt dart gen: Add scheme config and content-type header (#2987) Co-authored-by: zhoumingji --- tools/goctl/api/cmd.go | 1 + tools/goctl/api/dartgen/gen.go | 9 ++++++- tools/goctl/api/dartgen/genvars.go | 4 +-- tools/goctl/api/dartgen/vars.go | 39 +++++++++++++++++++----------- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/tools/goctl/api/cmd.go b/tools/goctl/api/cmd.go index 0545a5ce..3c550f30 100644 --- a/tools/goctl/api/cmd.go +++ b/tools/goctl/api/cmd.go @@ -106,6 +106,7 @@ func init() { dartCmd.Flags().StringVar(&dartgen.VarStringAPI, "api", "", "The api file") dartCmd.Flags().BoolVar(&dartgen.VarStringLegacy, "legacy", false, "Legacy generator for flutter v1") dartCmd.Flags().StringVar(&dartgen.VarStringHostname, "hostname", "", "hostname of the server") + dartCmd.Flags().StringVar(&dartgen.VarStringScheme, "scheme", "", "scheme of the server") docCmd.Flags().StringVar(&docgen.VarStringDir, "dir", "", "The target dir") docCmd.Flags().StringVar(&docgen.VarStringOutput, "o", "", "The output markdown directory") diff --git a/tools/goctl/api/dartgen/gen.go b/tools/goctl/api/dartgen/gen.go index b73e3901..edf90fd2 100644 --- a/tools/goctl/api/dartgen/gen.go +++ b/tools/goctl/api/dartgen/gen.go @@ -19,6 +19,8 @@ var ( VarStringLegacy bool // VarStringHostname defines the hostname. VarStringHostname string + // VarStringSchema defines the scheme. + VarStringScheme string ) // DartCommand create dart network request code @@ -27,6 +29,7 @@ func DartCommand(_ *cobra.Command, _ []string) error { dir := VarStringDir isLegacy := VarStringLegacy hostname := VarStringHostname + scheme := VarStringScheme if len(apiFile) == 0 { return errors.New("missing -api") } @@ -37,6 +40,10 @@ func DartCommand(_ *cobra.Command, _ []string) error { fmt.Println("you could use '-hostname' flag to specify your server hostname") hostname = "go-zero.dev" } + if len(scheme) == 0 { + fmt.Println("you could use '-scheme' flag to specify your server scheme") + scheme = "http" + } api, err := parser.Parse(apiFile) if err != nil { @@ -54,7 +61,7 @@ func DartCommand(_ *cobra.Command, _ []string) error { api.Info.Title = strings.Replace(apiFile, ".api", "", -1) logx.Must(genData(dir+"data/", api, isLegacy)) logx.Must(genApi(dir+"api/", api, isLegacy)) - logx.Must(genVars(dir+"vars/", isLegacy, hostname)) + logx.Must(genVars(dir+"vars/", isLegacy, scheme, hostname)) if err := formatDir(dir); err != nil { logx.Errorf("failed to format, %v", err) } diff --git a/tools/goctl/api/dartgen/genvars.go b/tools/goctl/api/dartgen/genvars.go index 72ee6bdc..ac1b0fac 100644 --- a/tools/goctl/api/dartgen/genvars.go +++ b/tools/goctl/api/dartgen/genvars.go @@ -76,14 +76,14 @@ Future getTokens() async { }` ) -func genVars(dir string, isLegacy bool, hostname string) error { +func genVars(dir string, isLegacy bool, scheme string, hostname string) error { err := os.MkdirAll(dir, 0o755) if err != nil { return err } if !fileExists(dir + "vars.dart") { - err = ioutil.WriteFile(dir+"vars.dart", []byte(fmt.Sprintf(`const serverHost='%s';`, hostname)), 0o644) + err = ioutil.WriteFile(dir+"vars.dart", []byte(fmt.Sprintf(`const serverHost='%s://%s';`, scheme, hostname)), 0o644) if err != nil { return err } diff --git a/tools/goctl/api/dartgen/vars.go b/tools/goctl/api/dartgen/vars.go index b34c3a6e..1a304f5b 100644 --- a/tools/goctl/api/dartgen/vars.go +++ b/tools/goctl/api/dartgen/vars.go @@ -56,12 +56,21 @@ Future _apiRequest(String method, String path, dynamic data, var client = HttpClient(); HttpClientRequest r; if (method == 'POST') { - r = await client.postUrl(Uri.parse('https://' + serverHost + path)); + r = await client.postUrl(Uri.parse(serverHost + path)); } else { - r = await client.getUrl(Uri.parse('https://' + serverHost + path)); + r = await client.getUrl(Uri.parse(serverHost + path)); + } + + var strData = ''; + if (data != null) { + strData = jsonEncode(data); + } + + if (method == 'POST') { + r.headers.set('Content-Type', 'application/json; charset=utf-8'); + r.headers.set('Content-Length', utf8.encode(strData).length); } - r.headers.set('Content-Type', 'application/json; charset=utf-8'); if (tokens != null) { r.headers.set('Authorization', tokens.accessToken); } @@ -70,11 +79,9 @@ Future _apiRequest(String method, String path, dynamic data, r.headers.set(k, v); }); } - var strData = ''; - if (data != null) { - strData = jsonEncode(data); - } + r.write(strData); + var rp = await r.close(); var body = await rp.transform(utf8.decoder).join(); print('${rp.statusCode} - $path'); @@ -147,12 +154,19 @@ Future _apiRequest(String method, String path, dynamic data, var client = HttpClient(); HttpClientRequest r; if (method == 'POST') { - r = await client.postUrl(Uri.parse('https://' + serverHost + path)); + r = await client.postUrl(Uri.parse(serverHost + path)); } else { - r = await client.getUrl(Uri.parse('https://' + serverHost + path)); + r = await client.getUrl(Uri.parse(serverHost + path)); } - r.headers.set('Content-Type', 'application/json; charset=utf-8'); + var strData = ''; + if (data != null) { + strData = jsonEncode(data); + } + if (method == 'POST') { + r.headers.set('Content-Type', 'application/json; charset=utf-8'); + r.headers.set('Content-Length', utf8.encode(strData).length); + } if (tokens != null) { r.headers.set('Authorization', tokens.accessToken); } @@ -161,10 +175,7 @@ Future _apiRequest(String method, String path, dynamic data, r.headers.set(k, v); }); } - var strData = ''; - if (data != null) { - strData = jsonEncode(data); - } + r.write(strData); var rp = await r.close(); var body = await rp.transform(utf8.decoder).join();