Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit d96d731

Browse files
author
Alan Shaw
committed
feat: wip client.Get
1 parent 5656ca6 commit d96d731

File tree

2 files changed

+75
-16
lines changed

2 files changed

+75
-16
lines changed

client.go

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ import (
1212

1313
"github.com/alanshaw/go-carbites"
1414
"github.com/filecoin-project/go-address"
15+
"github.com/ipfs/go-blockservice"
1516
bserv "github.com/ipfs/go-blockservice"
1617
"github.com/ipfs/go-cid"
1718
ds "github.com/ipfs/go-datastore"
1819
dssync "github.com/ipfs/go-datastore/sync"
1920
blockstore "github.com/ipfs/go-ipfs-blockstore"
21+
files "github.com/ipfs/go-ipfs-files"
2022
ipld "github.com/ipfs/go-ipld-format"
2123
"github.com/ipfs/go-merkledag"
24+
unixfile "github.com/ipfs/go-unixfs/file"
2225
"github.com/ipfs/ipfs-cluster/api"
2326
"github.com/ipld/go-car"
2427
peer "github.com/libp2p/go-libp2p-core/peer"
@@ -30,15 +33,60 @@ const iso8601 = "2006-01-02T15:04:05Z0700"
3033

3134
// Client is a HTTP API client to the web3.storage service.
3235
type Client interface {
33-
Get(context.Context, cid.Cid) (GetResponse, error)
36+
Get(context.Context, cid.Cid) (*Web3Response, error)
3437
Put(context.Context, fs.File, ...PutOption) (cid.Cid, error)
3538
PutCar(context.Context, io.Reader) (cid.Cid, error)
36-
Status(context.Context, cid.Cid) (Status, error)
39+
Status(context.Context, cid.Cid) (*Status, error)
3740
}
3841

39-
// GetResponse is a response to a call to the Get method.
40-
type GetResponse interface {
41-
Files() []fs.File
42+
// Web3Response is a response to a call to the Get method.
43+
type Web3Response struct {
44+
*http.Response
45+
}
46+
47+
// Files consumes the HTTP response and collects a slice of all UnixFs files.
48+
func (r *Web3Response) Files() (fs.File, error) {
49+
cr, err := car.NewCarReader(r.Body)
50+
if err != nil {
51+
return nil, err
52+
}
53+
54+
bsvc := newMemBlockService()
55+
for {
56+
b, err := cr.Next()
57+
if err != nil {
58+
if err == io.EOF {
59+
break
60+
}
61+
return nil, err
62+
}
63+
err = bsvc.AddBlock(b)
64+
if err != nil {
65+
return nil, err
66+
}
67+
}
68+
69+
ctx := r.Request.Context()
70+
dsvc := merkledag.NewDAGService(bsvc)
71+
72+
rootCid := cr.Header.Roots[0]
73+
rootNd, err := dsvc.Get(ctx, rootCid)
74+
if err != nil {
75+
return nil, err
76+
}
77+
78+
f, err := unixfile.NewUnixfsFile(ctx, dsvc, rootNd)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
return unixfsToFs(f)
84+
}
85+
86+
func unixfsToFs(n files.Node) (fs.File, error) {
87+
if d, ok := n.(files.Directory); ok {
88+
89+
}
4290
}
4391

4492
type PinStatus int
@@ -262,9 +310,13 @@ func NewClient(options ...Option) (Client, error) {
262310
return &c, nil
263311
}
264312

265-
func newMemDag() ipld.DAGService {
313+
func newMemBlockService() blockservice.BlockService {
266314
ds := dssync.MutexWrap(ds.NewMapDatastore())
267-
bs := bserv.New(blockstore.NewBlockstore(ds), nil)
315+
return bserv.New(blockstore.NewBlockstore(ds), nil)
316+
}
317+
318+
func newMemDag() ipld.DAGService {
319+
bs := newMemBlockService()
268320
return merkledag.NewDAGService(bs)
269321
}
270322

@@ -294,8 +346,14 @@ func (c *client) sendCar(ctx context.Context, r io.Reader) (cid.Cid, error) {
294346
return cid.Parse(out.Cid)
295347
}
296348

297-
func (c *client) Get(ctx context.Context, cid cid.Cid) (GetResponse, error) {
298-
return nil, fmt.Errorf("not implemented")
349+
func (c *client) Get(ctx context.Context, cid cid.Cid) (*Web3Response, error) {
350+
req, err := http.NewRequest("GET", fmt.Sprintf("%s/car/%s", c.cfg.endpoint, cid), nil)
351+
if err != nil {
352+
return nil, err
353+
}
354+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
355+
res, err := c.hc.Do(req)
356+
return &Web3Response{res}, err
299357
}
300358

301359
type putConfig struct {
@@ -400,24 +458,24 @@ func (c *client) PutCar(ctx context.Context, car io.Reader) (cid.Cid, error) {
400458
return root, sendErr
401459
}
402460

403-
func (c *client) Status(ctx context.Context, cid cid.Cid) (Status, error) {
404-
var s Status
461+
func (c *client) Status(ctx context.Context, cid cid.Cid) (*Status, error) {
405462
req, err := http.NewRequest("GET", fmt.Sprintf("%s/status/%s", c.cfg.endpoint, cid), nil)
406463
if err != nil {
407-
return s, err
464+
return nil, err
408465
}
409466
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
410467
res, err := c.hc.Do(req)
411468
if err != nil {
412-
return s, err
469+
return nil, err
413470
}
414471
if res.StatusCode != 200 {
415-
return s, fmt.Errorf("unexpected response status: %d", res.StatusCode)
472+
return nil, fmt.Errorf("unexpected response status: %d", res.StatusCode)
416473
}
474+
var s Status
417475
d := json.NewDecoder(res.Body)
418476
err = d.Decode(&s)
419477
if err != nil {
420-
return s, err
478+
return nil, err
421479
}
422-
return s, nil
480+
return &s, nil
423481
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/ipfs/go-datastore v0.4.6
1111
github.com/ipfs/go-ipfs-blockstore v1.0.4
1212
github.com/ipfs/go-ipfs-chunker v0.0.5
13+
github.com/ipfs/go-ipfs-files v0.0.8
1314
github.com/ipfs/go-ipfs-posinfo v0.0.1
1415
github.com/ipfs/go-ipld-format v0.2.0
1516
github.com/ipfs/go-merkledag v0.4.0

0 commit comments

Comments
 (0)