-
Notifications
You must be signed in to change notification settings - Fork 0
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(client): client package for indexer #31
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"io" | ||
gohttp "net/http" | ||
"net/url" | ||
|
||
"github.com/multiformats/go-multibase" | ||
"github.com/storacha/go-capabilities/pkg/assert" | ||
"github.com/storacha/go-capabilities/pkg/claim" | ||
"github.com/storacha/go-ucanto/client" | ||
"github.com/storacha/go-ucanto/core/delegation" | ||
"github.com/storacha/go-ucanto/core/invocation" | ||
"github.com/storacha/go-ucanto/core/receipt" | ||
"github.com/storacha/go-ucanto/core/result" | ||
"github.com/storacha/go-ucanto/core/result/failure" | ||
fdm "github.com/storacha/go-ucanto/core/result/failure/datamodel" | ||
unit "github.com/storacha/go-ucanto/core/result/ok" | ||
udm "github.com/storacha/go-ucanto/core/result/ok/datamodel" | ||
"github.com/storacha/go-ucanto/principal" | ||
"github.com/storacha/go-ucanto/transport/http" | ||
"github.com/storacha/go-ucanto/ucan" | ||
"github.com/storacha/indexing-service/pkg/service/queryresult" | ||
"github.com/storacha/indexing-service/pkg/types" | ||
) | ||
|
||
const claimsPath = "/claims" | ||
|
||
var ErrNoReceiptFound = errors.New("missing receipt link") | ||
|
||
type ErrFailedResponse struct { | ||
StatusCode int | ||
Body string | ||
} | ||
|
||
func errFromResponse(res *gohttp.Response) ErrFailedResponse { | ||
err := ErrFailedResponse{StatusCode: res.StatusCode} | ||
|
||
message, merr := io.ReadAll(res.Body) | ||
if merr != nil { | ||
err.Body = merr.Error() | ||
} else { | ||
err.Body = string(message) | ||
} | ||
return err | ||
} | ||
|
||
func (e ErrFailedResponse) Error() string { | ||
return fmt.Sprintf("http request failed, status: %d %s, message: %s", e.StatusCode, gohttp.StatusText(e.StatusCode), e.Body) | ||
} | ||
|
||
type Client struct { | ||
servicePrincipal ucan.Principal | ||
serviceURL url.URL | ||
connection client.Connection | ||
} | ||
|
||
func (c *Client) execute(inv invocation.Invocation) error { | ||
resp, err := client.Execute([]invocation.Invocation{inv}, c.connection) | ||
if err != nil { | ||
return fmt.Errorf("sending invocation: %w", err) | ||
} | ||
rcptlnk, ok := resp.Get(inv.Link()) | ||
if !ok { | ||
return ErrNoReceiptFound | ||
} | ||
|
||
reader, err := receipt.NewReceiptReaderFromTypes[unit.Unit, fdm.FailureModel](udm.UnitType(), fdm.FailureType()) | ||
if err != nil { | ||
return fmt.Errorf("generating receipt reader: %w", err) | ||
} | ||
|
||
rcpt, err := reader.Read(rcptlnk, resp.Blocks()) | ||
if err != nil { | ||
return fmt.Errorf("reading receipt: %w", err) | ||
} | ||
|
||
_, err = result.Unwrap(result.MapError(rcpt.Out(), failure.FromFailureModel)) | ||
return err | ||
} | ||
|
||
func (c *Client) PublishIndexClaim(ctx context.Context, issuer principal.Signer, caveats assert.IndexCaveats, options ...delegation.Option) error { | ||
inv, err := assert.Index.Invoke(issuer, c.servicePrincipal, c.servicePrincipal.DID().String(), caveats, options...) | ||
if err != nil { | ||
return fmt.Errorf("generating invocation: %w", err) | ||
} | ||
return c.execute(inv) | ||
} | ||
|
||
func (c *Client) PublishEqualsClaim(ctx context.Context, issuer principal.Signer, caveats assert.EqualsCaveats, options ...delegation.Option) error { | ||
inv, err := assert.Equals.Invoke(issuer, c.servicePrincipal, c.servicePrincipal.DID().String(), caveats, options...) | ||
if err != nil { | ||
return fmt.Errorf("generating invocation: %w", err) | ||
} | ||
return c.execute(inv) | ||
} | ||
|
||
func (c *Client) CacheClaim(ctx context.Context, issuer principal.Signer, cacheClaim delegation.Delegation, provider claim.Provider, options ...delegation.Option) error { | ||
inv, err := claim.Cache.Invoke(issuer, c.servicePrincipal, c.servicePrincipal.DID().String(), claim.CacheCaveats{ | ||
Claim: cacheClaim.Link(), | ||
Provider: provider, | ||
}, options...) | ||
if err != nil { | ||
return fmt.Errorf("generating invocation: %w", err) | ||
} | ||
|
||
for blk, err := range cacheClaim.Blocks() { | ||
if err != nil { | ||
return fmt.Errorf("reading claim blocks: %w", err) | ||
} | ||
if err := inv.Attach(blk); err != nil { | ||
return fmt.Errorf("attaching claim block: %w", err) | ||
} | ||
} | ||
|
||
return c.execute(inv) | ||
} | ||
|
||
func (c *Client) QueryClaims(ctx context.Context, query types.Query) (types.QueryResult, error) { | ||
url := c.serviceURL.JoinPath(claimsPath) | ||
q := url.Query() | ||
for _, mh := range query.Hashes { | ||
mhString, err := multibase.Encode(multibase.Base64pad, mh) | ||
if err != nil { | ||
return nil, fmt.Errorf("encoding query multihash") | ||
} | ||
q.Add("multihash", mhString) | ||
} | ||
for _, space := range query.Match.Subject { | ||
q.Add("spaces", space.String()) | ||
} | ||
url.RawQuery = q.Encode() | ||
res, err := gohttp.DefaultClient.Get(url.String()) | ||
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 probably allow this to be configured... |
||
if err != nil { | ||
return nil, fmt.Errorf("sending query to server: %w", err) | ||
} | ||
if res.StatusCode < 200 || res.StatusCode > 299 { | ||
return nil, errFromResponse(res) | ||
} | ||
return queryresult.Extract(res.Body) | ||
} | ||
|
||
func New(servicePrincipal ucan.Principal, serviceURL url.URL) (*Client, error) { | ||
channel := http.NewHTTPChannel(serviceURL.JoinPath(claimsPath)) | ||
conn, err := client.NewConnection(servicePrincipal, channel) | ||
if err != nil { | ||
return nil, fmt.Errorf("creating connection: %w", err) | ||
} | ||
return &Client{servicePrincipal, serviceURL, conn}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Need a constructor