7
0
gaore-common-sdk-go/README.md
2020-08-06 18:21:37 +08:00

152 lines
4.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# gaore-common-sdk-go
### 1.目录架构
```
├── README.md
├── sdk
│   ├── auth `认证类`
│   ├── client.go `客户端主入口类`
│   ├── config.go
│   ├── requests `请求类`
│   ├── responses `响应类`
│   └── utils `工具类`
└── services
└── jedi
```
### 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"
```
### 3.服务端认证调用事例
调用 `auth.UnSign` 方法,提供`*http.Request`请求实例,以及`signer.Signer` 签名器(签名器需要包括凭据)
即可完成整个验签过程
这里以`beego`为例 :
```go
package applications
import (
"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"
)
func init() {
// 过滤器
beego.InsertFilter("/api/*", beego.BeforeRouter, func(context *context.Context) {
httpRequest := context.Request
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)
}
})
}
```
### 4.sdk编写
在`services`目录下以服务归类新建文件夹, 如`jedi`为短信服务,以 `RpcRequest``BaseResponse` 以基类写好请求子类和响应子类。一个请求接口对应一对请求和响应类。
```go
import "golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk"
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
```
完整示例:
```go
package jedi
import (
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
)
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{}}
req.InitWithApiInfo(HOST, VERSION, "/api/sms/Index")
req.Method = requests.GET
return
}
type DemoTestResponse struct {
*responses.BaseResponse
Data DemoTestResponseData `json:"data"`
}
type DemoTestResponseData struct {
Account string `json:"account"`
Total int `json:"total"`
}
func CreateDemoTestResponse() *DemoTestResponse {
return &DemoTestResponse{
BaseResponse: &responses.BaseResponse{},
}
}
```