generated from theriverman/go-cli-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests.go
158 lines (136 loc) · 4.7 KB
/
requests.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package vocdriver
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// Evaluation Tools
var httpSuccessCodes = [...]int{
http.StatusOK,
http.StatusCreated,
http.StatusAccepted,
http.StatusNoContent,
}
// RequestService is a handle to HTTP request operations
type RequestService struct {
client *Client
}
// SuccessfulHTTPRequest returns true if the given Response's StatusCode
// is one of `[...]int{200, 201, 202, 204}`; otherwise returns false
// Taiga does not return status codes other than above stated
func SuccessfulHTTPRequest(response *http.Response) bool {
for _, code := range httpSuccessCodes {
if response.StatusCode == code {
return true
}
}
return false
}
// Get a handler for composing a new HTTP GET request
//
// - URL must be an absolute (full) URL to the desired endpoint
// - ResponseBody must be a pointer to a struct representing the fields returned by Taiga
func (s *RequestService) Get(url string, responseBody interface{}) (*http.Response, error) {
return newRawRequest("GET", s.client, responseBody, url, nil)
}
// Head a handler for composing a new HTTP HEAD request
func (s *RequestService) Head() {
panic("HEAD requests are not implemented")
}
// Post a handler for composing a new HTTP POST request
//
// - URL must be an absolute (full) URL to the desired endpoint
// - Payload must be a pointer to a complete struct which will be sent to Taiga
// - ResponseBody must be a pointer to a struct representing the fields returned by Taiga
func (s *RequestService) Post(url string, payload interface{}, responseBody interface{}) (*http.Response, error) {
return newRawRequest("POST", s.client, responseBody, url, payload)
}
// Put a handler for composing a new HTTP PUT request
//
// - URL must be an absolute (full) URL to the desired endpoint
// - Payload must be a pointer to a complete struct which will be sent to Taiga
// - ResponseBody must be a pointer to a struct representing the fields returned by Taiga
func (s *RequestService) Put(url string, payload interface{}, responseBody interface{}) (*http.Response, error) {
return newRawRequest("PUT", s.client, responseBody, url, payload)
}
// Patch a handler for composing a new HTTP PATCH request
//
// - URL must be an absolute (full) URL to the desired endpoint
// - Payload must be a pointer to a complete struct which will be sent to Taiga
// - ResponseBody must be a pointer to a struct representing the fields returned by Taiga
func (s *RequestService) Patch(url string, payload interface{}, responseBody interface{}) (*http.Response, error) {
return newRawRequest("PATCH", s.client, responseBody, url, payload)
}
// Delete a handler for composing a new HTTP DELETE request
//
// - URL must be an absolute (full) URL to the desired endpoint
func (s *RequestService) Delete(url string) (*http.Response, error) {
return newRawRequest("DELETE", s.client, nil, url, nil)
}
// Connect a handler for composing a new HTTP CONNECT request
func (s *RequestService) Connect() {
panic("CONNECT requests are not implemented")
}
// Options a handler for composing a new HTTP OPTIONS request
func (s *RequestService) Options() {
panic("OPTIONS requests are not implemented")
}
// Trace a handler for composing a new HTTP TRACE request
func (s *RequestService) Trace() {
panic("TRACE requests are not implemented")
}
func newRawRequest(requestType string, c *Client, responseBody interface{}, url string, payload interface{}) (*http.Response, error) {
// New RAW request
var request *http.Request
var err error
switch {
case payload == nil:
request, err = http.NewRequest(requestType, url, nil)
if err != nil {
return nil, err
}
case payload != nil:
body, err := json.Marshal(payload)
if err != nil {
return nil, err
}
request, err = http.NewRequest(requestType, url, bytes.NewBuffer(body))
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("failed to build request because the received payload could not be processed")
}
// Load Headers
for key, values := range *c.Headers {
for _, value := range values {
request.Header.Add(key, value)
}
}
// Execute request
resp, err := c.HTTPClient.Do(request)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// Evaluate response status code
if SuccessfulHTTPRequest(resp) {
if resp.StatusCode == http.StatusNoContent { // There's no body returned for 204 responses
return resp, nil
}
// We expect content so convert response JSON string to struct
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&responseBody)
if err != nil {
return nil, err
}
return resp, nil
}
rawResponseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return nil, fmt.Errorf(string(rawResponseBody))
}