-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/act #408
Feat/act #408
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package api | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/ethersphere/bee/pkg/swarm" | ||
) | ||
|
||
type ActService service | ||
|
||
type ActUploadResponse struct { | ||
Reference swarm.Address `json:"reference"` | ||
HistoryAddress swarm.Address | ||
} | ||
|
||
type ActGranteesResponse struct { | ||
Reference swarm.Address `json:"ref"` | ||
HistoryAddress swarm.Address `json:"historyref"` | ||
} | ||
|
||
func (a *ActService) Download(ctx context.Context, addr swarm.Address, opts *DownloadOptions) (resp io.ReadCloser, err error) { | ||
return a.client.requestData(ctx, http.MethodGet, "/"+apiVersion+"/bzz/"+addr.String()+"/", nil, opts) | ||
} | ||
|
||
func (a *ActService) Upload(ctx context.Context, name string, data io.Reader, o UploadOptions) (ActUploadResponse, error) { | ||
var resp ActUploadResponse | ||
h := http.Header{} | ||
h.Add(postageStampBatchHeader, o.BatchID) | ||
h.Add("swarm-deferred-upload", "true") | ||
h.Add("content-type", "application/octet-stream") | ||
h.Add("Swarm-Act", "true") | ||
h.Add(swarmPinHeader, "true") | ||
historyParser := func(h http.Header) { | ||
resp.HistoryAddress, _ = swarm.ParseHexAddress(h.Get("Swarm-Act-History-Address")) | ||
} | ||
err := a.client.requestWithHeader(ctx, http.MethodPost, "/"+apiVersion+"/bzz?"+url.QueryEscape("name="+name), h, data, &resp, historyParser) | ||
return resp, err | ||
} | ||
|
||
func (a *ActService) AddGrantees(ctx context.Context, data io.Reader, o UploadOptions) (ActGranteesResponse, error) { | ||
var resp ActGranteesResponse | ||
h := http.Header{} | ||
h.Add(postageStampBatchHeader, o.BatchID) | ||
h.Add(swarmActHistoryAddress, o.ActHistoryAddress.String()) | ||
err := a.client.requestWithHeader(ctx, http.MethodPost, "/"+apiVersion+"/grantee", h, data, &resp) | ||
return resp, err | ||
} | ||
|
||
func (a *ActService) GetGrantees(ctx context.Context, addr swarm.Address) (resp io.ReadCloser, err error) { | ||
return a.client.requestData(ctx, http.MethodGet, "/"+apiVersion+"/grantee/"+addr.String(), nil, nil) | ||
} | ||
|
||
func (a *ActService) PatchGrantees(ctx context.Context, data io.Reader, addr swarm.Address, haddr swarm.Address, batchID string) (ActGranteesResponse, error) { | ||
var resp ActGranteesResponse | ||
h := http.Header{} | ||
h.Add("swarm-postage-batch-id", batchID) | ||
h.Add("swarm-act-history-address", haddr.String()) | ||
err := a.client.requestWithHeader(ctx, http.MethodPatch, "/"+apiVersion+"/grantee/"+addr.String(), h, data, &resp) | ||
return resp, err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ import ( | |
"bytes" | ||
"context" | ||
"crypto/tls" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
|
@@ -223,6 +224,21 @@ func (c *Client) DownloadFile(ctx context.Context, a swarm.Address, opts *api.Do | |
return size, h.Sum(nil), nil | ||
} | ||
|
||
func (c *Client) DownloadActFile(ctx context.Context, a swarm.Address, opts *api.DownloadOptions) (size int64, hash []byte, err error) { | ||
r, err := c.api.Act.Download(ctx, a, opts) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("download file %s: %w", a, err) | ||
} | ||
defer r.Close() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If an error occurs and r is not nil then this line will not be called and 'r' will not close. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 'r' is of type io.ReadCloser. In case of an error, r will be nil, so Close should not be called to avoid a potential panic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defer r.Close removed https://github.com/Solar-Punk-Ltd/beekeeper/blob/feat/act/pkg/bee/client.go#L233 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ferencsarai sorry for misunderstanding, "defer r.Close()" should be there (like you placed it). I was trying to reply on @martinconic message, but I forgot to tag him. Please, revert like it was. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
h := fileHasher() | ||
size, err = io.Copy(h, r) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("download file %s, hashing copy: %w", a, err) | ||
} | ||
|
||
return size, h.Sum(nil), nil | ||
} | ||
|
||
// HasChunk returns true/false if node has a chunk | ||
func (c *Client) HasChunk(ctx context.Context, a swarm.Address) (bool, error) { | ||
return c.api.Node.HasChunk(ctx, a) | ||
|
@@ -750,6 +766,55 @@ func (c *Client) UploadFile(ctx context.Context, f *File, o api.UploadOptions) ( | |
return | ||
} | ||
|
||
func (c *Client) UploadActFile(ctx context.Context, f *File, o api.UploadOptions) (err error) { | ||
h := fileHasher() | ||
r, err := c.api.Act.Upload(ctx, f.Name(), io.TeeReader(f.DataReader(), h), o) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we have also some There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. r is ActUploadResponse struct |
||
if err != nil { | ||
return fmt.Errorf("upload ACT file: %w", err) | ||
} | ||
|
||
f.SetAddress(r.Reference) | ||
f.SetHistroryAddress(r.HistoryAddress) | ||
f.SetHash(h.Sum(nil)) | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) AddActGrantees(ctx context.Context, f *File, o api.UploadOptions) (err error) { | ||
h := fileHasher() | ||
r, err := c.api.Act.AddGrantees(ctx, io.TeeReader(f.DataReader(), h), o) | ||
if err != nil { | ||
return fmt.Errorf("add ACT grantees: %w", err) | ||
} | ||
|
||
f.SetAddress(r.Reference) | ||
f.SetHistroryAddress(r.HistoryAddress) | ||
f.SetHash(h.Sum(nil)) | ||
|
||
return nil | ||
} | ||
|
||
func (c *Client) GetActGrantees(ctx context.Context, a swarm.Address) (addresses []string, err error) { | ||
r, e := c.api.Act.GetGrantees(ctx, a) | ||
if e != nil { | ||
return nil, fmt.Errorf("get grantees: %s: %w", a, e) | ||
} | ||
defer r.Close() | ||
err = json.NewDecoder(r).Decode(&addresses) | ||
return addresses, err | ||
} | ||
|
||
func (c *Client) PatchActGrantees(ctx context.Context, pf *File, addr swarm.Address, haddr swarm.Address, batchID string) (err error) { | ||
r, err := c.api.Act.PatchGrantees(ctx, pf.DataReader(), addr, haddr, batchID) | ||
if err != nil { | ||
return fmt.Errorf("add ACT grantees: %w", err) | ||
} | ||
|
||
pf.SetAddress(r.Reference) | ||
pf.SetHistroryAddress(r.HistoryAddress) | ||
return nil | ||
} | ||
|
||
// UploadCollection uploads TAR collection bytes to the node | ||
func (c *Client) UploadCollection(ctx context.Context, f *File, o api.UploadOptions) (err error) { | ||
h := fileHasher() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use err = json.NewDecoder(r.Body).Decode(&v) and return if not nil , else we do the
for
and return nil , right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is done in responseErrorHandler