-
Notifications
You must be signed in to change notification settings - Fork 1
/
formRequest.go
190 lines (160 loc) · 4.1 KB
/
formRequest.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"bytes"
"errors"
"io"
"io/ioutil"
"log"
"mime/multipart"
"net/http"
"os"
"strconv"
"strings"
)
// Creates a new file upload http request with optional extra params
func newfileUploadRequest(uri string, params map[string]string, paramName, path string) (*http.Request, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
fileContents, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
fi, err := file.Stat()
if err != nil {
return nil, err
}
file.Close()
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile(paramName, fi.Name())
if err != nil {
return nil, err
}
part.Write(fileContents)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err = writer.Close()
if err != nil {
return nil, err
}
return http.NewRequest("POST", uri, body)
}
// newFormRequest Creates a new form http request with optional extra params
func newFormRequest(uri string, params map[string]string) (*http.Request, error) {
body := new(bytes.Buffer)
writer := multipart.NewWriter(body)
for key, val := range params {
_ = writer.WriteField(key, val)
}
err := writer.Close()
if err != nil {
return nil, err
}
request, err := http.NewRequest("POST", uri, body)
if err != nil {
return nil, err
}
request.Header.Add("Content-Type", writer.FormDataContentType())
return request, nil
}
func makeRequest(uri string, user string, pass string, formParams map[string]string, requestName string) (string, error) {
var request *http.Request
var method string
var err error
if uri == "" {
return "", errors.New("URI cannot be empty")
}
if formParams == nil {
method = "GET"
request, err = http.NewRequest("GET", uri, nil)
if err != nil {
return "", err
}
} else {
method = "POST"
request, err = newFormRequest(uri, formParams)
if err != nil {
return "", err
}
}
authHeader, err := generateHeader(uri, method, user, pass)
if err != nil {
return "Error on generateHeader", err
}
request.Header.Set("Authorization", authHeader)
log.Println(requestName)
resp, err := http.DefaultClient.Do(request)
if err != nil {
// handle err
return "", errors.New(API_CON_ERROR)
}
defer resp.Body.Close()
bodyBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
bodyString := string(bodyBytes)
//log.Println(resp.StatusCode)
if resp.StatusCode != 200 {
if resp.StatusCode == 404 {
return "", errors.New(API_NOT_FOUND)
}
if resp.StatusCode == 403 {
return "", errors.New(FORBIDEN_ACCESS)
}
if resp.StatusCode == 401 {
return "", errors.New(INVALID_CREDS)
}
s := strconv.Itoa(resp.StatusCode)+"\n"+bodyString[len(bodyString)-1300 : len(bodyString)-900]
return "", errors.New(API_UNEXPECTED_ERROR + "\n\t" + s)
}
if strings.Contains(bodyString,"Access denied"){
return "", errors.New(FORBIDEN_ACCESS)
}
if strings.Contains(bodyString,"Could not find a build"){
return "", errors.New(BUILD_NOT_FOUND)
}
if strings.Contains(bodyString,"<error>"){
//s := strconv.Itoa(resp.StatusCode)+"\n"+bodyString[len(bodyString)-1300 : len(bodyString)-900]
s := strings.Split(bodyString, "<error>")[1]
return "", errors.New(API_UNEXPECTED_ERROR + "\n" +"Error message: "+ s)
}
// log.Println("bodyString:", bodyString)
return bodyString, nil
}
// DownloadFile download a file form URL with basic authentication
func DownloadFile(filepath string, uri string, user string, pass string) error {
var request *http.Request
var err error
if uri == "" {
return errors.New("URI cannot be empty")
}
request, err = http.NewRequest("GET", uri, nil)
if err != nil {
return err
}
authHeader, err := generateHeader(uri, "GET", user, pass)
if err != nil {
return err
}
request.Header.Set("Authorization", authHeader)
log.Println("Starting request")
resp, err := http.DefaultClient.Do(request)
if err != nil {
// handle err
log.Println(err)
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}