Skip to content

Commit

Permalink
Return empty response for not found to preserve compatability with cl…
Browse files Browse the repository at this point in the history
…ients that expect non-nil response
  • Loading branch information
gammazero committed Sep 23, 2023
1 parent 4bf4348 commit bdfc4da
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions find/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func New(baseURL string, options ...Option) (*Client, error) {
}, nil
}

// Find looks up content entries by multihash. If no results are found then a
// nil response without error is returned.
// Find looks up content entries by multihash. If no results are found then an
// empty response without error is returned.
func (c *Client) Find(ctx context.Context, m multihash.Multihash) (*model.FindResponse, error) {
u := c.findURL.JoinPath(m.B58String())
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
Expand All @@ -72,7 +72,7 @@ func (c *Client) Find(ctx context.Context, m multihash.Multihash) (*model.FindRe
// Handle failed requests
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
return nil, nil
return &model.FindResponse{}, nil
}
return nil, fmt.Errorf("find query failed: %v", http.StatusText(resp.StatusCode))
}
Expand Down
4 changes: 2 additions & 2 deletions find/client/dhash_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (c *DHashClient) PCache() *pcache.ProviderCache {

// Find launches FindAsync in a separate go routine and assembles the result
// into FindResponse as if it was a synchronous invocation. If no results are
// found then a nil response without error is returned.
// found then an empty response without error is returned.
func (c *DHashClient) Find(ctx context.Context, mh multihash.Multihash) (*model.FindResponse, error) {
resChan := make(chan model.ProviderResult)
errChan := make(chan error, 1)
Expand All @@ -117,7 +117,7 @@ func (c *DHashClient) Find(ctx context.Context, mh multihash.Multihash) (*model.
return nil, err
}
if len(providerResults) == 0 {
return nil, nil
return &model.FindResponse{}, nil
}

return &model.FindResponse{
Expand Down
7 changes: 5 additions & 2 deletions find/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import (
// Finder is the interface implemented by all find clients.
type Finder interface {
// Find queries for provider content records for a single multihash. If no
// results are found then a nil response without error is returned.
// results are found then an empty response without error is returned.
Find(context.Context, multihash.Multihash) (*model.FindResponse, 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 a nil response without error is returned.
// are found then an empty response without error is returned.
func FindBatch(ctx context.Context, finder Finder, mhs []multihash.Multihash) (*model.FindResponse, error) {
var resp *model.FindResponse
for i := range mhs {
Expand All @@ -41,5 +41,8 @@ func FindBatch(ctx context.Context, finder Finder, mhs []multihash.Multihash) (*
resp.MultihashResults = append(resp.MultihashResults, r.MultihashResults...)
}
}
if resp == nil {
resp = &model.FindResponse{}
}
return resp, nil
}

0 comments on commit bdfc4da

Please sign in to comment.