Client

Client is a Sponsorkit client for one target set.

On this page

Client is a Sponsorkit client for one target set. It is safe for concurrent use.

type Client struct {
	// contains filtered or unexported fields
}

Functions#

New#

func New(opts Options) (*Client, error)

New creates a Client. It returns an invalid_config *Error for missing or malformed targets, an unknown mock preset or other invalid options. It does no I/O.

View source

Methods#

CheckoutURL#

func (c *Client) CheckoutURL(opts CheckoutOptions) (string, error)

CheckoutURL returns a link to the <SiteBaseURL>/checkout/ page without sign-in. It is synchronous and never touches the network or disk, which suits nags and READMEs; a checkout started this way cannot be detected.

It returns an invalid_config *Error when opts names a provider that is not configured, or an invalid frequency, amount or currency (PROTOCOL §7.1 item 16).

View source

Disabled#

func (c *Client) Disabled() bool

Disabled reports whether SPONSORKIT_DISABLE is set for this client.

View source

GetFinancialContribution#

func (c *Client) GetFinancialContribution(ctx context.Context, opts ...GetOption) (*Result, error)

GetFinancialContribution reports what the current user has contributed to the targets. It never returns an error for network or server problems: it serves the cache, or a stale signed-in result when nothing is cached. The only error is invalid_config from the dev-time target existence check.

View source

IsMock#

func (c *Client) IsMock() bool

IsMock reports whether the client is in mock mode.

View source

IsSignedIn#

func (c *Client) IsSignedIn() bool

IsSignedIn reports whether a session token is stored. It makes no request.

View source

Nag#

func (c *Client) Nag(opts NagOptions) *Nag

Nag returns a nag helper:

nag := sk.Nag(sponsorkit.NagOptions{ID: "my-cli", Every: 10, OrEvery: 7 * 24 * time.Hour})
if nag.Due(ctx, func(r *sponsorkit.Result) bool { return len(r.Active()) == 0 }) {
	link, _ := sk.CheckoutURL(sponsorkit.CheckoutOptions{Frequency: sponsorkit.Monthly})
	fmt.Fprintln(os.Stderr, "Enjoying my-cli? Support it:", link)
	nag.Shown()
}

View source

Example#

package main

import (
	"context"
	"fmt"
	"log"
	"os"
	"time"

	sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"
)

func main() {
	ctx := context.Background()
	sk, err := sponsorkit.New(sponsorkit.Options{GitHub: "octocat", AppName: "my-cli"})
	if err != nil {
		log.Fatal(err)
	}
	nag := sk.Nag(sponsorkit.NagOptions{ID: "my-cli", Every: 10, OrEvery: 7 * 24 * time.Hour})
	if nag.Due(ctx, func(fc *sponsorkit.Result) bool { return len(fc.Active()) == 0 }) {
		link, err := sk.CheckoutURL(sponsorkit.CheckoutOptions{Frequency: sponsorkit.Monthly})
		if err != nil {
			log.Fatal(err)
		}
		fmt.Fprintln(os.Stderr, "Enjoying my-cli? Support it:", link)
		nag.Shown()
	}
}

OpenCheckout#

func (c *Client) OpenCheckout(ctx context.Context, opts CheckoutOptions) (*CheckoutResult, error)

OpenCheckout sends the user to contribute and waits for the result: it signs in first if needed (so the new contribution can be detected), opens <SiteBaseURL>/checkout/ in the browser, then polls with a zero cache TTL until a new or changed contribution appears. CheckoutOptions.Timeout bounds only the polling phase; the sign-in phase is bounded by Options.SignInTimeout. Cancelling ctx in either phase, or the polling timeout, resolves with CheckoutUnknown, not an error.

Errors: disabled, invalid_config, and the SignIn errors (including timeout when Options.SignInTimeout elapses) when a sign-in was needed and failed.

View source

Example#

package main

import (
	"context"
	"fmt"
	"log"
	"time"

	sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
	defer cancel()
	sk, err := sponsorkit.New(sponsorkit.Options{GitHub: "octocat", Mock: sponsorkit.MockPreset("none")})
	if err != nil {
		log.Fatal(err)
	}
	r, err := sk.OpenCheckout(ctx, sponsorkit.CheckoutOptions{Amount: 500, Frequency: sponsorkit.Monthly})
	if err != nil {
		log.Fatal(err)
	}
	if r.Status == sponsorkit.CheckoutContributed {
		fmt.Println("Thank you!", r.Contribution.Amount.Value, r.Contribution.Amount.Currency)
	}
}

Output:

Thank you! 500 USD

SessionToken#

func (c *Client) SessionToken() string

SessionToken returns the stored session token (sks_…) so it can be forwarded to a backend that calls VerifySession. It returns "" when signed out, disabled or in mock mode.

View source

SignIn#

func (c *Client) SignIn(ctx context.Context, opts ...SignInOptions) (*Result, error)

SignIn signs the user in, or adds an identity when already signed in, and returns the fresh result. It never prompts implicitly; call it from a user action. Pass at most one SignInOptions.

Cancel ctx to abandon the sign-in; Options.SignInTimeout (default 5 minutes) bounds how long it waits for the user.

Errors: disabled, invalid_config (also when the session cannot be written to storage), access_denied, invalid_state and provider_error (delivered by the sign-in page), invalid_code (the API rejected the code), cancelled (ctx was cancelled or the user abandoned the paste-back prompt), timeout (SignInTimeout elapsed) and network_error (Sponsorkit could not be reached or answered 5xx, 429 or an unexpected response; see Error.RetryAfter).

View source

Example#

package main

import (
	"context"
	"errors"
	"fmt"
	"log"
	"os"

	sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"
)

func main() {
	ctx := context.Background()
	sk, err := sponsorkit.New(sponsorkit.Options{GitHub: "octocat", AppName: "my-cli"})
	if err != nil {
		log.Fatal(err)
	}
	if !sk.IsSignedIn() {
		if _, err := sk.SignIn(ctx); err != nil {
			var skErr *sponsorkit.Error
			if errors.As(err, &skErr) && skErr.Code == sponsorkit.CodeAccessDenied {
				fmt.Fprintln(os.Stderr, "Sign-in cancelled.")
				return
			}
			log.Fatal(err)
		}
	}
}

SignOut#

func (c *Client) SignOut(_ context.Context) error

SignOut deletes the session token and cache locally; there is no server-side sign-out. Nag state is kept. It returns invalid_config when the storage cannot be written.

View source

Targets#

func (c *Client) Targets() []string

Targets returns the canonical target strings, e.g. ["github:octocat"].

View source

TargetsParam#

func (c *Client) TargetsParam() string

TargetsParam returns the canonical target param used in URLs.

View source