From c84d550def06fabddcab4a7827b9eaaa61024d68 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Mon, 19 Feb 2024 16:22:10 +0800 Subject: [PATCH] doc: fix cr --- core/README.md | 8 ++++++++ demo/example.go | 34 +++++++++++++++++++++------------- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/core/README.md b/core/README.md index 856f59c..8c97637 100644 --- a/core/README.md +++ b/core/README.md @@ -120,6 +120,11 @@ Operation 表示一个网关资源封装,方法定义: ```go +/// 创建结果变量 +var result AnythingResponse + +// 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) + // 传递路径参数 _, _ = client.Anything(bkapi.OptSetRequestPathParams(map[string]string{ "code": `200`, @@ -146,6 +151,9 @@ _, _ = client.StatusCode( bkapi.OptSetRequestHeader( "X-BKAPI-VERSION", "v3", )).SetResult(&result).Request() + +// 结果将自动填充到 result 中 +fmt.Printf("%#v", result) ``` ### 客户端封装 diff --git a/demo/example.go b/demo/example.go index b43b8ee..84fb186 100644 --- a/demo/example.go +++ b/demo/example.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/TencentBlueKing/bk-apigateway-sdks/core/bkapi" - "github.com/TencentBlueKing/bk-apigateway-sdks/core/define" + "github.com/google/martian/log" ) func clientExample() { @@ -15,32 +15,40 @@ func clientExample() { registry := bkapi.GetGlobalClientConfigRegistry() // 注册默认的配置(不区分网关) - registry.RegisterDefaultConfig(bkapi.ClientConfig{ + err := registry.RegisterDefaultConfig(bkapi.ClientConfig{ BkApiUrlTmpl: "http://{api_name}.example.com/", Stage: "prod", }) + if err != nil { + log.Errorf("registry default config error: %v", err) + return + } - // 注册指定网关配置 - registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig{ - Endpoint: "http://special-api.example.com/", - ClientOptions: []define.BkApiClientOption{bkapi.OptJsonResultProvider()}, // 声明这个网关的所有响应都是 JSON - }) + //// 注册指定网关配置 + //registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig{ + // Endpoint: "http://special-api.example.com/", + // ClientOptions: []define.BkApiClientOption{bkapi.OptJsonResultProvider()}, // 声明这个网关的所有响应都是 JSON + //}) // 可直接使用配置中心来初始化客户端 // client, _ := New(registry) // 创建客户端,并声明所有结果都使用 Json 格式 - client, _ := New(bkapi.ClientConfig{ + client, err := New(bkapi.ClientConfig{ Endpoint: "https://httpbin.org/", }, bkapi.OptJsonResultProvider()) + if err != nil { + log.Errorf("client init error: %v", err) + return + } // 创建结果变量 var result AnythingResponse - // 调用接口 + // 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) // 传递路径参数 - _, _ = client.Anything(bkapi.OptSetRequestPathParams(map[string]string{ + _, _ = client.StatusCode(bkapi.OptSetRequestPathParams(map[string]string{ "code": `200`, })).SetResult(&result).Request() @@ -53,15 +61,15 @@ func clientExample() { //_, _ = client.StatusCode(bkapi.OptSetRequestQueryParam("code", `200`)).SetResult(&result).Request() // 传递body参数 - _, _ = client.StatusCode(bkapi.OptSetRequestBody(map[string]string{ + _, _ = client.Anything(bkapi.OptSetRequestBody(map[string]string{ "code": `200`, })).SetResult(&result).Request() - _, _ = client.StatusCode(bkapi.OptSetRequestBody( + _, _ = client.Anything(bkapi.OptSetRequestBody( AnythingRequest{Code: "200"})).SetResult(&result).Request() // 传递header参数 - _, _ = client.StatusCode( + _, _ = client.Anything( bkapi.OptSetRequestHeader( "X-BKAPI-VERSION", "v3", )).SetResult(&result).Request()