This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
/
http.go
79 lines (72 loc) · 1.92 KB
/
http.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
package onapp
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
var cl http.Client
func (c *Client) getReq(path ...string) ([]byte, error, int) {
url := c.makeUri(path...)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err, -1
}
req.SetBasicAuth(c.apiUser, c.apiPassword)
resp, err := cl.Do(req)
if err != nil {
return nil, err, -1
}
return c.readResponse(resp)
}
func (c *Client) postReq(body string, path ...string) ([]byte, error, int) {
url := c.makeUri(path...)
req, err := http.NewRequest("POST", url, strings.NewReader(body))
if err != nil {
return nil, err, -1
}
req.SetBasicAuth(c.apiUser, c.apiPassword)
resp, err := cl.Do(req)
if err != nil {
return nil, err, -1
}
return c.readResponse(resp)
}
func (c *Client) readResponse(resp *http.Response) ([]byte, error, int) {
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err, resp.StatusCode
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil,
errors.New(fmt.Sprintf("Bad response on '%s' call: HTTP %d - %s\n%s", resp.Request.URL, resp.StatusCode, resp.Status, data)),
resp.StatusCode
}
return data, nil, resp.StatusCode
}
// Concatenates the path elements together, and then to the dashboard URL.
func (c *Client) makeUri(toConcat ...string) string {
buf := bytes.NewBufferString("")
if strings.Index(c.Server, "http://") == -1 && strings.Index(c.Server, "https://") == -1 {
buf.WriteString("https://")
}
buf.WriteString(c.Server)
if c.Server[len(c.Server)-1] != '/' {
buf.WriteByte('/')
}
for _, v := range toConcat {
buf.WriteString(v)
}
return buf.String()
}
// This is a union of all of the possible outer fields
// returned by the OnApp API, so that we don't need to
// have pointless nesting.
// * TestUnmarshalInner expects wrap by "outer":{}
// * /profile.json wrapped by "user":{}
type jsonOuterFields struct {
User Profile
}