From 47a5618b4cd935928d865c700f61706d1d061510 Mon Sep 17 00:00:00 2001 From: Acha Bill Date: Tue, 14 Nov 2023 11:58:57 +0100 Subject: [PATCH] feat: added download opts --- pkg/bee/api/api.go | 24 +++++++++++++------ pkg/bee/api/files.go | 4 ++-- pkg/bee/client.go | 6 ++--- pkg/check/fileretrieval/fileretrieval.go | 4 ++-- .../longavailability/longavailability.go | 4 +++- pkg/check/settlements/settlements.go | 2 +- pkg/test/node.go | 2 +- 7 files changed, 29 insertions(+), 17 deletions(-) diff --git a/pkg/bee/api/api.go b/pkg/bee/api/api.go index 68e5a6ac9..f79c377e4 100644 --- a/pkg/bee/api/api.go +++ b/pkg/bee/api/api.go @@ -8,18 +8,20 @@ import ( "io" "net/http" "net/url" + "strconv" "strings" "github.com/ethersphere/beekeeper" ) const ( - apiVersion = "v1" - contentType = "application/json; charset=utf-8" - postageStampBatchHeader = "Swarm-Postage-Batch-Id" - deferredUploadHeader = "Swarm-Deferred-Upload" - swarmPinHeader = "Swarm-Pin" - swarmTagHeader = "Swarm-Tag" + apiVersion = "v1" + contentType = "application/json; charset=utf-8" + postageStampBatchHeader = "Swarm-Postage-Batch-Id" + deferredUploadHeader = "Swarm-Deferred-Upload" + swarmPinHeader = "Swarm-Pin" + swarmTagHeader = "Swarm-Tag" + swarmCacheDownloadHeader = "Swarm-Cache" ) var userAgent = "beekeeper/" + beekeeper.Version @@ -171,7 +173,7 @@ func encodeJSON(w io.Writer, v interface{}) (err error) { } // requestData handles the HTTP request response cycle. -func (c *Client) requestData(ctx context.Context, method, path string, body io.Reader, v interface{}) (resp io.ReadCloser, err error) { +func (c *Client) requestData(ctx context.Context, method, path string, body io.Reader, opts *DownloadOptions) (resp io.ReadCloser, err error) { req, err := http.NewRequest(method, path, body) if err != nil { return nil, err @@ -191,6 +193,10 @@ func (c *Client) requestData(ctx context.Context, method, path string, body io.R req.Header.Set("Authorization", "Bearer "+key) } + if opts != nil && opts.Cache != nil { + req.Header.Set(swarmCacheDownloadHeader, strconv.FormatBool(*opts.Cache)) + } + r, err := c.httpClient.Do(req) if err != nil { return nil, err @@ -309,3 +315,7 @@ type UploadOptions struct { BatchID string Direct bool } + +type DownloadOptions struct { + Cache *bool +} diff --git a/pkg/bee/api/files.go b/pkg/bee/api/files.go index bcae3cc34..973752b5b 100644 --- a/pkg/bee/api/files.go +++ b/pkg/bee/api/files.go @@ -14,8 +14,8 @@ import ( type FilesService service // Download downloads data from the node -func (f *FilesService) Download(ctx context.Context, a swarm.Address) (resp io.ReadCloser, err error) { - return f.client.requestData(ctx, http.MethodGet, "/"+apiVersion+"/bzz/"+a.String(), nil, nil) +func (f *FilesService) Download(ctx context.Context, a swarm.Address, opts *DownloadOptions) (resp io.ReadCloser, err error) { + return f.client.requestData(ctx, http.MethodGet, "/"+apiVersion+"/bzz/"+a.String(), nil, opts) } // FilesUploadResponse represents Upload's response diff --git a/pkg/bee/client.go b/pkg/bee/client.go index f0de2350e..c06a9f90c 100644 --- a/pkg/bee/client.go +++ b/pkg/bee/client.go @@ -204,9 +204,9 @@ func (c *Client) DownloadChunk(ctx context.Context, a swarm.Address, targets str return io.ReadAll(r) } -// DownloadFile downloads chunk from the node and returns it's size and hash -func (c *Client) DownloadFile(ctx context.Context, a swarm.Address) (size int64, hash []byte, err error) { - r, err := c.api.Files.Download(ctx, a) +// DownloadFile downloads chunk from the node and returns it's size and hash. +func (c *Client) DownloadFile(ctx context.Context, a swarm.Address, opts *api.DownloadOptions) (size int64, hash []byte, err error) { + r, err := c.api.Files.Download(ctx, a, opts) if err != nil { return 0, nil, fmt.Errorf("download file %s: %w", a, err) } diff --git a/pkg/check/fileretrieval/fileretrieval.go b/pkg/check/fileretrieval/fileretrieval.go index 453c412fe..d878fb269 100644 --- a/pkg/check/fileretrieval/fileretrieval.go +++ b/pkg/check/fileretrieval/fileretrieval.go @@ -122,7 +122,7 @@ func (c *Check) defaultCheck(ctx context.Context, cluster orchestration.Cluster, client = clients[lastNodeName] - size, hash, err := client.DownloadFile(ctx, file.Address()) + size, hash, err := client.DownloadFile(ctx, file.Address(), nil) if err != nil { return fmt.Errorf("node %s: %w", lastNodeName, err) } @@ -194,7 +194,7 @@ func (c *Check) fullCheck(ctx context.Context, cluster orchestration.Cluster, o } t1 := time.Now() - size, hash, err := nc.DownloadFile(ctx, file.Address()) + size, hash, err := nc.DownloadFile(ctx, file.Address(), nil) if err != nil { return fmt.Errorf("node %s: %w", n, err) } diff --git a/pkg/check/longavailability/longavailability.go b/pkg/check/longavailability/longavailability.go index a2a339d9c..369078944 100644 --- a/pkg/check/longavailability/longavailability.go +++ b/pkg/check/longavailability/longavailability.go @@ -6,6 +6,7 @@ import ( "time" "github.com/ethersphere/bee/pkg/swarm" + "github.com/ethersphere/beekeeper/pkg/bee/api" "github.com/ethersphere/beekeeper/pkg/beekeeper" "github.com/ethersphere/beekeeper/pkg/logging" "github.com/ethersphere/beekeeper/pkg/orchestration" @@ -92,7 +93,8 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, o interf c.logger.Infof("node %s: download attempt %d for %s", node.Name(), i+1, addr) start := time.Now() - size, _, err := node.Client().DownloadFile(ctx, addr) + cache := false + size, _, err := node.Client().DownloadFile(ctx, addr, &api.DownloadOptions{Cache: &cache}) if err != nil { c.metrics.FailedDownloadAttempts.WithLabelValues(labelValue).Inc() c.logger.Errorf("node %s: download %s error: %v", node.Name(), addr, err) diff --git a/pkg/check/settlements/settlements.go b/pkg/check/settlements/settlements.go index ac22bbc5e..03cb1ba41 100644 --- a/pkg/check/settlements/settlements.go +++ b/pkg/check/settlements/settlements.go @@ -174,7 +174,7 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int dNode := pickAtRandom(rnd, sortedNodes, uNode) c.logger.Infof("download starting from %s", overlays[dNode].String()) - size, hash, err := clients[dNode].DownloadFile(ctx, file.Address()) + size, hash, err := clients[dNode].DownloadFile(ctx, file.Address(), nil) if err != nil { return fmt.Errorf("node %s: %w", dNode, err) } diff --git a/pkg/test/node.go b/pkg/test/node.go index c0cf68cfd..608285cf6 100644 --- a/pkg/test/node.go +++ b/pkg/test/node.go @@ -65,7 +65,7 @@ func (b *BeeV2) UploadFile(ctx context.Context, file File) error { } func (b *BeeV2) ExpectToHaveFile(ctx context.Context, file File) error { - size, hash, err := b.client.DownloadFile(ctx, file.address) + size, hash, err := b.client.DownloadFile(ctx, file.address, nil) if err != nil { return fmt.Errorf("node %s: %w", b.name, err) }