This repository has been archived by the owner on Sep 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
160 lines (136 loc) · 3.5 KB
/
common.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
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
package main
import (
"bytes"
"encoding/json"
"errors"
"io"
"log"
"mime/multipart"
"net/http"
"os"
"path/filepath"
)
type jahiaConnectInfo struct {
url string
siteKey string
user string
password string
}
func get(connectInfo jahiaConnectInfo, url string) (map[string]interface{}, error) {
req, err := http.NewRequest("GET", connectInfo.url+url, nil)
req.SetBasicAuth(connectInfo.user, connectInfo.password)
cli := &http.Client{}
resp, err := cli.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(parseBody(resp).String())
}
defer resp.Body.Close()
return parseJsonBody(resp)
}
func delete(connectInfo jahiaConnectInfo, url string) (map[string]interface{}, error) {
req, err := http.NewRequest("DELETE", connectInfo.url+url, nil)
req.SetBasicAuth(connectInfo.user, connectInfo.password)
cli := &http.Client{}
resp, err := cli.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(parseBody(resp).String())
}
defer resp.Body.Close()
return parseJsonBody(resp)
}
func post(connectInfo jahiaConnectInfo, url string) (map[string]interface{}, error) {
req, err := http.NewRequest("POST", connectInfo.url+url, nil)
req.SetBasicAuth(connectInfo.user, connectInfo.password)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
cli := &http.Client{}
resp, err := cli.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(parseBody(resp).String())
}
defer resp.Body.Close()
return parseJsonBody(resp)
}
func downloadFile(connectInfo jahiaConnectInfo, filepath string, url string) error {
req, err := http.NewRequest("GET", connectInfo.url+url, nil)
req.SetBasicAuth(connectInfo.user, connectInfo.password)
cli := &http.Client{}
resp, err := cli.Do(req)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return errors.New(parseBody(resp).String())
}
defer resp.Body.Close()
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func postFile(connectInfo jahiaConnectInfo, url string, params map[string]string, paramName, filePath string) (map[string]interface{}, error) {
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, filepath.Base(filePath))
if err != nil {
return nil, err
}
_, err = io.Copy(part, file)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", connectInfo.url+url, body)
req.SetBasicAuth(connectInfo.user, connectInfo.password)
req.Header.Set("Content-Type", writer.FormDataContentType())
if err != nil {
log.Fatal(err)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
return parseJsonBody(resp)
}
func parseJsonBody(resp *http.Response) (map[string]interface{}, error) {
var target map[string]interface{}
err := json.NewDecoder(resp.Body).Decode(&target)
return target, err
}
func parseBody(resp *http.Response) *bytes.Buffer {
body := &bytes.Buffer{}
_, err := body.ReadFrom(resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
return body
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}