-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublish.go
55 lines (52 loc) · 1.42 KB
/
publish.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
package publisher
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strings"
)
func Publish(changeset *MetaChangeset, organizationId string, baseUrl string, username string, password string) (string, error) {
req, err := MakeRequest(changeset, organizationId, baseUrl, username, password)
if err != nil {
return "", err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
if res.StatusCode >= 400 {
txt, err := httputil.DumpResponse(res, true)
if err != nil {
return "", err
}
cleaned := strings.ReplaceAll(string(txt), "\r\n", "\n")
return "", fmt.Errorf("HTTP request failed:\n\n%s", cleaned)
}
buf := new(bytes.Buffer)
_, err = buf.ReadFrom(res.Body)
if err != nil {
return "", err
}
return buf.String(), nil
}
func MakeRequest(changeset *MetaChangeset, organizationId string, baseUrl string, username string, password string) (*http.Request, error) {
body, err := json.MarshalIndent(changeset, "", " ")
if err != nil {
return nil, err
}
u, err := url.Parse(baseUrl)
if err != nil {
return nil, err
}
u.Path = "/api/organization/" + url.PathEscape(organizationId) + "/changeset"
req, err := http.NewRequest(http.MethodPost, u.String(), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/vnd.smartbear.onereport.changeset.v1+json")
req.SetBasicAuth(username, password)
return req, nil
}