From a71f2f6cb800e9c7dc91c57e1c834e4ce58a0fac Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Mon, 19 Feb 2024 15:50:25 +0800 Subject: [PATCH 01/10] doc: fix use example --- .gitignore | 1 + core/README.md | 33 ++++++++++++++++++++-- demo/example.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ demo/response.go | 4 +++ 4 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 demo/example.go diff --git a/.gitignore b/.gitignore index c7a3858..437bebe 100644 --- a/.gitignore +++ b/.gitignore @@ -78,3 +78,4 @@ $RECYCLE.BIN/ *.code-workspace vendor/ +.idea diff --git a/core/README.md b/core/README.md index f8f786c..856f59c 100644 --- a/core/README.md +++ b/core/README.md @@ -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 ( @@ -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 }) @@ -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 表示一个网关封装,方法定义: diff --git a/demo/example.go b/demo/example.go new file mode 100644 index 0000000..b43b8ee --- /dev/null +++ b/demo/example.go @@ -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) + +} diff --git a/demo/response.go b/demo/response.go index bb4af1d..3aea5b9 100644 --- a/demo/response.go +++ b/demo/response.go @@ -23,3 +23,7 @@ type AnythingResponse struct { Origin string `json:"origin"` URL string `json:"url"` } + +type AnythingRequest struct { + Code string `json:"code"` +} 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 02/10] 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() From 932207bbc9de583b40c899dc31a9c6af76355d61 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Mon, 19 Feb 2024 16:34:56 +0800 Subject: [PATCH 03/10] doc: fix cr --- demo/example.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/demo/example.go b/demo/example.go index 84fb186..d4a1ca1 100644 --- a/demo/example.go +++ b/demo/example.go @@ -2,9 +2,9 @@ package demo import ( "fmt" + "log" "github.com/TencentBlueKing/bk-apigateway-sdks/core/bkapi" - "github.com/google/martian/log" ) func clientExample() { @@ -20,7 +20,7 @@ func clientExample() { Stage: "prod", }) if err != nil { - log.Errorf("registry default config error: %v", err) + log.Printf("registry default config error: %v", err) return } @@ -39,7 +39,7 @@ func clientExample() { }, bkapi.OptJsonResultProvider()) if err != nil { - log.Errorf("client init error: %v", err) + log.Printf("client init error: %v", err) return } // 创建结果变量 From cb99cf8116bd1d651869cc798eff7a91878167e3 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Mon, 19 Feb 2024 17:57:39 +0800 Subject: [PATCH 04/10] doc: fix cr --- core/README.md | 151 ++++++++++++++++++++++++++++++++++++++--------- demo/example.go | 124 ++++++++++++++++++++++++++------------ demo/request.go | 1 + demo/response.go | 29 --------- 4 files changed, 208 insertions(+), 97 deletions(-) create mode 100644 demo/request.go delete mode 100644 demo/response.go diff --git a/core/README.md b/core/README.md index 8c97637..582e9ff 100644 --- a/core/README.md +++ b/core/README.md @@ -24,27 +24,93 @@ go get -u github.com/TencentBlueKing/bk-apigateway-sdks/core@latest 以下基于 [demo](../demo/) 包中的部分示例(具体见: [example.go](../demo/example.go)): ```golang +package demo + import ( "fmt" + "log" + "github.com/TencentBlueKing/bk-apigateway-sdks/core/bkapi" - "github.com/TencentBlueKing/bk-apigateway-sdks/demo" + "github.com/TencentBlueKing/bk-apigateway-sdks/core/define" ) -// 创建客户端,并声明所有结果都使用 Json 格式 -client, _ := demo.New(bkapi.ClientConfig{ - Endpoint: "https://httpbin.org/", -}, bkapi.OptJsonResultProvider()) - -// 创建结果变量 -var result demo.AnythingResponse - -// 调用接口 -_, _ = client.Anything(). - SetResult(&result). - Request() - -// 结果将自动填充到 result 中 -fmt.Printf("%#v", result) +type QueryUserDemoBodyRequest struct { + Name string `json:"name"` + Age int `json:"age"` +} + +type QueryUserDemoResponse struct { + Name string `json:"name"` + Age int `json:"age"` + Address string `json:"address"` + Gender string `json:"gender"` +} + +func clientExample() { + + // 初始化client + + client,err:=bkapi.NewBkApiClient("demo", bkapi.ClientConfig{ + Endpoint: "http://special-api.example.com/prod",// 具体某个网关地址 + ClientOptions: []define.BkApiClientOption{ + // 设置一些通用的client配置,eg: + bkapi.OptJsonResultProvider(),// 声明这个网关的所有响应都是JSON + bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON + bkapi.OptSetRequestHeader("X-Api-Key", "123456"),// 设置统一的header + }, + }) + if err != nil { + log.Printf("client init error: %v", err) + return + } + + + + // 创建 api operation + apiOperation := client.NewOperation( + // 填充接口配置 + bkapi.OperationConfig{ + Name: "query_team_user_demo", + Method: "GET", + Path: "/get/{team_id}/user/", + }, + // 设置path参数 + bkapi.OptSetRequestPathParams( + map[string]string{ + "team_id": `1`, + }, + ), + // 设置query参数 + bkapi.OptSetRequestQueryParam("name", "demo"), + // 设置body参数: 自定义struct + bkapi.OptSetRequestBody(QueryUserDemoBodyRequest{Name: "demo"}), + // 设置body参数: map[string]string + bkapi.OptSetRequestBody(map[string]string{"name": "demo"}), + // 设置header参数 + bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), + ) + + // 创建结果变量 + var result QueryUserDemoResponse + + // 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) + + //// 直接通过 api operation传参 + //_,_=apiOperation.SetPathParams(map[string]string{"team_id": `1`}). + // SetBody(QueryUserDemoBodyRequest{Name: "demo"}). + // SetQueryParams(map[string]string{"name": "demo"}). + // SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). + // SetResult(&result).Request() + + //_, _ = client.StatusCode(bkapi.OptSetRequestQueryParams(map[string]string{ + // "code": `200`, + //})).SetResult(&result).Request() + + _, _ = apiOperation.SetResult(&result).Request() + // 结果将自动填充到 result 中 + fmt.Printf("%#v", result) + +} ``` ## 进阶用法 @@ -58,23 +124,50 @@ fmt.Printf("%#v", result) 可以使用 `ClientConfigRegistry` 简化每次初始化客户端时都需要传递网关地址,认证信息等重复性工作,`ClientConfigRegistry` 本身已实现成 `ClientConfigProvider`,可直接替代 `ClientConfig` 来使用: ```golang -// 获取默认的配置中心 -registry := bkapi.GetGlobalClientConfigRegistry() +// 通过ClientConfigRegistry初始化client -// 注册默认的配置(不区分网关) -registry.RegisterDefaultConfig(bkapi.ClientConfig{ - BkApiUrlTmpl: "http://{api_name}.example.com/", - Stage: "prod", -}) +// 使用默认的全局的注册配置来初始化 +registry: = bkapi.GetGlobalClientConfigRegistry() -// 注册指定网关配置 -registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig{ - Endpoint: "http://special-api.example.com/", - ClientOptions: []define.BkApiClientOption{bkapi.OptJsonResultProvider()}, // 声明这个网关的所有响应都是 JSON +// 方式一:注册默认的配置(不区分网关) +err: = registry.RegisterDefaultConfig(bkapi.ClientConfig { + BkApiUrlTmpl: "http://{api_name}.example.com/", //网关通用地址 + Stage: "prod", }) +if err != nil { + log.Printf("registry default config error: %v", err) + return +} +// 使用默认的配置,在创建client的时候再指定网关 +client, err: = bkapi.NewBkApiClient("my-gateway", registry) +if err != nil { + log.Fatalf("create bkapi client error: %v", err) + return +} + + + +// 方式二:注册指定的网关配置 +err = registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig { + Endpoint: "http://special-api.example.com/prod", // 具体某个网关地址 + ClientOptions: [] define.BkApiClientOption { + // 设置一些通用的client配置,eg: + bkapi.OptJsonResultProvider(), // 声明这个网关的所有响应都是JSON + bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON + bkapi.OptSetRequestHeader("X-Api-Key", "123456"), // 设置统一的header + }, +}) +if err != nil { + log.Fatalf("set bkapi client config error: %v", err) + return +} + -// 可直接使用配置中心来初始化客户端 -client, _ := New(registry) +client, err = bkapi.NewBkApiClient("my-gateway", registry) +if err != nil { + log.Fatalf("create bkapi client error: %v", err) + return +} ``` ### Prometheus 指标 diff --git a/demo/example.go b/demo/example.go index d4a1ca1..a256d9a 100644 --- a/demo/example.go +++ b/demo/example.go @@ -5,75 +5,121 @@ import ( "log" "github.com/TencentBlueKing/bk-apigateway-sdks/core/bkapi" + "github.com/TencentBlueKing/bk-apigateway-sdks/core/define" ) +type QueryUserDemoBodyRequest struct { + Name string `json:"name"` + Age int `json:"age"` +} + +type QueryUserDemoResponse struct { + Name string `json:"name"` + Age int `json:"age"` + Address string `json:"address"` + Gender string `json:"gender"` +} + func clientExample() { - // 初始化 + // 初始化client - // 获取默认的配置中心 + // 使用默认的全局的注册配置来初始化 registry := bkapi.GetGlobalClientConfigRegistry() - // 注册默认的配置(不区分网关) + // 方式一:注册默认的配置(不区分网关) err := registry.RegisterDefaultConfig(bkapi.ClientConfig{ - BkApiUrlTmpl: "http://{api_name}.example.com/", + BkApiUrlTmpl: "http://{api_name}.example.com/", //网关通用地址 Stage: "prod", }) if err != nil { log.Printf("registry default config error: %v", err) return } + // 使用默认的配置,在创建client的时候再指定网关 + client, err := bkapi.NewBkApiClient("my-gateway", registry) + if err != nil { + log.Fatalf("create bkapi client error: %v", err) + return + } - //// 注册指定网关配置 - //registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig{ - // Endpoint: "http://special-api.example.com/", - // ClientOptions: []define.BkApiClientOption{bkapi.OptJsonResultProvider()}, // 声明这个网关的所有响应都是 JSON - //}) - - // 可直接使用配置中心来初始化客户端 - // client, _ := New(registry) + // 方式二:注册指定的网关配置 + err = registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig{ + Endpoint: "http://special-api.example.com/prod", // 具体某个网关地址 + ClientOptions: []define.BkApiClientOption{ + // 设置一些通用的client配置,eg: + bkapi.OptJsonResultProvider(), // 声明这个网关的所有响应都是JSON + bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON + bkapi.OptSetRequestHeader("X-Api-Key", "123456"), // 设置统一的header + }, + }) + if err != nil { + log.Fatalf("set bkapi client config error: %v", err) + return + } - // 创建客户端,并声明所有结果都使用 Json 格式 - client, err := New(bkapi.ClientConfig{ - Endpoint: "https://httpbin.org/", - }, bkapi.OptJsonResultProvider()) + client, err = bkapi.NewBkApiClient("my-gateway", registry) + if err != nil { + log.Fatalf("create bkapi client error: %v", err) + return + } + // 直接使用自定义的配置来创建 + client, err = bkapi.NewBkApiClient("demo", bkapi.ClientConfig{ + Endpoint: "http://special-api.example.com/prod", // 具体某个网关地址 + ClientOptions: []define.BkApiClientOption{ + // 设置一些通用的client配置,eg: + bkapi.OptJsonResultProvider(), // 声明这个网关的所有响应都是JSON + bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON + bkapi.OptSetRequestHeader("X-Api-Key", "123456"), // 设置统一的header + }, + }) if err != nil { log.Printf("client init error: %v", err) return } + + // 创建 api operation + apiOperation := client.NewOperation( + // 填充接口配置 + bkapi.OperationConfig{ + Name: "query_team_user_demo", + Method: "GET", + Path: "/get/{team_id}/user/", + }, + // 设置path参数 + bkapi.OptSetRequestPathParams( + map[string]string{ + "team_id": `1`, + }, + ), + // 设置query参数 + bkapi.OptSetRequestQueryParam("name", "demo"), + // 设置body参数: 自定义struct + bkapi.OptSetRequestBody(QueryUserDemoBodyRequest{Name: "demo"}), + // 设置body参数: map[string]string + bkapi.OptSetRequestBody(map[string]string{"name": "demo"}), + // 设置header参数 + bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), + ) + // 创建结果变量 - var result AnythingResponse + var result QueryUserDemoResponse // 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) - // 传递路径参数 - _, _ = client.StatusCode(bkapi.OptSetRequestPathParams(map[string]string{ - "code": `200`, - })).SetResult(&result).Request() + //// 直接通过 api operation传参 + //_,_=apiOperation.SetPathParams(map[string]string{"team_id": `1`}). + // SetBody(QueryUserDemoBodyRequest{Name: "demo"}). + // SetQueryParams(map[string]string{"name": "demo"}). + // SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). + // 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.Anything(bkapi.OptSetRequestBody(map[string]string{ - "code": `200`, - })).SetResult(&result).Request() - - _, _ = client.Anything(bkapi.OptSetRequestBody( - AnythingRequest{Code: "200"})).SetResult(&result).Request() - - // 传递header参数 - _, _ = client.Anything( - bkapi.OptSetRequestHeader( - "X-BKAPI-VERSION", "v3", - )).SetResult(&result).Request() - + _, _ = apiOperation.SetResult(&result).Request() // 结果将自动填充到 result 中 fmt.Printf("%#v", result) diff --git a/demo/request.go b/demo/request.go new file mode 100644 index 0000000..bed5a16 --- /dev/null +++ b/demo/request.go @@ -0,0 +1 @@ +package demo diff --git a/demo/response.go b/demo/response.go deleted file mode 100644 index 3aea5b9..0000000 --- a/demo/response.go +++ /dev/null @@ -1,29 +0,0 @@ -/** - * TencentBlueKing is pleased to support the open source community by - * making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available. - * Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. - * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. - * You may obtain a copy of the License at http://opensource.org/licenses/MIT - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on - * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -package demo - -// AnythingResponse is the response type for Anything. -type AnythingResponse struct { - Args map[string]string `json:"args"` - Data string `json:"data"` - Files map[string]interface{} `json:"files"` - Form map[string]interface{} `json:"form"` - Headers map[string]string `json:"headers"` - JSON map[string]interface{} `json:"json"` - Method string `json:"method"` - Origin string `json:"origin"` - URL string `json:"url"` -} - -type AnythingRequest struct { - Code string `json:"code"` -} From 1383ad39c5142424ebe49de9996e18edc095bb37 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Mon, 19 Feb 2024 17:59:06 +0800 Subject: [PATCH 05/10] doc: fix cr --- demo/request.go | 1 - 1 file changed, 1 deletion(-) delete mode 100644 demo/request.go diff --git a/demo/request.go b/demo/request.go deleted file mode 100644 index bed5a16..0000000 --- a/demo/request.go +++ /dev/null @@ -1 +0,0 @@ -package demo From 8fe7a3b0b72810afb1ef6ea4a1eb57b0ed2b28c6 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Mon, 19 Feb 2024 18:03:19 +0800 Subject: [PATCH 06/10] doc: fix cr --- demo/response.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 demo/response.go diff --git a/demo/response.go b/demo/response.go new file mode 100644 index 0000000..bb4af1d --- /dev/null +++ b/demo/response.go @@ -0,0 +1,25 @@ +/** + * TencentBlueKing is pleased to support the open source community by + * making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available. + * Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. + * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at http://opensource.org/licenses/MIT + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package demo + +// AnythingResponse is the response type for Anything. +type AnythingResponse struct { + Args map[string]string `json:"args"` + Data string `json:"data"` + Files map[string]interface{} `json:"files"` + Form map[string]interface{} `json:"form"` + Headers map[string]string `json:"headers"` + JSON map[string]interface{} `json:"json"` + Method string `json:"method"` + Origin string `json:"origin"` + URL string `json:"url"` +} From a9ef2296bffd75e6e19e238523e0156d1914be17 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Mon, 19 Feb 2024 18:11:30 +0800 Subject: [PATCH 07/10] doc: fix readme --- core/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/README.md b/core/README.md index 582e9ff..faa6bae 100644 --- a/core/README.md +++ b/core/README.md @@ -49,8 +49,8 @@ type QueryUserDemoResponse struct { func clientExample() { // 初始化client - - client,err:=bkapi.NewBkApiClient("demo", bkapi.ClientConfig{ + + client, err :=bkapi.NewBkApiClient("demo", bkapi.ClientConfig{ Endpoint: "http://special-api.example.com/prod",// 具体某个网关地址 ClientOptions: []define.BkApiClientOption{ // 设置一些通用的client配置,eg: @@ -126,7 +126,7 @@ func clientExample() { ```golang // 通过ClientConfigRegistry初始化client -// 使用默认的全局的注册配置来初始化 +// 使用默认的全局的配置中心来初始化 registry: = bkapi.GetGlobalClientConfigRegistry() // 方式一:注册默认的配置(不区分网关) From 250f0778c6bc30f02ac3860e1b846de96136fdc9 Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Tue, 20 Feb 2024 09:50:21 +0800 Subject: [PATCH 08/10] doc: remove bad example --- demo/example.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/demo/example.go b/demo/example.go index a256d9a..661a5b9 100644 --- a/demo/example.go +++ b/demo/example.go @@ -115,10 +115,6 @@ func clientExample() { // SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). // SetResult(&result).Request() - //_, _ = client.StatusCode(bkapi.OptSetRequestQueryParams(map[string]string{ - // "code": `200`, - //})).SetResult(&result).Request() - _, _ = apiOperation.SetResult(&result).Request() // 结果将自动填充到 result 中 fmt.Printf("%#v", result) From 8430b5818ff40b387d34218d78945eac1e8d8eec Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Tue, 20 Feb 2024 09:56:41 +0800 Subject: [PATCH 09/10] doc: fix readme --- core/README.md | 63 +++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/core/README.md b/core/README.md index faa6bae..7aba8bd 100644 --- a/core/README.md +++ b/core/README.md @@ -50,7 +50,7 @@ func clientExample() { // 初始化client - client, err :=bkapi.NewBkApiClient("demo", bkapi.ClientConfig{ + client, err := bkapi.NewBkApiClient("demo", bkapi.ClientConfig{ Endpoint: "http://special-api.example.com/prod",// 具体某个网关地址 ClientOptions: []define.BkApiClientOption{ // 设置一些通用的client配置,eg: @@ -213,38 +213,43 @@ Operation 表示一个网关资源封装,方法定义: ```go -/// 创建结果变量 -var result AnythingResponse - -// 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) - -// 传递路径参数 -_, _ = 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() +// 创建 api operation +apiOperation := client.NewOperation( + // 填充接口配置 + bkapi.OperationConfig{ + Name: "query_team_user_demo", + Method: "GET", + Path: "/get/{team_id}/user/", + }, + // 设置path参数 + bkapi.OptSetRequestPathParams( + map[string]string{ + "team_id": `1`, + }, + ), + // 设置query参数 + bkapi.OptSetRequestQueryParam("name", "demo"), + // 设置body参数: 自定义struct + bkapi.OptSetRequestBody(QueryUserDemoBodyRequest{Name: "demo"}), + // 设置body参数: map[string]string + bkapi.OptSetRequestBody(map[string]string{"name": "demo"}), + // 设置header参数 + bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), +) -// 传递body参数 -_, _ = client.StatusCode(bkapi.OptSetRequestBody(map[string]string{ -"code": `200`, -})).SetResult(&result).Request() +// 创建结果变量 +var result QueryUserDemoResponse -_, _ = client.StatusCode(bkapi.OptSetRequestBody( -AnythingRequest{Code: "200"})).SetResult(&result).Request() +// 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) -// 传递header参数 -_, _ = client.StatusCode( -bkapi.OptSetRequestHeader( -"X-BKAPI-VERSION", "v3", -)).SetResult(&result).Request() +//// 直接通过 api operation传参 +//_,_=apiOperation.SetPathParams(map[string]string{"team_id": `1`}). +// SetBody(QueryUserDemoBodyRequest{Name: "demo"}). +// SetQueryParams(map[string]string{"name": "demo"}). +// SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). +// SetResult(&result).Request() +_, _ = apiOperation.SetResult(&result).Request() // 结果将自动填充到 result 中 fmt.Printf("%#v", result) ``` From cf5a093951a4b0e093ccdcb7f801cb4f6ba3047e Mon Sep 17 00:00:00 2001 From: hanyajun <1581532052@qq.com> Date: Tue, 20 Feb 2024 10:27:26 +0800 Subject: [PATCH 10/10] doc: fix cr --- core/README.md | 35 ++++++++++++++++++----------------- demo/example.go | 8 ++++---- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/core/README.md b/core/README.md index 7aba8bd..c0ce02f 100644 --- a/core/README.md +++ b/core/README.md @@ -53,10 +53,10 @@ func clientExample() { client, err := bkapi.NewBkApiClient("demo", bkapi.ClientConfig{ Endpoint: "http://special-api.example.com/prod",// 具体某个网关地址 ClientOptions: []define.BkApiClientOption{ - // 设置一些通用的client配置,eg: - bkapi.OptJsonResultProvider(),// 声明这个网关的所有响应都是JSON - bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON + // 设置一些通用的client配置,eg: bkapi.OptSetRequestHeader("X-Api-Key", "123456"),// 设置统一的header + bkapi.OptJsonResultProvider(),// 声明这个网关的所有响应都是JSON + bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON }, }) if err != nil { @@ -73,7 +73,9 @@ func clientExample() { Name: "query_team_user_demo", Method: "GET", Path: "/get/{team_id}/user/", - }, + }, + // 设置header参数 + bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), // 设置path参数 bkapi.OptSetRequestPathParams( map[string]string{ @@ -86,8 +88,6 @@ func clientExample() { bkapi.OptSetRequestBody(QueryUserDemoBodyRequest{Name: "demo"}), // 设置body参数: map[string]string bkapi.OptSetRequestBody(map[string]string{"name": "demo"}), - // 设置header参数 - bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), ) // 创建结果变量 @@ -96,10 +96,10 @@ func clientExample() { // 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) //// 直接通过 api operation传参 - //_,_=apiOperation.SetPathParams(map[string]string{"team_id": `1`}). + //_,_=apiOperation.SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). + // SetPathParams(map[string]string{"team_id": `1`}). // SetBody(QueryUserDemoBodyRequest{Name: "demo"}). // SetQueryParams(map[string]string{"name": "demo"}). - // SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). // SetResult(&result).Request() //_, _ = client.StatusCode(bkapi.OptSetRequestQueryParams(map[string]string{ @@ -151,12 +151,13 @@ if err != nil { err = registry.RegisterClientConfig("my-gateway", bkapi.ClientConfig { Endpoint: "http://special-api.example.com/prod", // 具体某个网关地址 ClientOptions: [] define.BkApiClientOption { - // 设置一些通用的client配置,eg: - bkapi.OptJsonResultProvider(), // 声明这个网关的所有响应都是JSON - bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON + // 设置一些通用的client配置,eg: bkapi.OptSetRequestHeader("X-Api-Key", "123456"), // 设置统一的header - }, -}) + bkapi.OptJsonResultProvider(), // 声明这个网关的所有响应都是JSON + bkapi.OptJsonBodyProvider(), // 声明这个网关的body请求都是JSON + }, + }) + if err != nil { log.Fatalf("set bkapi client config error: %v", err) return @@ -221,6 +222,8 @@ apiOperation := client.NewOperation( Method: "GET", Path: "/get/{team_id}/user/", }, + // 设置header参数 + bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), // 设置path参数 bkapi.OptSetRequestPathParams( map[string]string{ @@ -233,8 +236,6 @@ apiOperation := client.NewOperation( bkapi.OptSetRequestBody(QueryUserDemoBodyRequest{Name: "demo"}), // 设置body参数: map[string]string bkapi.OptSetRequestBody(map[string]string{"name": "demo"}), - // 设置header参数 - bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), ) // 创建结果变量 @@ -243,10 +244,10 @@ var result QueryUserDemoResponse // 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) //// 直接通过 api operation传参 -//_,_=apiOperation.SetPathParams(map[string]string{"team_id": `1`}). +//_,_=apiOperation.SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). +// SetPathParams(map[string]string{"team_id": `1`}). // SetBody(QueryUserDemoBodyRequest{Name: "demo"}). // SetQueryParams(map[string]string{"name": "demo"}). -// SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). // SetResult(&result).Request() _, _ = apiOperation.SetResult(&result).Request() diff --git a/demo/example.go b/demo/example.go index 661a5b9..f413ca1 100644 --- a/demo/example.go +++ b/demo/example.go @@ -87,6 +87,8 @@ func clientExample() { Method: "GET", Path: "/get/{team_id}/user/", }, + // 设置header参数 + bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), // 设置path参数 bkapi.OptSetRequestPathParams( map[string]string{ @@ -99,8 +101,6 @@ func clientExample() { bkapi.OptSetRequestBody(QueryUserDemoBodyRequest{Name: "demo"}), // 设置body参数: map[string]string bkapi.OptSetRequestBody(map[string]string{"name": "demo"}), - // 设置header参数 - bkapi.OptSetRequestHeader("X-Bkapi-Header", "demo"), ) // 创建结果变量 @@ -109,10 +109,10 @@ func clientExample() { // 调用接口(Request()的返回值是:*http.Response,err,看具体情况是否需要处理) //// 直接通过 api operation传参 - //_,_=apiOperation.SetPathParams(map[string]string{"team_id": `1`}). + //_,_=apiOperation.SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). + // SetPathParams(map[string]string{"team_id": `1`}). // SetBody(QueryUserDemoBodyRequest{Name: "demo"}). // SetQueryParams(map[string]string{"name": "demo"}). - // SetHeaders(map[string]string{"X-Bkapi-Header": "demo"}). // SetResult(&result).Request() _, _ = apiOperation.SetResult(&result).Request()