-
Notifications
You must be signed in to change notification settings - Fork 61
/
transport.go
119 lines (110 loc) · 2.81 KB
/
transport.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
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
package godingtalk
import (
"bytes"
"encoding/json"
"errors"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"net/url"
"os"
)
const typeJSON = "application/json"
//UploadFile is for uploading a single file to DingTalk
type UploadFile struct {
FieldName string
FileName string
Reader io.Reader
}
//DownloadFile is for downloading a single file from DingTalk
type DownloadFile struct {
MediaID string
FileName string
Reader io.Reader
}
func (c *DingTalkClient) httpRPC(path string, params url.Values, requestData interface{}, responseData Unmarshallable) error {
if c.AccessToken != "" {
if params == nil {
params = url.Values{}
}
if params.Get("access_token") == "" {
params.Set("access_token", c.AccessToken)
}
}
return c.httpRequest(path, params, requestData, responseData)
}
func (c *DingTalkClient) httpRequest(path string, params url.Values, requestData interface{}, responseData Unmarshallable) error {
client := c.HTTPClient
var request *http.Request
ROOT := os.Getenv("oapi_server")
if ROOT == "" {
ROOT = "oapi.dingtalk.com"
}
DEBUG := os.Getenv("debug") != ""
url2 := "https://" + ROOT + "/" + path + "?" + params.Encode()
// log.Println(url2)
if requestData != nil {
switch requestData.(type) {
case UploadFile:
var b bytes.Buffer
w := multipart.NewWriter(&b)
uploadFile := requestData.(UploadFile)
if uploadFile.Reader == nil {
return errors.New("upload file is empty")
}
fw, err := w.CreateFormFile(uploadFile.FieldName, uploadFile.FileName)
if err != nil {
return err
}
if _, err = io.Copy(fw, uploadFile.Reader); err != nil {
return err
}
if err = w.Close(); err != nil {
return err
}
request, _ = http.NewRequest("POST", url2, &b)
request.Header.Set("Content-Type", w.FormDataContentType())
default:
d, _ := json.Marshal(requestData)
if DEBUG {
log.Printf("url: %s request: %s", url2, string(d))
}
request, _ = http.NewRequest("POST", url2, bytes.NewReader(d))
request.Header.Set("Content-Type", typeJSON)
}
} else {
if DEBUG {
log.Printf("url: %s", url2)
}
request, _ = http.NewRequest("GET", url2, nil)
}
resp, err := client.Do(request)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New("Server error: " + resp.Status)
}
defer resp.Body.Close()
contentType := resp.Header.Get("Content-Type")
if DEBUG {
log.Printf("url: %s response content type: %s", url2, contentType)
}
pos := len(typeJSON)
if len(contentType) >= pos && contentType[0:pos] == typeJSON {
content, err := ioutil.ReadAll(resp.Body)
if DEBUG {
log.Println(string(content))
}
if err == nil {
json.Unmarshal(content, responseData)
return responseData.checkError()
}
} else {
io.Copy(responseData.getWriter(), resp.Body)
return responseData.checkError()
}
return err
}