-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f56ed1
commit a71f2f6
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,3 +78,4 @@ $RECYCLE.BIN/ | |
|
||
*.code-workspace | ||
vendor/ | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters