-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
141 lines (108 loc) · 3 KB
/
client.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
package httpx
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/avast/retry-go/v4"
"github.com/gabriel-vasile/mimetype"
"github.com/samber/lo"
)
var (
ErrorNoResults = errors.New("no results")
)
const (
DefaultTimeout = 3 * time.Second
DefaultAttempts = 3
)
type Client interface {
Fetch(ctx context.Context, path string) (io.ReadCloser, error)
GetContentType(ctx context.Context, path string) (string, error)
}
var _ Client = (*httpClient)(nil)
type httpClient struct {
httpClient *http.Client
attempts uint
}
func (h *httpClient) Fetch(ctx context.Context, path string) (readCloser io.ReadCloser, err error) {
retryableFunc := func() error {
readCloser, err = h.fetch(ctx, path)
return err
}
retryIfFunc := func(err error) bool {
return !errors.Is(err, ErrorNoResults)
}
if err := retry.Do(retryableFunc, retry.Attempts(h.attempts), retry.RetryIf(retryIfFunc)); err != nil {
return nil, fmt.Errorf("retry attempts: %w", err)
}
return readCloser, nil
}
func (h *httpClient) fetch(ctx context.Context, path string) (io.ReadCloser, error) {
request, err := http.NewRequestWithContext(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, fmt.Errorf("new request: %w", err)
}
response, err := h.httpClient.Do(request)
if err != nil {
return nil, fmt.Errorf("send request: %w", err)
}
if response.StatusCode != http.StatusOK {
defer lo.Try(response.Body.Close)
return nil, fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
return response.Body, nil
}
func (h *httpClient) GetContentType(ctx context.Context, uri string) (string, error) {
// TODO: Support chain:// protocol.
if strings.HasPrefix(uri, "chain://") {
return "text/html", nil
}
request, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return "", fmt.Errorf("new request: %w", err)
}
request.Header.Set("User-Agent", "curl/7.86.0")
response, err := h.httpClient.Do(request)
if err != nil {
return "", fmt.Errorf("send request: %w", err)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status code: %d", response.StatusCode)
}
mimeType, err := mimetype.DetectReader(response.Body)
if err != nil || mimeType == nil {
return "", fmt.Errorf("fail to detect mime type %s", uri)
}
return mimeType.String(), nil
}
func NewHTTPClient(options ...ClientOption) (Client, error) {
instance := httpClient{
httpClient: &http.Client{
Timeout: DefaultTimeout,
},
attempts: DefaultAttempts,
}
for _, option := range options {
if err := option(&instance); err != nil {
return nil, fmt.Errorf("apply options: %w", err)
}
}
return &instance, nil
}
type ClientOption func(*httpClient) error
func WithAttempts(attempts uint) ClientOption {
return func(h *httpClient) error {
h.attempts = attempts
return nil
}
}
func WithTimeout(timeout time.Duration) ClientOption {
return func(h *httpClient) error {
h.httpClient.Timeout = timeout
return nil
}
}