-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequest.go
More file actions
163 lines (135 loc) · 3.79 KB
/
request.go
File metadata and controls
163 lines (135 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package confluence
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"net/http"
"net/url"
"path/filepath"
)
// 错误信息的响应结构
type ErrorResp struct {
StatusCode int
Data ErrorData
Message string
Reason string
}
// 错误响应中的错误数据
type ErrorData struct {
Authorized bool
Valid bool
AllowedInReadOnlyMode bool
Successful bool
Errors []interface{}
}
//可供展开的字段信息
type ExpandableResponse map[string]string
// 响应信息中的链接信息
type LinkResp struct {
Base string
Context string
Next string
Self string
WebUI string
Download string
}
// 响应信息中的分页信息
type PageResp struct {
Size int
Start int
Limit int
Links LinkResp `json:"_links,omitempty"`
}
//下载指定链接的内容
func (cli *Client) Download(downloadUrl string) ([]byte, error) {
u, err := url.Parse(downloadUrl)
if err != nil {
return nil, err
}
resp, err := cli.Request("GET", u.Path, u.Query(), nil, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
//发起GET类型的API请求
func (cli *Client) ApiGET(path string, query url.Values) (*http.Response, error) {
return cli.ApiRequest("GET", path, query, nil, nil)
}
//发起POST类型的API请求
func (cli *Client) ApiPOST(path string, data interface{}) (*http.Response, error) {
r, err := dataToJsonReader(data)
if err != nil {
return nil, fmt.Errorf("编码请求数据失败: %s", err)
}
return cli.ApiRequest("POST", path, nil, nil, r)
}
//发起PUT类型的API请求
func (cli *Client) ApiPUT(path string, data interface{}) (*http.Response, error) {
r, err := dataToJsonReader(data)
if err != nil {
return nil, fmt.Errorf("编码请求数据失败: %s", err)
}
return cli.ApiRequest("PUT", path, nil, nil, r)
}
//发起POST类型的文件上传请求
func (cli *Client) ApiPOSTFiles(path string, files []string) (*http.Response, error) {
var body bytes.Buffer
w := multipart.NewWriter(&body)
for _, file := range files {
fw, err := w.CreateFormFile("file", file)
if err != nil {
return nil, fmt.Errorf("创建上传字段错误: %s", err)
}
content, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("读取文件%s错误: %s", file, err)
}
_, err = fw.Write(content)
if err != nil {
return nil, fmt.Errorf("添加上传文件%s错误: %s", file, err)
}
}
w.Close()
header := url.Values{
"X-Atlassian-Token": {"no-check"},
"Content-Type": {w.FormDataContentType()},
}
return cli.ApiRequest("POST", path, nil, header, &body)
}
//发起指定方法的API请求
func (cli *Client) ApiRequest(method, path string, query, header url.Values, body io.Reader) (*http.Response, error) {
return cli.Request(method, filepath.Join("/rest/api", path), query, header, body)
}
//执行指定的HTTP请求,执行前会自动添加上认证信息和Content-Type信息
func (cli *Client) Request(method, path string, query, header url.Values, body io.Reader) (*http.Response, error) {
// 检查添加Query参数
if query != nil {
path += "?" + query.Encode()
}
// 构造请求
req, err := http.NewRequest(method, cli.Hostname+path, body)
if err != nil {
return nil, fmt.Errorf("创建请求失败: %s", err)
}
if cli.Username != "" {
req.SetBasicAuth(cli.Username, cli.Password)
}
req.Header.Set("Content-Type", "application/json")
for name, _ := range header {
req.Header.Set(name, header.Get(name))
}
return http.DefaultClient.Do(req)
}
//数据转换为JSON流reader
func dataToJsonReader(data interface{}) (io.Reader, error) {
jsonData, err := json.Marshal(data)
if err != nil {
return nil, fmt.Errorf("无法编码Data: %s", err)
}
return bytes.NewReader(jsonData), nil
}