Skip to content

Commit

Permalink
doc: fix cr
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun committed Feb 19, 2024
1 parent a71f2f6 commit c84d550
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
8 changes: 8 additions & 0 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ Operation 表示一个网关资源封装,方法定义:


```go
/// 创建结果变量
var result AnythingResponse

// 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理)

// 传递路径参数
_, _ = client.Anything(bkapi.OptSetRequestPathParams(map[string]string{
"code": `200`,
Expand All @@ -146,6 +151,9 @@ _, _ = client.StatusCode(
bkapi.OptSetRequestHeader(
"X-BKAPI-VERSION", "v3",
)).SetResult(&result).Request()

// 结果将自动填充到 result 中
fmt.Printf("%#v", result)
```
### 客户端封装

Expand Down
34 changes: 21 additions & 13 deletions demo/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Check failure on line 7 in demo/example.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

missing go.sum entry for module providing package github.com/google/martian/log (imported by github.com/TencentBlueKing/bk-apigateway-sdks/demo); to add:

Check failure on line 7 in demo/example.go

View workflow job for this annotation

GitHub Actions / test (1.17.x, ubuntu-latest)

missing go.sum entry for module providing package github.com/google/martian/log (imported by github.com/TencentBlueKing/bk-apigateway-sdks/demo); to add:

Check failure on line 7 in demo/example.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest)

missing go.sum entry for module providing package github.com/google/martian/log (imported by github.com/TencentBlueKing/bk-apigateway-sdks/demo); to add:

Check failure on line 7 in demo/example.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

missing go.sum entry for module providing package github.com/google/martian/log (imported by github.com/TencentBlueKing/bk-apigateway-sdks/demo); to add:

Check failure on line 7 in demo/example.go

View workflow job for this annotation

GitHub Actions / test (1.16.x, ubuntu-latest)

missing go.sum entry for module providing package github.com/google/martian/log (imported by github.com/TencentBlueKing/bk-apigateway-sdks/demo); to add:

Check failure on line 7 in demo/example.go

View workflow job for this annotation

GitHub Actions / test (1.18.x, ubuntu-latest)

missing go.sum entry for module providing package github.com/google/martian/log (imported by github.com/TencentBlueKing/bk-apigateway-sdks/demo); to add:
)

func clientExample() {
Expand All @@ -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()

Expand All @@ -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()
Expand Down

0 comments on commit c84d550

Please sign in to comment.