Commit 8f63ca27 authored by liupei's avatar liupei

init

parents
Pipeline #228 failed with stages
/.idea
/*.exe
/*.gz
/cache
\ No newline at end of file
## 使用帮助
### 安装
```go
go get gitlab.linanquan.com/root/go-lib-dingtalk
```
### 快速开始
```go
import (
"gitlab.linanquan.com/root/go-lib-dingtalk"
)
c := go-lib-dingtalk.NewClient()
// 构造请求参数
req := go-lib-dingtalk.NewRequest()
req.SetPath("/get_jsapi_ticket")
req.SetMethod("Get")
// 执行请求
res := c.Execute(req)
res.IsSuccess() // false
res.GetBodyData("errcode").Int() // 100000
res.GetError()) // 不合法的access_token
```
\ No newline at end of file
package go_lib_dingtalk
import "github.com/astaxie/beego/httplib"
const (
OapiHost = "https://oapi.dingtalk.com"
)
type Client struct {
}
func NewClient() Client {
return Client{}
}
func (c *Client) Execute(req Request) Response {
res := c.sendRequest(req)
return res
}
// sendRequest 发送http请求
func (c *Client) sendRequest(req Request) Response {
var url string
queryString := req.queryParams.Encode()
if queryString == "" {
url = OapiHost + req.path
} else {
url = OapiHost + req.path + "?" + req.queryParams.Encode()
}
var httpReq *httplib.BeegoHTTPRequest
var err error
if req.method == "post" {
httpReq = httplib.Post(url)
} else if req.method == "get" {
httpReq = httplib.Get(url)
httpReq.JSONBody(req.bodyParam)
httpReq.Header("Content-Type", "application/json")
}
res, err := httpReq.Response()
return NewResponse(*res, err)
}
package go_lib_dingtalk
import (
"log"
"net/url"
"testing"
)
func TestGet(t *testing.T) {
c := NewClient()
req := NewRequest()
req.SetPath("/get_jsapi_ticket")
req.SetMethod("Get")
res := c.Execute(req)
log.Println(res.IsSuccess(), res.GetError())
}
func TestPost(t *testing.T) {
c := NewClient()
req := NewRequest()
req.SetPath("/user/create")
query := url.Values{}
query.Add("access_token", "11111")
req.SetQueryParam(query)
req.SetBodyParam(map[string]interface{}{
"userid": "11111",
})
req.SetMethod("Post")
res := c.Execute(req)
log.Println(res.IsSuccess(), res.GetError())
}
module go-lib-dingtalk
go 1.13
require (
github.com/astaxie/beego v1.12.2
github.com/tidwall/gjson v1.6.1
)
This diff is collapsed.
package go_lib_dingtalk
import (
"errors"
"net/url"
"strings"
)
type Request struct {
method string
path string
queryParams url.Values
bodyParam map[string]interface{}
contentType string
}
// NewRequest
func NewRequest() Request {
return Request{
contentType: "application/json",
}
}
// SetMethod 设置请求类型,支持post,get
// sample: SetMethod("POST") 或 SetMethod("post") 都是合法
func (req *Request) SetMethod(method string) error {
lowerMethod := strings.ToLower(method)
if (lowerMethod != "post") && (lowerMethod != "get") {
return errors.New("method: " + method + " is invalid")
}
req.method = lowerMethod
return nil
}
// SetPath 设置请求路
// sample: SetPath("/get_jsapi_ticket")
func (req *Request) SetPath(path string) error {
req.path = path
return nil
}
// SetQueryParam 设置query参数
func (req *Request) SetQueryParam(values url.Values) error {
req.queryParams = values
return nil
}
// SetBodyParam 设置post body参数
// sample:
// SetBodyParam(map[string]interface{}{
// "name": "sven",
// "age": 18
// })
func (req *Request) SetBodyParam(body map[string]interface{}) error {
req.bodyParam = body
return nil
}
package go_lib_dingtalk
import (
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
)
type Response struct {
httpCode int
httpResponse http.Response
httpErr string
errCode int64
errMsg string
httpBody string
success bool
}
// NewResponse 实例化response
func NewResponse(httpResponse http.Response, reqErr error) Response {
res := Response{
httpResponse: httpResponse,
}
if reqErr != nil {
res.httpErr = reqErr.Error()
}
res.init()
return res
}
// init 解析钉钉返回
func (res *Response) init() {
body, err := ioutil.ReadAll(res.httpResponse.Body)
if err != nil {
res.success = false
res.httpErr = err.Error()
return
}
res.httpBody = string(body)
res.errCode = gjson.Get(res.httpBody, "errcode").Int()
if (res.httpCode >= 200 && res.httpCode < 400) && res.errCode == 0 {
res.success = true
} else {
res.success = false
}
res.errMsg = gjson.Get(res.httpBody, "errmsg").String()
}
// IsSuccess 判断返回值是否成功
func (res *Response) IsSuccess() bool {
return res.success
}
// GetHttpCode 返回httpCode
func (res *Response) GetHttpCode() int {
return res.httpCode
}
// GetBodyData 返回钉钉返回数据
func (res *Response) GetBodyData(path string) gjson.Result {
return gjson.Get(res.httpBody, path)
}
// GetError 返回错误信息,优先级:httpErr > errMsg
func (res *Response) GetError() string {
if res.httpErr != "" {
return res.httpErr
}
return res.errMsg
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment