-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblobstore.go
53 lines (50 loc) · 1.26 KB
/
blobstore.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
package pocketic
import (
"encoding/hex"
"fmt"
"io"
"net/http"
)
// GetBlob retrieves a binary blob from the PocketIC server.
func (pic PocketIC) GetBlob(blobID []byte) ([]byte, error) {
var bytes []byte
if err := pic.do(
http.MethodGet,
fmt.Sprintf("%s/blobstore/%s", pic.server.URL(), hex.EncodeToString(blobID)),
nil,
&bytes,
); err != nil {
return nil, err
}
return bytes, nil
}
// UploadBlob uploads and stores a binary blob to the PocketIC server.
func (pic PocketIC) UploadBlob(bytes []byte, gzipCompression bool) ([]byte, error) {
method := http.MethodPost
url := fmt.Sprintf("%s/blobstore", pic.server.URL())
pic.logger.Printf("[POCKETIC] %s %s %+v", method, url, bytes)
req, err := newRequest(method, url, bytes)
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/octet-stream")
if gzipCompression {
req.Header.Set("content-encoding", "gzip")
}
resp, err := pic.client.Do(req)
if err != nil {
return nil, err
}
hexBlobID, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
blobID, err := hex.DecodeString(string(hexBlobID))
if err != nil {
return nil, err
}
return blobID, nil
}