-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_client_call.go
168 lines (146 loc) · 4 KB
/
http_client_call.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
159
160
161
162
163
164
165
166
167
168
package client
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
)
// Constants for error messages.
const (
errorEmptyHost = "empty host"
errorEmptyMethod = "empty method"
errorMethodNotAllowed = "method not allowed"
)
// HTTPClientDoer is an interface for executing an HTTP request.
type HTTPClientDoer interface {
Do(*http.Request) (*http.Response, error)
}
// HTTPClientCall encapsulates the configuration and execution of an HTTP request.
type HTTPClientCall struct {
client HTTPClientDoer
method string
host string
path string
params url.Values
headers http.Header
body any
isEncodeURL bool
gzipCompress bool
}
// NewHTTPClientCall creates a new HTTPClientCall with the specified host and HTTP client.
func NewHTTPClientCall(host string, client HTTPClientDoer) *HTTPClientCall {
if client == nil {
panic("You must create client")
}
if host == "" {
panic("empty host")
}
return &HTTPClientCall{
client: client,
host: host,
path: "",
params: nil,
isEncodeURL: true,
method: "",
headers: nil,
body: nil,
gzipCompress: false,
}
}
// Path sets the path for the HTTP request.
func (r *HTTPClientCall) Path(path string) *HTTPClientCall {
r.path = path
return r
}
// Params sets the URL parameters for the HTTP request.
func (r *HTTPClientCall) Params(params url.Values) *HTTPClientCall {
r.params = params
return r
}
// IsEncodeURL sets whether the URL should be encoded.
func (r *HTTPClientCall) IsEncodeURL(isEncodeURL bool) *HTTPClientCall {
r.isEncodeURL = isEncodeURL
return r
}
// Method sets the HTTP method for the request.
func (r *HTTPClientCall) Method(method string) *HTTPClientCall {
r.method = method
return r
}
// Headers sets the HTTP headers for the request.
func (r *HTTPClientCall) Headers(headers http.Header) *HTTPClientCall {
r.headers = headers
return r
}
// Body sets the body for the HTTP request.
func (r *HTTPClientCall) Body(body any) *HTTPClientCall {
r.body = body
return r
}
// UseGzipCompress sets whether the request body should be gzip compressed.
func (r *HTTPClientCall) UseGzipCompress(gzipCompress bool) *HTTPClientCall {
r.gzipCompress = gzipCompress
return r
}
// Do executes the HTTP request with the configured settings.
func (r *HTTPClientCall) Do(ctx context.Context) (*http.Response, error) {
if r.host == "" {
return nil, errors.New(errorEmptyHost)
}
if err := r.validateHTTPMethod(); err != nil {
return nil, err
}
fullURL := r.constructURL()
req, err := newClientRequest(ctx, r.method, fullURL)
if err != nil {
return nil, err
}
if err = r.setRequestBody(req); err != nil {
return nil, err
}
r.setHeaders(req)
resp, err := r.client.Do(req)
r.params = nil
r.body = nil
return resp, err
}
// HTTPClientCallResponse encapsulates the response status code from an HTTP request.
type HTTPClientCallResponse struct {
StatusCode int `json:"status_code"`
}
// DoWithUnmarshal executes the HTTP request and unmarshals the response body into the provided interface.
func (r *HTTPClientCall) DoWithUnmarshal(ctx context.Context, responseBody any) (*HTTPClientCallResponse, error) {
resp, err := r.Do(ctx)
if err != nil {
return nil, err
}
defer func() {
_ = resp.Body.Close()
}()
contentType := resp.Header.Get("Content-Type")
decoder := selectDecoder(contentType)
if decoder == nil {
return nil, fmt.Errorf("unsupported content type: %s", contentType)
}
err = decoder.Decode(resp.Body, responseBody)
if err != nil {
return nil, err
}
httpClientCallResponse := &HTTPClientCallResponse{
StatusCode: resp.StatusCode,
}
return httpClientCallResponse, nil
}
// validateHTTPMethod checks if the HTTP method is valid and allowed.
func (r *HTTPClientCall) validateHTTPMethod() error {
if r.method == "" {
return errors.New(errorEmptyMethod)
}
switch r.method {
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete, http.MethodPatch:
return nil
default:
return errors.New(errorMethodNotAllowed)
}
}