-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrequest.go
More file actions
136 lines (111 loc) · 3.22 KB
/
request.go
File metadata and controls
136 lines (111 loc) · 3.22 KB
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
package gokick
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
func makeRequest[T any](
ctx context.Context,
request *Client,
method string,
path string,
statusCode int,
body io.Reader,
) (Response[T], error) {
url := request.buildURL(request.options.APIBaseURL, path)
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return Response[T]{}, fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := request.do(req)
if err != nil {
return Response[T]{}, fmt.Errorf("failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return Response[T]{}, nil
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return Response[T]{}, fmt.Errorf("failed to read response body (KICK status code %d): %v", resp.StatusCode, err)
}
if resp.StatusCode != statusCode {
var errorOutput errorResponse
err = json.Unmarshal(responseBody, &errorOutput)
if err != nil {
return Response[T]{}, fmt.Errorf(
"failed to unmarshal error response (KICK status code: %d and body %q): %v",
resp.StatusCode,
string(responseBody),
err,
)
}
return Response[T]{}, NewError(resp.StatusCode, errorOutput.Message)
}
type successResponse struct {
Result T `json:"data"`
}
var success successResponse
err = json.Unmarshal(responseBody, &success)
if err != nil {
return Response[T]{}, fmt.Errorf(
"failed to unmarshal response body (KICK status code %d and body %q): %v", resp.StatusCode, string(responseBody), err,
)
}
return Response[T](success), nil
}
func makeAuthRequest[T any](
ctx context.Context,
request *Client,
method string,
path string,
statusCode int,
body io.Reader,
) (T, error) {
url := request.buildURL(request.options.AuthBaseURL, path)
var response T
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return response, fmt.Errorf("failed to create request: %v", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := request.do(req)
if err != nil {
return response, fmt.Errorf("failed to make request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNoContent {
return response, nil
}
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return response, fmt.Errorf("failed to read response body (KICK status code %d): %v", resp.StatusCode, err)
}
if resp.StatusCode != statusCode {
var errorOutput authErrorResponse
err = json.Unmarshal(responseBody, &errorOutput)
if err != nil {
return response, fmt.Errorf(
"failed to unmarshal error response (KICK status code: %d and body %q): %v",
resp.StatusCode,
string(responseBody),
err,
)
}
if errorOutput.Message != "" {
return response, NewError(resp.StatusCode, errorOutput.Message)
} else {
return response, NewError(resp.StatusCode, errorOutput.Error).WithDescription(errorOutput.ErrorDescription)
}
}
err = json.Unmarshal(responseBody, &response)
if err != nil {
return response, fmt.Errorf(
"failed to unmarshal response body (KICK status code %d and body %q): %v", resp.StatusCode, string(responseBody), err,
)
}
return response, nil
}