-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
99 lines (88 loc) · 2.29 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
package blobscache
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"time"
ia "github.com/castai/image-analyzer"
json "github.com/json-iterator/go"
)
func NewRemoteBlobsCacheClient(serverURL string) ia.CacheClient {
return &remoteBlobsCache{
url: serverURL,
client: &http.Client{
Timeout: 10 * time.Second,
},
}
}
type remoteBlobsCache struct {
url string
client *http.Client
}
func (c *remoteBlobsCache) PutBlob(ctx context.Context, key string, blob []byte) error {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&PubBlobRequest{
Key: key,
Blob: blob,
}); err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/blobscache/PutBlob", c.url), &buf)
if err != nil {
return err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if st := resp.StatusCode; st != http.StatusOK {
errMsg, _ := io.ReadAll(resp.Body)
return fmt.Errorf("put blob failed, response status=%d: %v", st, string(errMsg))
}
return nil
}
func (c *remoteBlobsCache) GetBlob(ctx context.Context, key string) ([]byte, error) {
var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(&GetBlobRequest{
Key: key,
}); err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, "POST", fmt.Sprintf("%s/blobscache/GetBlob", c.url), &buf)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if st := resp.StatusCode; st != http.StatusOK {
if st == http.StatusNotFound {
return nil, ia.ErrCacheNotFound
}
errMsg, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("put blob failed, response status=%d: %v", st, string(errMsg))
}
var res GetBlobResponse
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
return nil, err
}
return res.Blob, nil
}
func NewMockBlobsCacheClient() ia.CacheClient {
return &mockBlobsCacheClient{}
}
type mockBlobsCacheClient struct {
}
func (m mockBlobsCacheClient) PutBlob(ctx context.Context, key string, blob []byte) error {
return nil
}
func (m mockBlobsCacheClient) GetBlob(ctx context.Context, key string) ([]byte, error) {
return nil, ia.ErrCacheNotFound
}