This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
client.go
66 lines (58 loc) · 1.57 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
package w3s
import (
"context"
"fmt"
"io"
"io/fs"
"net/http"
bserv "github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
blockstore "github.com/ipfs/go-ipfs-blockstore"
w3http "github.com/web3-storage/go-w3s-client/http"
)
const clientName = "web3.storage/go"
// Client is a HTTP API client to the web3.storage service.
type Client interface {
Get(context.Context, cid.Cid) (*w3http.Web3Response, error)
Put(context.Context, fs.File, ...PutOption) (cid.Cid, error)
PutCar(context.Context, io.Reader) (cid.Cid, error)
Status(context.Context, cid.Cid) (*Status, error)
List(context.Context, ...ListOption) (*UploadIterator, error)
Pin(context.Context, cid.Cid, ...PinOption) (*PinResponse, error)
}
type clientConfig struct {
token string
endpoint string
ds ds.Batching
hc *http.Client
}
type client struct {
cfg *clientConfig
bsvc bserv.BlockService
}
// NewClient creates a new web3.storage API client.
func NewClient(options ...Option) (Client, error) {
cfg := clientConfig{
endpoint: "https://api.web3.storage",
hc: &http.Client{},
}
for _, opt := range options {
if err := opt(&cfg); err != nil {
return nil, err
}
}
if cfg.token == "" {
return nil, fmt.Errorf("missing auth token")
}
c := client{cfg: &cfg}
if cfg.ds != nil {
c.bsvc = bserv.New(blockstore.NewBlockstore(cfg.ds), nil)
} else {
ds := dssync.MutexWrap(ds.NewMapDatastore())
c.bsvc = bserv.New(blockstore.NewBlockstore(ds), nil)
}
return &c, nil
}
var _ Client = (*client)(nil)