-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
77 lines (61 loc) · 2.26 KB
/
api.go
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
package wrikego
import (
"fmt"
"io"
"net/http"
"net/url"
params "github.com/TGoers-FNSB/WrikeGo/parameters"
)
func Get(config Config, path string, params url.Values) ([]byte, *http.Response, error) {
return api("GET", config, path, params)
}
func Post(config Config, path string, params url.Values) ([]byte, *http.Response, error) {
return api("POST", config, path, params)
}
func Put(config Config, path string, params url.Values) ([]byte, *http.Response, error) {
return api("PUT", config, path, params)
}
func Delete(config Config, path string, params url.Values) ([]byte, *http.Response, error) {
return api("DELETE", config, path, params)
}
func Download(config Config, path string, params url.Values) ([]byte, *http.Response, error) {
return api("GET", config, path, params)
}
func Upload(config Config, path string, params params.UploadAttachment) ([]byte, *http.Response, error) {
return apiAttachment("POST", config, path, params)
}
func Update(config Config, path string, params params.UploadAttachment) ([]byte, *http.Response, error) {
return apiAttachment("PUT", config, path, params)
}
func api(method string, config Config, path string, params url.Values) ([]byte, *http.Response, error) {
url := config.BaseUrl + path + "?" + params.Encode()
client := http.Client{}
req, err := http.NewRequest(method, url, nil)
ErrorCheck(err)
req.Header.Add("Authorization", "Bearer "+config.PermAccessToken)
res, err := client.Do(req)
ErrorCheck(err)
defer res.Body.Close()
response, err := io.ReadAll(res.Body)
ErrorCheck(err)
return response, res, err
}
func apiAttachment(method string, config Config, path string, params params.UploadAttachment) ([]byte, *http.Response, error) {
url := config.BaseUrl + path
if params.Url != "" {
url += fmt.Sprintf("?url=%s", params.Url)
}
client := http.Client{}
req, err := http.NewRequest(method, url, params.DataBinary)
ErrorCheck(err)
req.Header.Add("Authorization", "Bearer "+config.PermAccessToken)
req.Header.Add("content-type", "application/octet-stream")
req.Header.Add("X-Requested-With", "XMLHttpRequest")
req.Header.Add("X-File-Name", params.FileName)
res, err := client.Do(req)
ErrorCheck(err)
defer res.Body.Close()
response, err := io.ReadAll(res.Body)
ErrorCheck(err)
return response, res, err
}