-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequest.go
136 lines (110 loc) · 3.6 KB
/
request.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
/**
* Copyright (c) 2023 Sebastian Borchers
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
package wego
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
func (c *Client) newAuthenticatedGETRequest(ctx context.Context, endpoint string) (req *http.Request, err error) {
req, err = c.newGETRequest(ctx, endpoint)
if err != nil {
return
}
c.authenticateRequest(ctx, req)
return
}
func (c *Client) newGETRequest(ctx context.Context, endpoint string) (req *http.Request, err error) {
req, err = http.NewRequestWithContext(ctx, http.MethodGet, c.opts.RemoteAddr+endpoint, nil)
if err != nil {
return nil, fmt.Errorf("new http GET request: %v", err)
}
// Set headers.
req.Header.Set("Accept", "application/json")
return
}
func (c *Client) newAuthenticatedPOSTRequest(ctx context.Context, endpoint string, body any) (req *http.Request, err error) {
// Marshal the request data to JSON.
reqData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal json: %v", err)
}
req, err = http.NewRequestWithContext(ctx, http.MethodPost, c.opts.RemoteAddr+endpoint, bytes.NewReader(reqData))
if err != nil {
return nil, fmt.Errorf("new http POST request: %v", err)
}
// Set headers.
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
c.authenticateRequest(ctx, req)
return
}
func (c *Client) newAuthenticatedPUTRequest(ctx context.Context, endpoint string, body any) (req *http.Request, err error) {
// Marshal the request data to JSON.
reqData, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("failed to marshal json: %v", err)
}
req, err = http.NewRequestWithContext(ctx, http.MethodPut, c.opts.RemoteAddr+endpoint, strings.NewReader(string(reqData)))
if err != nil {
return nil, fmt.Errorf("new http POST request: %v", err)
}
// Set headers.
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")
c.authenticateRequest(ctx, req)
return
}
func (c *Client) newAuthenticatedDELETERequest(ctx context.Context, endpoint string) (req *http.Request, err error) {
req, err = http.NewRequestWithContext(ctx, http.MethodDelete, c.opts.RemoteAddr+endpoint, nil)
if err != nil {
return nil, fmt.Errorf("new http DELETE request: %v", err)
}
// Set headers.
c.authenticateRequest(ctx, req)
return
}
// doSimpleRequest is a helper that executes the given request and attempts to parse
// its JSON response into resp.
// The argument resp must be a pointer.
// If any other status code than 200 is received, an error is returned.
func (c *Client) doSimpleRequest(req *http.Request, resp any) error {
r, err := c.httpc.Do(req)
if err != nil {
return fmt.Errorf("failed to send POST request: %v", err)
} else if r.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status code '%d' received", r.StatusCode)
}
// If no return value is expected, do not parse the response.
if resp == nil {
return nil
}
// Parse response.
err = parseResponse(r, &resp)
if err != nil {
return fmt.Errorf("failed to parse response: %w", err)
}
return nil
}
// Returns io.EOF, if the response was empty, but dst is not nil.
func parseResponse(resp *http.Response, dst any) error {
data, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("failed to read response: %v", err)
} else if len(data) == 0 && dst != nil {
return io.EOF
}
err = json.Unmarshal(data, dst)
if err != nil {
return fmt.Errorf("failed to unmarshal response: %v; raw response: %s", err, string(data))
}
return nil
}