Files
ucan/toolkit/client/requester.go

54 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-12-09 16:53:44 +01:00
package client
import (
"context"
"iter"
"time"
2024-12-09 16:53:44 +01:00
"code.sonr.org/go/did-it"
"github.com/avast/retry-go/v4"
2025-08-05 12:11:20 +02:00
"code.sonr.org/go/ucan/pkg/command"
"code.sonr.org/go/ucan/token/delegation"
2024-12-09 16:53:44 +01:00
)
type DelegationRequester interface {
// RequestDelegation retrieve a delegation or chain of delegation for the given parameters.
// - cmd: the command to execute
// - audience: the DID of the client, also the issuer of the invocation token
// - subject: the DID of the resource to operate on, also the subject (or audience if defined) of the invocation token
2025-01-16 15:16:01 +01:00
// The returned delegations MUST be ordered starting from the leaf (the one matching the invocation) to the root
// (the one given by the service).
// Note: you can read it as "(audience) wants to do (cmd) on (subject)".
2024-12-09 16:53:44 +01:00
// Note: the returned delegation(s) don't have to match exactly the parameters, as long as they allow them.
RequestDelegation(ctx context.Context, audience did.DID, cmd command.Command, subject did.DID) (iter.Seq2[*delegation.Bundle, error], error)
}
var _ DelegationRequester = &withRetry{}
type withRetry struct {
requester DelegationRequester
initialDelay time.Duration
maxAttempts uint
}
// RequesterWithRetry wraps a DelegationRequester to perform exponential backoff,
// with an initial delay and a maximum attempt count.
func RequesterWithRetry(requester DelegationRequester, initialDelay time.Duration, maxAttempt uint) DelegationRequester {
return &withRetry{
requester: requester,
initialDelay: initialDelay,
maxAttempts: maxAttempt,
}
}
func (w withRetry) RequestDelegation(ctx context.Context, audience did.DID, cmd command.Command, subject did.DID) (iter.Seq2[*delegation.Bundle, error], error) {
return retry.DoWithData(func() (iter.Seq2[*delegation.Bundle, error], error) {
return w.requester.RequestDelegation(ctx, audience, cmd, subject)
},
retry.Context(ctx),
retry.Delay(w.initialDelay),
retry.Attempts(w.maxAttempts),
)
2024-12-09 16:53:44 +01:00
}