Skip to content

Commit

Permalink
doc: fix use example
Browse files Browse the repository at this point in the history
  • Loading branch information
Han-Ya-Jun committed Feb 19, 2024
1 parent 4f56ed1 commit a71f2f6
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ $RECYCLE.BIN/

*.code-workspace
vendor/
.idea
33 changes: 31 additions & 2 deletions core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ go get -u github.com/TencentBlueKing/bk-apigateway-sdks/core@latest
| bkapi.*ResultProvider | 结构 | 提供响应体反序列化封装的接口 | json.go |

### 快速使用
以下基于 [demo](./demo/) 包中的示例
以下基于 [demo](../demo/) 包中的部分示例(具体见: [example.go](../demo/example.go))

```golang
import (
Expand Down Expand Up @@ -68,7 +68,7 @@ registry.RegisterDefaultConfig(bkapi.ClientConfig{
})

// 注册指定网关配置
registry.RegisterDefaultConfig("my-gateway", bkapi.ClientConfig{
registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig{
Endpoint: "http://special-api.example.com/",
ClientOptions: []define.BkApiClientOption{bkapi.OptJsonResultProvider()}, // 声明这个网关的所有响应都是 JSON
})
Expand Down Expand Up @@ -118,6 +118,35 @@ Operation 表示一个网关资源封装,方法定义:
| Apply | 增加额外选项 |
| Request | 发送请求 |


```go
// 传递路径参数
_, _ = client.Anything(bkapi.OptSetRequestPathParams(map[string]string{
"code": `200`,
})).SetResult(&result).Request()

// 传递query参数
//_, _ = client.StatusCode(bkapi.OptSetRequestQueryParams(map[string]string{
// "code": `200`,
//})).SetResult(&result).Request()

// 传递单个query参数
//_, _ = client.StatusCode(bkapi.OptSetRequestQueryParam("code", `200`)).SetResult(&result).Request()

// 传递body参数
_, _ = client.StatusCode(bkapi.OptSetRequestBody(map[string]string{
"code": `200`,
})).SetResult(&result).Request()

_, _ = client.StatusCode(bkapi.OptSetRequestBody(
AnythingRequest{Code: "200"})).SetResult(&result).Request()

// 传递header参数
_, _ = client.StatusCode(
bkapi.OptSetRequestHeader(
"X-BKAPI-VERSION", "v3",
)).SetResult(&result).Request()
```
### 客户端封装

BkApiClient 表示一个网关封装,方法定义:
Expand Down
72 changes: 72 additions & 0 deletions demo/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package demo

import (
"fmt"

"github.com/TencentBlueKing/bk-apigateway-sdks/core/bkapi"
"github.com/TencentBlueKing/bk-apigateway-sdks/core/define"
)

func clientExample() {

// 初始化

// 获取默认的配置中心
registry := bkapi.GetGlobalClientConfigRegistry()

// 注册默认的配置(不区分网关)
registry.RegisterDefaultConfig(bkapi.ClientConfig{
BkApiUrlTmpl: "http://{api_name}.example.com/",
Stage: "prod",
})

// 注册指定网关配置
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{
Endpoint: "https://httpbin.org/",
}, bkapi.OptJsonResultProvider())

// 创建结果变量
var result AnythingResponse

// 调用接口

// 传递路径参数
_, _ = client.Anything(bkapi.OptSetRequestPathParams(map[string]string{
"code": `200`,
})).SetResult(&result).Request()

// 传递query参数
//_, _ = client.StatusCode(bkapi.OptSetRequestQueryParams(map[string]string{
// "code": `200`,
//})).SetResult(&result).Request()

// 传递单个query参数
//_, _ = client.StatusCode(bkapi.OptSetRequestQueryParam("code", `200`)).SetResult(&result).Request()

// 传递body参数
_, _ = client.StatusCode(bkapi.OptSetRequestBody(map[string]string{
"code": `200`,
})).SetResult(&result).Request()

_, _ = client.StatusCode(bkapi.OptSetRequestBody(
AnythingRequest{Code: "200"})).SetResult(&result).Request()

// 传递header参数
_, _ = client.StatusCode(
bkapi.OptSetRequestHeader(
"X-BKAPI-VERSION", "v3",
)).SetResult(&result).Request()

// 结果将自动填充到 result 中
fmt.Printf("%#v", result)

}
4 changes: 4 additions & 0 deletions demo/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ type AnythingResponse struct {
Origin string `json:"origin"`
URL string `json:"url"`
}

type AnythingRequest struct {
Code string `json:"code"`
}

0 comments on commit a71f2f6

Please sign in to comment.