-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcache.go
More file actions
113 lines (100 loc) · 2.51 KB
/
Copy pathcache.go
File metadata and controls
113 lines (100 loc) · 2.51 KB
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
package cacheclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/maxmcd/bramble/internal/store"
"github.com/maxmcd/bramble/pkg/chunkedarchive"
"github.com/maxmcd/bramble/pkg/httpx"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)
type cacheClient interface {
PostChunk(context.Context, io.Reader) (string, error)
PostDerivation(context.Context, store.Derivation) (string, error)
PostOutput(context.Context, store.OutputRequestBody) error
}
type Client struct {
host string
client *http.Client
}
var _ cacheClient = new(Client)
func New(host string) *Client {
return &Client{
host: host,
client: &http.Client{
Transport: otelhttp.NewTransport(http.DefaultTransport),
},
}
}
func (cc *Client) request(ctx context.Context, method, path, contentType string, body io.Reader, resp interface{}) (err error) {
url := fmt.Sprintf("%s/%s",
strings.TrimSuffix(cc.host, "/"),
strings.TrimPrefix(path, "/"),
)
return httpx.Request(ctx, cc.client, method, url, contentType, body, resp)
}
func (cc *Client) PostDerivation(ctx context.Context, drv store.Derivation) (filename string, err error) {
return filename, cc.request(ctx,
http.MethodPost,
"/derivation",
"application/json",
bytes.NewBuffer(drv.JSON()),
&filename)
}
func (cc *Client) PostOutput(ctx context.Context, req store.OutputRequestBody) (err error) {
b, err := json.Marshal(req)
if err != nil {
return err
}
return cc.request(ctx,
http.MethodPost,
"/output",
"application/json",
bytes.NewBuffer(b),
nil)
}
func (cc *Client) PostChunk(ctx context.Context, chunk io.Reader) (hash string, err error) {
return hash, cc.request(ctx,
http.MethodPost,
"/chunk",
"application/octet-stream",
chunk,
&hash)
}
func (cc *Client) GetDerivation(ctx context.Context, filename string) (drv store.Derivation, exists bool, err error) {
err = cc.request(ctx,
http.MethodGet,
"/derivation/"+filename,
"",
nil,
drv)
if err == os.ErrNotExist {
return drv, false, nil
}
return drv, err == nil, err
}
func (cc *Client) GetOutput(ctx context.Context, hash string) (output []chunkedarchive.TOCEntry, exists bool, err error) {
err = cc.request(ctx,
http.MethodGet,
"/output/"+hash,
"",
nil,
&output)
if err == os.ErrNotExist {
return nil, false, nil
}
return output, err == nil, err
}
func (cc *Client) GetChunk(ctx context.Context, hash string, chunk io.Writer) (err error) {
return cc.request(ctx,
http.MethodGet,
"/chunk/"+hash,
"",
nil,
chunk)
}