This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathclient.go
81 lines (68 loc) · 2.46 KB
/
client.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
package parsecli
import (
"net/http"
"net/url"
"github.com/bgentry/heroku-go"
"github.com/facebookgo/parse"
"github.com/facebookgo/stackerr"
)
// ParseAPIClient is the http client used by parse-cli
type ParseAPIClient struct {
APIClient *parse.Client
}
func NewParseAPIClient(e *Env) (*ParseAPIClient, error) {
baseURL, err := url.Parse(e.Server)
if err != nil {
return nil, stackerr.Newf("invalid server URL %q: %s", e.Server, err)
}
return &ParseAPIClient{
APIClient: &parse.Client{
BaseURL: baseURL,
},
}, nil
}
func NewHerokuAPIClient(e *Env) (*heroku.Client, error) {
return &heroku.Client{}, nil
}
func (c *ParseAPIClient) appendCommonHeaders(header http.Header) http.Header {
if header == nil {
header = make(http.Header)
}
header.Add("User-Agent", UserAgent)
return header
}
// Get performs a GET method call on the given url and unmarshal response into
// result.
func (c *ParseAPIClient) Get(u *url.URL, result interface{}) (*http.Response, error) {
return c.Do(&http.Request{Method: "GET", URL: u}, nil, result)
}
// Post performs a POST method call on the given url with the given body and
// unmarshal response into result.
func (c *ParseAPIClient) Post(u *url.URL, body, result interface{}) (*http.Response, error) {
return c.Do(&http.Request{Method: "POST", URL: u}, body, result)
}
// Put performs a PUT method call on the given url with the given body and
// unmarshal response into result.
func (c *ParseAPIClient) Put(u *url.URL, body, result interface{}) (*http.Response, error) {
return c.Do(&http.Request{Method: "PUT", URL: u}, body, result)
}
// Delete performs a DELETE method call on the given url and unmarshal response
// into result.
func (c *ParseAPIClient) Delete(u *url.URL, result interface{}) (*http.Response, error) {
return c.Do(&http.Request{Method: "DELETE", URL: u}, nil, result)
}
// RoundTrip is a wrapper for parse.Client.RoundTrip
func (c *ParseAPIClient) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header = c.appendCommonHeaders(req.Header)
return c.APIClient.RoundTrip(req)
}
// Do is a wrapper for parse.Client.Do
func (c *ParseAPIClient) Do(req *http.Request, body, result interface{}) (*http.Response, error) {
req.Header = c.appendCommonHeaders(req.Header)
return c.APIClient.Do(req, body, result)
}
// WithCredentials is a wrapper for parse.Client.WithCredentials
func (c *ParseAPIClient) WithCredentials(cr parse.Credentials) *ParseAPIClient {
c.APIClient = c.APIClient.WithCredentials(cr)
return c
}