Skip to content

Commit

Permalink
fix: cs gen client.
Browse files Browse the repository at this point in the history
  • Loading branch information
chaosannals committed Dec 30, 2024
1 parent d864b3e commit 7d8aa43
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
18 changes: 10 additions & 8 deletions tools/goctl/api/csgen/ApiBaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,31 @@ public abstract class ApiBaseClient
protected readonly HttpClient httpClient = new HttpClient();


public ApiBaseClient(string host = "localhost", short port = 8080, string scheme = "http")
public ApiBaseClient(string host, short port, string scheme)
{
httpClient.BaseAddress = new Uri($"{scheme}://{host}:{port}");
}

public async Task<R> CallResultAsync<R>(HttpMethod method, string path, CancellationToken cancellationToken) where R : new()
protected async Task<R> CallResultAsync<R>(HttpMethod method, string path, CancellationToken cancellationToken) where R : new()
{
var response = await CallAsync(HttpMethod.Post, path, cancellationToken);
return await ParseResponseAsync<R>(response);
}

public async Task<HttpResponseMessage> CallAsync(HttpMethod method, string path, CancellationToken cancellationToken)
protected async Task<HttpResponseMessage> CallAsync(HttpMethod method, string path, CancellationToken cancellationToken)
{
using var request = new HttpRequestMessage(method, path);
return await httpClient.SendAsync(request, cancellationToken);
}


public async Task<R> RequestResultAsync<T, R>(HttpMethod method, string path, T? param, CancellationToken cancellationToken) where R : new()
protected async Task<R> RequestResultAsync<T, R>(HttpMethod method, string path, T? param, CancellationToken cancellationToken) where R : new()
{
var response = await RequestAsync(HttpMethod.Post, path, param, cancellationToken);
return await ParseResponseAsync<R>(response);
}

public async Task<HttpResponseMessage> RequestAsync<T>(HttpMethod method, string path, T? param, CancellationToken cancellationToken)
protected async Task<HttpResponseMessage> RequestAsync<T>(HttpMethod method, string path, T? param, CancellationToken cancellationToken)
{
using var request = new HttpRequestMessage();
request.Method = method;
Expand Down Expand Up @@ -81,7 +81,7 @@ public async Task<HttpResponseMessage> RequestAsync<T>(HttpMethod method, string
return await httpClient.SendAsync(request, cancellationToken);
}

public static async Task<R> ParseResponseAsync<R>(HttpResponseMessage response) where R : new()
protected static async Task<R> ParseResponseAsync<R>(HttpResponseMessage response) where R : new()
{
R result = await response.Content.ReadFromJsonAsync<R>() ?? new R();
foreach (var p in typeof(R).GetProperties())
Expand All @@ -90,9 +90,11 @@ public async Task<HttpResponseMessage> RequestAsync<T>(HttpMethod method, string
var hpn = p.GetCustomAttribute<HeaderPropertyName>();
if (hpn != null && response.Headers.Contains(hpn.Name))
{
// TODO 这里应该有类型问题
var v = response.Headers.GetValues(hpn.Name);
p.SetValue(result, v);
if (v != null && v.Count() > 0)
{
p.SetValue(result, v.First());
}
continue;
}
}
Expand Down
3 changes: 3 additions & 0 deletions tools/goctl/api/csgen/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func writeClient(dir string, ns string, api *spec.ApiSpec) error {
// 类
fmt.Fprintf(f, "public sealed class %sClient : ApiBaseClient\r\n{\r\n", name)

// 构造函数
fmt.Fprintf(f, " public %sClient(string host, short port, string scheme = \"http\") : base(host, port, scheme){}\r\n", name)

// 组
for _, g := range api.Service.Groups {
prefix := g.GetAnnotation("prefix")
Expand Down
30 changes: 17 additions & 13 deletions tools/goctl/api/csgen/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ func genMessages(dir string, ns string, api *spec.ApiSpec) error {
defer f.Close()

// 引入依赖
fmt.Fprintln(f, "using System.Text.Json.Serialization;")
fmt.Fprintln(f)
fmt.Fprint(f, "using System.Text.Json.Serialization;\r\n\r\n")

// 名字空间
fmt.Fprintf(f, "namespace %s;\n\n", ns)
fmt.Fprintf(f, "namespace %s;\r\n\r\n", ns)

// 类
fmt.Fprintf(f, "public class %s\n{\n", cn)
fmt.Fprintf(f, "public class %s\r\n{\r\n", cn)

for _, tagKey := range tagKeys {
// 获取字段
Expand All @@ -66,31 +65,36 @@ func genMessages(dir string, ns string, api *spec.ApiSpec) error {
writeIndent(f, 4)
switch tagKey {
case bodyTagKey:
fmt.Fprintf(f, "[JsonPropertyName(\"%s\")]\n", k)
fmt.Fprintf(f, "[JsonPropertyName(\"%s\")]\r\n", k)
case headerTagKey:
fmt.Fprintln(f, "[JsonIgnore]")
fmt.Fprint(f, "[JsonIgnore]\r\n")
writeIndent(f, 4)
fmt.Fprintf(f, "[HeaderPropertyName(\"%s\")]\n", k)
fmt.Fprintf(f, "[HeaderPropertyName(\"%s\")]\r\n", k)
case formTagKey:
fmt.Fprintln(f, "[JsonIgnore]")
fmt.Fprint(f, "[JsonIgnore]\r\n")
writeIndent(f, 4)
fmt.Fprintf(f, "[FormPropertyName(\"%s\")]\n", k)
fmt.Fprintf(f, "[FormPropertyName(\"%s\")]\r\n", k)
case pathTagKey:
fmt.Fprintln(f, "[JsonIgnore]")
fmt.Fprint(f, "[JsonIgnore]\r\n")
writeIndent(f, 4)
fmt.Fprintf(f, "[PathPropertyName(\"%s\")]\n", k)
fmt.Fprintf(f, "[PathPropertyName(\"%s\")]\r\n", k)
}

writeIndent(f, 4)
tn, err := apiTypeToCsTypeName(m.Type)
if err != nil {
return err
}
fmt.Fprintf(f, "public %s %s { get; set; }\n\n", tn, camelCase(m.Name, true))

optionalTag := ""
if m.IsOptionalOrOmitEmpty() {
optionalTag = "?"
}
fmt.Fprintf(f, "public %s%s %s { get; set; }\r\n\r\n", tn, optionalTag, camelCase(m.Name, true))
}
}

fmt.Fprintln(f, "}")
fmt.Fprint(f, "}\r\n")
}
return nil
}
Expand Down

0 comments on commit 7d8aa43

Please sign in to comment.