-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
141 lines (122 loc) · 3.21 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
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
package zia
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"reflect"
)
// Request ... // Needs to review this function
func (c *Client) Request(endpoint, method string, data []byte, contentType string) ([]byte, error) {
c.Lock()
defer c.Unlock()
if contentType == "" {
contentType = contentTypeJSON
}
var req *http.Request
var err error
err = c.checkSession()
if err != nil {
return nil, err
}
req, err = http.NewRequest(method, c.URL+endpoint, bytes.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", contentType)
if c.UserAgent != "" {
req.Header.Add("User-Agent", c.UserAgent)
}
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 299 {
return nil, checkErrorInResponse(resp, fmt.Errorf("api responded with code: %d", resp.StatusCode))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}
// Create send HTTP Post request
func (c *Client) Create(endpoint string, o interface{}) (interface{}, error) {
if o == nil {
return nil, errors.New("tried to create with a nil payload not a Struct")
}
t := reflect.TypeOf(o)
if t.Kind() != reflect.Struct {
return nil, errors.New("tried to create with a " + t.Kind().String() + " not a Struct")
}
data, err := json.Marshal(o)
if err != nil {
return nil, err
}
resp, err := c.Request(endpoint, "POST", data, "application/json")
if err != nil {
return nil, err
}
responseObject := reflect.New(t).Interface()
err = json.Unmarshal(resp, &responseObject)
if err != nil {
return nil, err
}
id := reflect.Indirect(reflect.ValueOf(responseObject)).FieldByName("ID")
log.Printf("Created Object with ID %v", id)
return responseObject, nil
}
// Read ...
func (c *Client) Read(endpoint string, o interface{}) error {
contentType := c.GetContentType()
resp, err := c.Request(endpoint, "GET", nil, contentType)
if err != nil {
return err
}
err = json.Unmarshal(resp, o)
if err != nil {
return err
}
return nil
}
// Update ...
func (c *Client) UpdateWithPut(endpoint string, o interface{}) (interface{}, error) {
return c.updateGeneric(endpoint, o, "PUT", "application/json")
}
// Update ...
func (c *Client) Update(endpoint string, o interface{}) (interface{}, error) {
return c.updateGeneric(endpoint, o, "PATCH", "application/merge-patch+json")
}
// Update ...
func (c *Client) updateGeneric(endpoint string, o interface{}, method, contentType string) (interface{}, error) {
if o == nil {
return nil, errors.New("tried to update with a nil payload not a Struct")
}
t := reflect.TypeOf(o)
if t.Kind() != reflect.Struct {
return nil, errors.New("tried to update with a " + t.Kind().String() + " not a Struct")
}
data, err := json.Marshal(o)
if err != nil {
return nil, err
}
resp, err := c.Request(endpoint, method, data, contentType)
if err != nil {
return nil, err
}
responseObject := reflect.New(t).Interface()
err = json.Unmarshal(resp, &responseObject)
return responseObject, err
}
// Delete ...
func (c *Client) Delete(endpoint string) error {
_, err := c.Request(endpoint, "DELETE", nil, "application/json")
if err != nil {
return err
}
return nil
}