Update goclt dart gen: Add scheme config and content-type header (#2987)

Co-authored-by: zhoumingji <zhoumingji@cmsr.chinamobile.com>
master
Snake 2 years ago committed by GitHub
parent a2592a17e9
commit e136deb3a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -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")

@ -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)
}

@ -76,14 +76,14 @@ Future<Tokens?> 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
}

@ -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);
}
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));
}
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();

Loading…
Cancel
Save