Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

feat: LsBatchSync to fetch single batch of results #6

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,30 @@ func (c *Client) LsSync(ctx context.Context, opts ...LsOption) ([]PinStatusGette
return res, <-errCh
}

// Manual version of Ls that returns a single batch of results and int with total count
func (c *Client) LsBatchSync(ctx context.Context, opts ...LsOption) ([]PinStatusGetter, int, error) {
var res []PinStatusGetter

settings := new(lsSettings)
for _, o := range opts {
if err := o(settings); err != nil {
return nil, 0, err
}
}

pinRes, err := c.lsInternal(ctx, settings)
if err != nil {
return nil, 0, err
}

results := pinRes.GetResults()
for _, r := range results {
res = append(res, &pinStatusObject{r})
}

return res, int(pinRes.Count), nil
}

func (c *Client) lsInternal(ctx context.Context, settings *lsSettings) (pinResults, error) {
getter := c.client.PinsApi.PinsGet(ctx)
if len(settings.cids) > 0 {
Expand Down Expand Up @@ -257,7 +281,7 @@ func (pinAddOpts) WithName(name string) AddOption {
}
}

func (pinLsOpts) WithOrigins(origins ...multiaddr.Multiaddr) AddOption {
func (pinAddOpts) WithOrigins(origins ...multiaddr.Multiaddr) AddOption {
return func(options *addSettings) error {
for _, o := range origins {
options.origins = append(options.origins, o.String())
Expand Down Expand Up @@ -326,7 +350,7 @@ func (c *Client) DeleteByID(ctx context.Context, pinID string) error {
return nil
}

func (c *Client) Modify(ctx context.Context, pinID string, cid cid.Cid, opts ...AddOption) (PinStatusGetter, error) {
func (c *Client) Replace(ctx context.Context, pinID string, cid cid.Cid, opts ...AddOption) (PinStatusGetter, error) {
settings := new(addSettings)
for _, o := range opts {
if err := o(settings); err != nil {
Expand Down Expand Up @@ -371,13 +395,13 @@ func httperr(resp *http.Response, e error) error {
if ok {
ferr, ok := oerr.Model().(openapi.Failure)
if ok {
return errors.Wrapf(e,"statusCode: %d, reason : %q, details : %q", resp.StatusCode, ferr.Error.GetReason(), ferr.Error.GetDetails())
return errors.Wrapf(e, "reason: %q, details: %q", ferr.Error.GetReason(), ferr.Error.GetDetails())
}
}

if resp == nil {
return errors.Wrapf(e,"empty response from remote pinning service")
return errors.Wrapf(e, "empty response from remote pinning service")
}

return errors.Wrapf(e, "remote pinning service error. statusCode: %d", resp.StatusCode)
return errors.Wrapf(e, "remote pinning service returned http error %d", resp.StatusCode)
}