Add StatusCids() to Restapi client

This commit is contained in:
Hector Sanjuan 2022-03-01 13:13:52 +01:00
parent a046af811c
commit a1b6dabbb0
4 changed files with 56 additions and 1 deletions

View File

@ -84,6 +84,8 @@ type Client interface {
// the information affects only the current peer, otherwise the information // the information affects only the current peer, otherwise the information
// is fetched from all cluster peers. // is fetched from all cluster peers.
Status(ctx context.Context, ci cid.Cid, local bool) (*api.GlobalPinInfo, error) Status(ctx context.Context, ci cid.Cid, local bool) (*api.GlobalPinInfo, error)
// StatusCids status information for the requested CIDs.
StatusCids(ctx context.Context, cids []cid.Cid, local bool) ([]*api.GlobalPinInfo, error)
// StatusAll gathers Status() for all tracked items. // StatusAll gathers Status() for all tracked items.
StatusAll(ctx context.Context, filter api.TrackerStatus, local bool) ([]*api.GlobalPinInfo, error) StatusAll(ctx context.Context, filter api.TrackerStatus, local bool) ([]*api.GlobalPinInfo, error)

View File

@ -253,6 +253,21 @@ func (lc *loadBalancingClient) Status(ctx context.Context, ci cid.Cid, local boo
return pinInfo, err return pinInfo, err
} }
// StatusCids returns Status() information for the given Cids. If local is
// true, the information affects only the current peer, otherwise the
// information is fetched from all cluster peers.
func (lc *loadBalancingClient) StatusCids(ctx context.Context, cids []cid.Cid, local bool) ([]*api.GlobalPinInfo, error) {
var pinInfos []*api.GlobalPinInfo
call := func(c Client) error {
var err error
pinInfos, err = c.StatusCids(ctx, cids, local)
return err
}
err := lc.retry(0, call)
return pinInfos, err
}
// StatusAll gathers Status() for all tracked items. If a filter is // StatusAll gathers Status() for all tracked items. If a filter is
// provided, only entries matching the given filter statuses // provided, only entries matching the given filter statuses
// will be returned. A filter can be built by merging TrackerStatuses with // will be returned. A filter can be built by merging TrackerStatuses with

View File

@ -220,12 +220,23 @@ func (c *defaultClient) Status(ctx context.Context, ci cid.Cid, local bool) (*ap
return &gpi, err return &gpi, err
} }
// StatusCids returns Status() information for the given Cids. If local is
// true, the information affects only the current peer, otherwise the
// information is fetched from all cluster peers.
func (c *defaultClient) StatusCids(ctx context.Context, cids []cid.Cid, local bool) ([]*api.GlobalPinInfo, error) {
return c.statusAllWithCids(ctx, api.TrackerStatusUndefined, cids, local)
}
// StatusAll gathers Status() for all tracked items. If a filter is // StatusAll gathers Status() for all tracked items. If a filter is
// provided, only entries matching the given filter statuses // provided, only entries matching the given filter statuses
// will be returned. A filter can be built by merging TrackerStatuses with // will be returned. A filter can be built by merging TrackerStatuses with
// a bitwise OR operation (st1 | st2 | ...). A "0" filter value (or // a bitwise OR operation (st1 | st2 | ...). A "0" filter value (or
// api.TrackerStatusUndefined), means all. // api.TrackerStatusUndefined), means all.
func (c *defaultClient) StatusAll(ctx context.Context, filter api.TrackerStatus, local bool) ([]*api.GlobalPinInfo, error) { func (c *defaultClient) StatusAll(ctx context.Context, filter api.TrackerStatus, local bool) ([]*api.GlobalPinInfo, error) {
return c.statusAllWithCids(ctx, filter, nil, local)
}
func (c *defaultClient) statusAllWithCids(ctx context.Context, filter api.TrackerStatus, cids []cid.Cid, local bool) ([]*api.GlobalPinInfo, error) {
ctx, span := trace.StartSpan(ctx, "client/StatusAll") ctx, span := trace.StartSpan(ctx, "client/StatusAll")
defer span.End() defer span.End()
@ -239,10 +250,16 @@ func (c *defaultClient) StatusAll(ctx context.Context, filter api.TrackerStatus,
} }
} }
var cidsStr []string
for _, c := range cids {
cidsStr = append(cidsStr, c.String())
}
err := c.do( err := c.do(
ctx, ctx,
"GET", "GET",
fmt.Sprintf("/pins?local=%t&filter=%s", local, url.QueryEscape(filterStr)), fmt.Sprintf("/pins?local=%t&filter=%s&cids=%s",
local, url.QueryEscape(filterStr), strings.Join(cidsStr, ",")),
nil, nil,
nil, nil,
&gpis, &gpis,

View File

@ -327,6 +327,27 @@ func TestStatus(t *testing.T) {
testClients(t, api, testF) testClients(t, api, testF)
} }
func TestStatusCids(t *testing.T) {
ctx := context.Background()
api := testAPI(t)
defer shutdown(api)
testF := func(t *testing.T, c Client) {
pins, err := c.StatusCids(ctx, []cid.Cid{test.Cid1}, false)
if err != nil {
t.Fatal(err)
}
if len(pins) != 1 {
t.Fatal("wrong number of pins returned")
}
if !pins[0].Cid.Equals(test.Cid1) {
t.Error("should be same pin")
}
}
testClients(t, api, testF)
}
func TestStatusAll(t *testing.T) { func TestStatusAll(t *testing.T) {
ctx := context.Background() ctx := context.Background()
api := testAPI(t) api := testAPI(t)