-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpclient.go
81 lines (68 loc) · 2.15 KB
/
httpclient.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
package client
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"path"
)
type HTTPClient struct {
Scheme string
Host string
BasePath string
client http.Client
}
func NewHTTPClient(host, basePath, scheme string, client http.Client) *HTTPClient {
return &HTTPClient{
Scheme: scheme,
Host: host,
BasePath: basePath,
client: client,
}
}
func (hc *HTTPClient) Do(ctx context.Context, r *http.Request) ([]byte, error) {
res, err := hc.client.Do(r)
if err != nil {
return nil, fmt.Errorf("can't do request: %w", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusUnauthorized && res.StatusCode != http.StatusBadRequest {
return nil, fmt.Errorf("can't do request: %w", fmt.Errorf("%w to %s: %d", ErrWrongStatusCode, res.Request.URL.String(), res.StatusCode))
} else if res.StatusCode == http.StatusUnauthorized {
return nil, fmt.Errorf("can't do request: %w", fmt.Errorf("%w to %s", Err401StatusCode, res.Request.URL.String()))
} else if res.StatusCode == http.StatusBadRequest {
return nil, fmt.Errorf("can't do request: %w", fmt.Errorf("%w to %s", Err400StatusCode, res.Request.URL.String()))
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("can't do request: %w", fmt.Errorf("can't read response body: %w", err))
}
return body, nil
}
// CreateRequest return http.Request with given parameters. If you don't need some of the parameters then give nil.
func (hc *HTTPClient) CreateRequest(ctx context.Context, httpMethod string, url string, header http.Header, body io.Reader, query url.Values) (*http.Request, error) {
req, err := http.NewRequest(httpMethod, url, body)
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrCantCreateRequest, err)
}
if header != nil {
req.Header = header
}
if query != nil {
req.URL.RawQuery = query.Encode()
}
return req, nil
}
func (hc *HTTPClient) GetFullUrl() *url.URL {
return &url.URL{
Scheme: hc.Scheme,
Host: hc.Host,
Path: path.Join(hc.BasePath, "/"),
}
}
func (hc *HTTPClient) GetUlrWithMethods(method string) *url.URL {
u := hc.GetFullUrl()
u.Path = path.Join(u.Path, method)
return u
}