Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: fix use example #17

Merged
merged 10 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
"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()
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved

// 传递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{
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
Endpoint: "http://special-api.example.com/",
ClientOptions: []define.BkApiClientOption{bkapi.OptJsonResultProvider()}, // 声明这个网关的所有响应都是 JSON
})

// 可直接使用配置中心来初始化客户端
// client, _ := New(registry)

// 创建客户端,并声明所有结果都使用 Json 格式
client, _ := New(bkapi.ClientConfig{
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
Endpoint: "https://httpbin.org/",
}, bkapi.OptJsonResultProvider())
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved

// 创建结果变量
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()
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved

Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
// 传递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"`
}
Han-Ya-Jun marked this conversation as resolved.
Show resolved Hide resolved
Loading