-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupstream.go
72 lines (57 loc) · 1.41 KB
/
upstream.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
package hibp
import (
"errors"
"fmt"
"io"
"net/http"
)
type hibpClient struct {
endpoint string
httpClient *http.Client
maxRetries int
}
type hibpResponse struct {
NotModified bool
ETag string
Data []byte
}
func (h *hibpClient) RequestRange(rangePrefix, etag string) (*hibpResponse, error) {
req, err := http.NewRequest("GET", h.endpoint+rangePrefix, nil)
if err != nil {
return nil, fmt.Errorf("creating request for range %q: %w", rangePrefix, err)
}
if etag != "" {
req.Header.Set("If-None-Match", etag)
}
var mErr error
for i := 0; i < 1+h.maxRetries; i++ {
resp, err := h.request(req)
if err == nil {
return resp, nil
}
// TODO: Log error with debug level
mErr = errors.Join(mErr, err)
}
return nil, fmt.Errorf("requesting range %q: %w", rangePrefix, mErr)
}
func (h *hibpClient) request(req *http.Request) (*hibpResponse, error) {
resp, err := h.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("executing request: %w", err)
}
if resp.StatusCode == http.StatusNotModified {
return &hibpResponse{NotModified: true}, nil
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("reading response body: %w", err)
}
return &hibpResponse{
ETag: resp.Header.Get("ETag"),
Data: body,
}, nil
}