/
put.go
38 lines (35 loc) · 881 Bytes
/
put.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
package w3s
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/ipfs/go-cid"
)
func (c *client) PutCar(ctx context.Context, r io.Reader) (cid.Cid, error) {
req, err := http.NewRequestWithContext(ctx, "POST", c.cfg.endpoint+"/car", r)
if err != nil {
return cid.Undef, err
}
req.Header.Add("Content-Type", "application/car")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
req.Header.Add("X-Client", clientName)
res, err := c.cfg.hc.Do(req)
if err != nil {
return cid.Undef, err
}
if res.StatusCode != 200 {
b, _ := io.ReadAll(res.Body)
return cid.Undef, fmt.Errorf("error putting CAR data to Web3.Storage: %v %v", res.Status, string(b))
}
d := json.NewDecoder(res.Body)
var out struct {
Cid string `json:"cid"`
}
err = d.Decode(&out)
if err != nil {
return cid.Undef, err
}
return cid.Parse(out.Cid)
}