Skip to content

Commit

Permalink
Drop support for FindBatch API
Browse files Browse the repository at this point in the history
- Add FindBatch convenience fuinction
- Define common Finder interface to replace Interface
  • Loading branch information
gammazero committed Sep 19, 2023
1 parent d307043 commit 7d160df
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 88 deletions.
19 changes: 1 addition & 18 deletions find/client/client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package client

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand Down Expand Up @@ -30,7 +29,7 @@ type Client struct {
}

// Client must implement Interface.
var _ Interface = (*Client)(nil)
var _ Finder = (*Client)(nil)

// New creates a new find HTTP client.
func New(baseURL string, options ...Option) (*Client, error) {
Expand Down Expand Up @@ -67,22 +66,6 @@ func (c *Client) Find(ctx context.Context, m multihash.Multihash) (*model.FindRe
return c.sendRequest(req)
}

// FindBatch looks up content entries for a batch of multihashes
func (c *Client) FindBatch(ctx context.Context, mhs []multihash.Multihash) (*model.FindResponse, error) {
if len(mhs) == 0 {
return &model.FindResponse{}, nil
}
data, err := model.MarshalFindRequest(&model.FindRequest{Multihashes: mhs})
if err != nil {
return nil, err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.findURL.String(), bytes.NewBuffer(data))
if err != nil {
return nil, err
}
return c.sendRequest(req)
}

func (c *Client) ListProviders(ctx context.Context) ([]*model.ProviderInfo, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.providersURL.String(), nil)
if err != nil {
Expand Down
40 changes: 28 additions & 12 deletions find/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,40 @@ package client

import (
"context"
"errors"
"net/http"

"github.com/ipni/go-libipni/apierror"
"github.com/ipni/go-libipni/find/model"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multihash"
)

// Interface is the interface implemented by all find clients.
type Interface interface {
// Finder is the interface implemented by all find clients.
type Finder interface {
// Find queries for provider content records for a single multihash.
Find(context.Context, multihash.Multihash) (*model.FindResponse, error)
// FindBatch queries for provider content records for a batch of multihashes.
FindBatch(context.Context, []multihash.Multihash) (*model.FindResponse, error)

// GetProvider gets information about the provider identified by peer.ID.
GetProvider(context.Context, peer.ID) (*model.ProviderInfo, error)
// ListPrividers gets information about all providers known to the indexer.
ListProviders(ctx context.Context) ([]*model.ProviderInfo, error)
}

// GetStats get statistics for indexer.
GetStats(context.Context) (*model.Stats, error)
// FindBatch is a convenience function to lookup results for multiple
// multihashes. This works with either the Client or DHashClient. If no results
// are found, then an error is not returned and there are no results in the
// response.
func FindBatch(ctx context.Context, finder Finder, mhs []multihash.Multihash) (*model.FindResponse, error) {
var resp *model.FindResponse
for i := range mhs {
r, err := finder.Find(ctx, mhs[i])
if err != nil {
var ae *apierror.Error
if errors.As(err, &ae) && ae.Status() == http.StatusNotFound {
continue
}
return nil, err
}
if resp == nil {
resp = r
} else {
resp.MultihashResults = append(resp.MultihashResults, r.MultihashResults...)
}
}
return resp, nil
}
22 changes: 0 additions & 22 deletions find/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
"github.com/multiformats/go-multihash"
)

// FindRequest is the client request send by end user clients
type FindRequest struct {
Multihashes []multihash.Multihash
}

// ProviderResult is a one of possibly multiple results when looking up a
// provider of indexed context.
type ProviderResult struct {
Expand Down Expand Up @@ -61,23 +56,6 @@ func (pr ProviderResult) Equal(other ProviderResult) bool {
return true
}

// MarshalFindRequest serializes the request. Currently uses JSON, but could
// use anything else.
//
// NOTE: Consider using other serialization formats? We could maybe use IPLD
// schemas instead of structs for requests and response so we have any codec by
// design.
func MarshalFindRequest(r *FindRequest) ([]byte, error) {
return json.Marshal(r)
}

// UnmarshalFindRequest de-serializes the request.
func UnmarshalFindRequest(b []byte) (*FindRequest, error) {
r := &FindRequest{}
err := json.Unmarshal(b, r)
return r, err
}

// MarshalFindResponse serializes a find response.
func MarshalFindResponse(r *FindResponse) ([]byte, error) {
return json.Marshal(r)
Expand Down
47 changes: 11 additions & 36 deletions find/model/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,11 @@ import (

func TestMarshal(t *testing.T) {
// Generate some multihashes and populate indexer
mhs := test.RandomMultihashes(3)
p, _ := peer.Decode("12D3KooWKRyzVWW6ChFjQjK4miCty85Niy48tpPV95XdKu1BcvMA")
ctxID := []byte("test-context-id")
metadata := []byte("test-metadata")

// Masrhal request and check e2e
t.Log("e2e marshalling request")
req := &model.FindRequest{Multihashes: mhs}
b, err := model.MarshalFindRequest(req)
if err != nil {
t.Fatal(err)
}

r, err := model.UnmarshalFindRequest(b)
if err != nil {
t.Fatal(err)
}
if !equalMultihashes(r.Multihashes, mhs) {
t.Fatal("Request marshal/unmarshal not correct")
}

m1, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
if err != nil {
t.Fatal(err)
}

m2, err := multiaddr.NewMultiaddr("/dns4/ipni.io/tcp/443/https/httpath/http-cid-data")
if err != nil {
t.Fatal(err)
}

// Masrhal response and check e2e
t.Log("e2e marshalling response")
resp := &model.FindResponse{
MultihashResults: []model.MultihashResult{},
}
ctxID := []byte("test-context-id")
p, _ := peer.Decode("12D3KooWKRyzVWW6ChFjQjK4miCty85Niy48tpPV95XdKu1BcvMA")
m1, _ := multiaddr.NewMultiaddr("/ip4/127.0.0.1/udp/1234")
m2, _ := multiaddr.NewMultiaddr("/dns4/ipni.io/tcp/443/https/httpath/http-cid-data")

providerResult := model.ProviderResult{
ContextID: ctxID,
Expand All @@ -59,14 +28,20 @@ func TestMarshal(t *testing.T) {
},
}

// Masrhal response and check e2e
resp := &model.FindResponse{
MultihashResults: []model.MultihashResult{},
}

mhs := test.RandomMultihashes(3)
for i := range mhs {
resp.MultihashResults = append(resp.MultihashResults, model.MultihashResult{
Multihash: mhs[i],
ProviderResults: []model.ProviderResult{providerResult},
})
}

b, err = model.MarshalFindResponse(resp)
b, err := model.MarshalFindResponse(resp)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 7d160df

Please sign in to comment.