7
0
gaore-common-sdk-go/README.md

179 lines
5.1 KiB
Markdown
Raw Permalink Normal View History

2020-08-03 14:38:20 +08:00
# gaore-common-sdk-go
2020-08-06 18:20:43 +08:00
### 1.目录架构
2020-08-06 17:29:29 +08:00
```
├── README.md
├── sdk
│   ├── auth `认证类`
│   ├── client.go `客户端主入口类`
│   ├── config.go
│   ├── requests `请求类`
│   ├── responses `响应类`
│   └── utils `工具类`
└── services
└── jedi
```
2020-08-06 18:20:43 +08:00
### 2.引入
```go
go get -u golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk
```
或 Go Module
```go
import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk"
```
2020-08-06 17:29:29 +08:00
2020-08-06 18:38:04 +08:00
### 3.服务端认证及返回处理
2020-08-06 17:29:29 +08:00
调用 `auth.UnSign` 方法,提供`*http.Request`请求实例,以及`signer.Signer` 签名器(签名器需要包括凭据)
即可完成整个验签过程
2020-08-06 18:20:43 +08:00
这里以`beego`为例 :
2020-08-06 17:29:29 +08:00
```go
2020-08-06 18:20:43 +08:00
package applications
2020-08-06 17:29:29 +08:00
import (
2020-08-06 18:20:43 +08:00
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/signers"
"golib.gaore.com/GaoreGo/grlogs"
2020-08-06 17:29:29 +08:00
)
func init() {
2020-08-06 18:20:43 +08:00
// 过滤器
2020-08-06 17:29:29 +08:00
beego.InsertFilter("/api/*", beego.BeforeRouter, func(context *context.Context) {
httpRequest := context.Request
2020-08-06 18:20:43 +08:00
err := auth.UnSign(httpRequest, signers.NewAccessKeySigner(&credentials.AccessKeyCredential{
AccessKeyId: "aaaaaa",
AccessKeySecret: "bbbbbb",
AccessKeyFrom: context.Input.Param("access_from"),
}))
if err != nil {
resp := response.NewJsonByDefaultFailed()
resp.Msg = err.Error()
resp.Code = 10086
var (
hasIndent = beego.BConfig.RunMode != beego.PROD
)
context.Output.Status = 500
context.Output.JSON(resp, hasIndent, false)
}
})
}
```
2020-08-06 18:37:26 +08:00
服务端返回的响应体为以下结构的json字符流, `code`,`status`,`msg` 这三个字段*最好*要包含且数据类型要一致 , `data` 部分为业务内容,自行定义
2020-08-06 18:35:41 +08:00
```json
{
"code": 1001,
"msg": "不能为空",
"status": false,
"data": {
...
}
}
```
2020-08-06 18:20:43 +08:00
### 4.sdk编写
在`services`目录下以服务归类新建文件夹, 如`jedi`为短信服务,以 `RpcRequest``BaseResponse` 以基类写好请求子类和响应子类。一个请求接口对应一对请求和响应类。
```go
import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk"
2020-08-06 18:35:41 +08:00
const (
HOST = "jedi" // 如果非全域名会自动补全.gaore.com , 如jedi 会自动补全jedi.goare.com, 也可以打全域名 test.uu89.com
VERSION = "2020-08-04"
)
2020-08-06 18:20:43 +08:00
type Client struct {
sdk.Client
}
func NewClientWithAccessKey(accesskey, secrect, source string) (client *Client, err error) {
client = &Client{}
err = client.InitWithAccessKey(accesskey, secrect, source)
return
}
func (c *Client) Test(req *DemoTestRequest) (response *DemoTestResponse, err error) {
response = CreateDemoTestResponse()
err = c.DoAction(req, response)
return
}
```
请求类,对参数用标签解释, 如 `position:"Query" field:"param_1" default:""` 有三个字段 `position` , `field`, `default`
- `position` 为标志该参数为请求体对应的位置, `Query` query参数, `Body` post请求体参数, `Head`请求头参数
- `field` 解释成参数实际名称
- `default` 默认值
```go
type DemoTestRequest struct {
*requests.RpcRequest
Param1 string `position:"Query" field:"param_1" default:"" `
Param2 int `position:"Query" field:"param_2" default:"10086" `
Param3 bool `position:"Query" field:"param_3" default:"false" `
}
```
对应解释成http请求为
```go
GET param_1=111&param_2=10086&param_3=false
```
2020-08-06 18:21:37 +08:00
完整示例:
2020-08-06 18:20:43 +08:00
```go
package jedi
import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
)
2020-08-06 18:35:41 +08:00
// 定义请求体
2020-08-06 18:20:43 +08:00
type DemoTestRequest struct {
*requests.RpcRequest
Param1 string `position:"Query" field:"param_1" default:"" `
Param2 int `position:"Query" field:"param_2" default:"10086" `
Param3 bool `position:"Query" field:"param_3" default:"false" `
}
func CreateDemoTestRequest() (req *DemoTestRequest) {
req = &DemoTestRequest{RpcRequest: &requests.RpcRequest{}}
2020-08-06 18:35:41 +08:00
// InitWithApiInfo 初始化请求,有三个参数,域名,版本,和路径
// 域名参数如果非全域名会自动补全.gaore.com , 如jedi 会自动补全jedi.goare.com, 也可以打全域名 test.uu89.com
2020-08-06 18:20:43 +08:00
req.InitWithApiInfo(HOST, VERSION, "/api/sms/Index")
2020-08-06 18:35:41 +08:00
// 定义 请求方法 POST 或 GET
2020-08-06 18:20:43 +08:00
req.Method = requests.GET
return
}
2020-08-06 18:35:41 +08:00
// 定义响应体
2020-08-06 18:20:43 +08:00
type DemoTestResponse struct {
*responses.BaseResponse
Data DemoTestResponseData `json:"data"`
2020-08-06 17:29:29 +08:00
}
2020-08-06 18:20:43 +08:00
type DemoTestResponseData struct {
Account string `json:"account"`
Total int `json:"total"`
}
func CreateDemoTestResponse() *DemoTestResponse {
return &DemoTestResponse{
BaseResponse: &responses.BaseResponse{},
}
}
```
终端调试打开方式, 需要在系统环境变量上加入(三选一):
```
DEBUG=sdk,signer,request
2020-08-06 17:29:29 +08:00
```