-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
98 lines (80 loc) · 2.45 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package pantopoda
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
// ResponseError is an error implementation for client and server errors in API calls.
type ResponseError struct {
Status string
Payload []byte
}
func (e ResponseError) Error() string {
return fmt.Sprintf("%s: %s", e.Status, e.Payload)
}
// Pantopoda is a HTTP client that makes it easy to send HTTP requests and
// trivial to integrate with web services.
type Pantopoda struct {
}
// NewPantopoda generate new instance of pantopoda client
func NewPantopoda() *Pantopoda {
return &Pantopoda{}
}
// Request sends a `method` request to the `endpoint` with given request data.
func (c *Pantopoda) Request(method string, endpoint string, request Request) (Response, error) {
var b []byte
if request.HasBody() {
b = request.Payload.ToJSON()
} else {
b = []byte("{}")
}
if !request.Query.Empty() {
endpoint = endpoint + "?" + request.Query.ToString()
}
req, err := http.NewRequest(method, endpoint, bytes.NewBuffer(b))
if err != nil {
return Response{}, err
}
for key, value := range request.Headers {
req.Header.Set(key, value)
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return Response{}, err
}
defer resp.Body.Close()
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Response{}, err
}
if resp.StatusCode >= 300 {
statusErr := ResponseError{
Status: resp.Status,
Payload: resBody,
}
return newResponse(resp, resBody), statusErr
}
return newResponse(resp, resBody), nil
}
// Get sends a GET request to `endpoint` with given data.
func (c *Pantopoda) Get(endpoint string, request Request) (Response, error) {
return c.Request("GET", endpoint, request)
}
// Post sends a POST request to `endpoint` with given data.
func (c *Pantopoda) Post(endpoint string, request Request) (Response, error) {
return c.Request("POST", endpoint, request)
}
// Put sends a PUT request to `endpoint` with given given data.
func (c *Pantopoda) Put(endpoint string, request Request) (Response, error) {
return c.Request("PUT", endpoint, request)
}
// Patch sends a PATCH request to `endpoint` with given given data.
func (c *Pantopoda) Patch(endpoint string, request Request) (Response, error) {
return c.Request("PATCH", endpoint, request)
}
// Delete sends a DELETE request to `endpoint` with given given data.
func (c *Pantopoda) Delete(endpoint string, request Request) (Response, error) {
return c.Request("DELETE", endpoint, request)
}