parent
f3654bb868
commit
6e49867d6f
@ -0,0 +1,37 @@
|
|||||||
|
package kuaidi100
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/dtapps/go-library/utils/golog"
|
||||||
|
"github.com/dtapps/go-library/utils/gorequest"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ClientConfig 实例配置
|
||||||
|
type ClientConfig struct {
|
||||||
|
Customer string // 授权码
|
||||||
|
Key string // 密钥
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client 实例
|
||||||
|
type Client struct {
|
||||||
|
requestClient *gorequest.App // 请求服务
|
||||||
|
config struct {
|
||||||
|
customer string // 授权码
|
||||||
|
key string // 密钥
|
||||||
|
}
|
||||||
|
zap struct {
|
||||||
|
status bool // 状态
|
||||||
|
client *golog.ApiZapLog // 日志服务
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewClient 创建实例化
|
||||||
|
func NewClient(config *ClientConfig) (*Client, error) {
|
||||||
|
|
||||||
|
c := &Client{}
|
||||||
|
|
||||||
|
c.config.customer = config.Customer
|
||||||
|
|
||||||
|
c.requestClient = gorequest.NewHttp()
|
||||||
|
|
||||||
|
return c, nil
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package kuaidi100
|
||||||
|
|
||||||
|
import "github.com/dtapps/go-library/utils/golog"
|
||||||
|
|
||||||
|
func (c *Client) Config(customer string) *Client {
|
||||||
|
c.config.customer = customer
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConfigZapClientFun 日志配置
|
||||||
|
func (c *Client) ConfigZapClientFun(apiZapLogFun golog.ApiZapLogFun) {
|
||||||
|
apiZapLog := apiZapLogFun()
|
||||||
|
if apiZapLog != nil {
|
||||||
|
c.zap.client = apiZapLog
|
||||||
|
c.zap.status = true
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package kuaidi100
|
||||||
|
|
||||||
|
const (
|
||||||
|
apiUrl = "https://poll.kuaidi100.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
LogTable = "kuaidi100"
|
||||||
|
)
|
@ -0,0 +1,9 @@
|
|||||||
|
package kuaidi100
|
||||||
|
|
||||||
|
func (c *Client) GetCustomer() string {
|
||||||
|
return c.config.customer
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) GetKey() string {
|
||||||
|
return c.config.key
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package kuaidi100
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/dtapps/go-library/utils/gojson"
|
||||||
|
"github.com/dtapps/go-library/utils/gorequest"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PollResponse struct {
|
||||||
|
Result bool `json:"result"`
|
||||||
|
ReturnCode string `json:"returnCode"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PollResult struct {
|
||||||
|
Result PollResponse // 结果
|
||||||
|
Body []byte // 内容
|
||||||
|
Http gorequest.Response // 请求
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPollResult(result PollResponse, body []byte, http gorequest.Response) *PollResult {
|
||||||
|
return &PollResult{Result: result, Body: body, Http: http}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Poll 实时快递查询接口
|
||||||
|
// https://api.kuaidi100.com/document/5f0ffb5ebc8da837cbd8aefc
|
||||||
|
func (c *Client) Poll(ctx context.Context, notMustParams ...gorequest.Params) (*PollResult, error) {
|
||||||
|
// 参数
|
||||||
|
params := gorequest.NewParamsWith(notMustParams...)
|
||||||
|
// 请求
|
||||||
|
request, err := c.request(ctx, apiUrl+"/poll", params, http.MethodPost)
|
||||||
|
if err != nil {
|
||||||
|
return newPollResult(PollResponse{}, request.ResponseBody, request), err
|
||||||
|
}
|
||||||
|
// 定义
|
||||||
|
var response PollResponse
|
||||||
|
err = gojson.Unmarshal(request.ResponseBody, &response)
|
||||||
|
return newPollResult(response, request.ResponseBody, request), err
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package kuaidi100
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"github.com/dtapps/go-library/utils/gojson"
|
||||||
|
"github.com/dtapps/go-library/utils/gorequest"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) request(ctx context.Context, url string, params map[string]interface{}, method string) (gorequest.Response, error) {
|
||||||
|
|
||||||
|
newParams := gorequest.NewParams()
|
||||||
|
|
||||||
|
// 公共参数
|
||||||
|
newParams["customer"] = c.GetCustomer()
|
||||||
|
|
||||||
|
// 请求参数
|
||||||
|
newParams["param"] = gojson.JsonEncodeNoError(params)
|
||||||
|
|
||||||
|
// 签名
|
||||||
|
newParams["sign"] = c.getSign(gojson.JsonEncodeNoError(params))
|
||||||
|
|
||||||
|
// 创建请求
|
||||||
|
client := c.requestClient
|
||||||
|
|
||||||
|
// 设置请求地址
|
||||||
|
client.SetUri(url)
|
||||||
|
|
||||||
|
// 设置方式
|
||||||
|
client.SetMethod(method)
|
||||||
|
|
||||||
|
// 设置格式
|
||||||
|
client.SetContentTypeForm()
|
||||||
|
|
||||||
|
// 设置参数
|
||||||
|
client.SetParams(params)
|
||||||
|
|
||||||
|
// 发起请求
|
||||||
|
request, err := client.Request(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return gorequest.Response{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录日志
|
||||||
|
if c.zap.status {
|
||||||
|
go c.zap.client.Middleware(ctx, request)
|
||||||
|
}
|
||||||
|
|
||||||
|
return request, err
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package kuaidi100
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/dtapps/go-library/utils/gomd5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (c *Client) getSign(param string) string {
|
||||||
|
return gomd5.ToUpper(fmt.Sprintf("%s%s%s", param, c.GetKey(), c.GetCustomer()))
|
||||||
|
}
|
Loading…
Reference in new issue