Rust API reference
Every public type, function and constant of the sponsorkit crate 1.0.0, generated from its source and doc comments.
On this page
- Guarantees
- Cancellation
- Errors
- Runtime
- Modules
- Structs
- Enums
- Type aliases
- Functions and constants
- Re-exports
- Functions
- base64url
- code_challenge
- generate_verifier
- mock_result
- parse_iso_duration
- Constants
- DEFAULT_API_BASE_URL
- DEFAULT_CACHE_TTL
- DEFAULT_CHECKOUT_POLL_INTERVAL
- DEFAULT_CHECKOUT_TIMEOUT
- DEFAULT_REQUEST_TIMEOUT
- DEFAULT_SIGN_IN_TIMEOUT
- DEFAULT_SITE_BASE_URL
- MAX_RETRY_AFTER
- MIN_CACHE_TTL
- MOCK_PRESETS_JSON
API reference for the sponsorkit crate, version 1.0.0. It's generated from the crate's source and doc comments; for a guided tour, start with Sponsorkit for Rust.
Pre-release
The crate hasn't been published to crates.io yet. Until it is, depend on the repository:
[dependencies]
sponsorkit = { git = "https://github.com/ffMathy/sponsorkit", branch = "main" }See installing the Rust SDK for a local path and the Cargo.toml line to use once the crate is released.
Build GitHub Sponsors and Open Collective contributions into your Rust app or CLI. No account, no API key: name the account(s) you receive contributions on, ask what the current user contributes, and send them to contribute.
use sponsorkit::{CheckoutOptions, Frequency, GetOptions, Options, Sponsorkit};
let sk = Sponsorkit::new(Options {
github: Some("octocat".into()),
open_collective: Some("sponsorkit".into()),
app_name: Some("my-cli".into()),
..Default::default()
})?;
let fc = sk.get_financial_contribution(GetOptions::default()).await?;
let supports = fc.active().iter().any(|c| c.amount.currency == "USD" && c.amount.value >= 500);
if fc.signed_in && !supports {
println!("Support my-cli: {}", sk.checkout_url(&CheckoutOptions::default())?);
}
// Interactive: sign in (if needed), open the checkout, wait for the contribution.
let outcome = sk
.open_checkout(CheckoutOptions { frequency: Some(Frequency::Monthly), amount: Some(500), ..Default::default() })
.await?;Guarantees#
Sponsorkit::get_financial_contributionnever fails because Sponsorkit, a provider or the network is down; it serves the local cache instead . Its only error isinvalid_configfrom the dev-time check.SPONSORKIT_DISABLE=1turns the SDK into a no-op without network or disk access.- Mock mode (
Options::mockorSPONSORKIT_MOCK) gives deterministic data for tests.
Cancellation#
Sponsorkit::sign_in accepts a CancellationToken in
SignInOptions::cancel; cancelling fails with ErrorCode::Cancelled.
Sponsorkit::open_checkout accepts one in CheckoutOptions::cancel;
cancelling (in the sign-in or the polling phase) resolves
CheckoutOutcome::Unknown. Dropping either future (e.g. through
tokio::time::timeout or select!) also stops it, without a result.
Errors#
Every error is an Error whose Error::code is one of the codes
shared by all Sponsorkit SDKs: invalid_config,
disabled, popup_blocked, access_denied, invalid_state,
provider_error, invalid_code, cancelled, timeout,
network_error (with Error::retry_after), session_invalid and
targets_mismatch.
Runtime#
The SDK needs a Tokio runtime (current-thread or multi-thread). Local storage uses small synchronous file operations.
Modules#
| Name | Description |
|---|---|
server | Backend verification: check a session token your frontend sent you. |
Structs#
| Name | Description |
|---|---|
CheckoutOptions | Options for Sponsorkit::open_checkout and Sponsorkit::checkout_url. |
Contribution | One sponsorship (GitHub) or order (Open Collective), normalized. |
ContributionResult | The result of crate::Sponsorkit::get_financial_contribution. |
DueOptions | Options for Nag::due_with. |
Error | The error type of this crate (SponsorkitError in the other SDKs). |
GetOptions | Options for Sponsorkit::get_financial_contribution. |
Identity | A provider identity of the signed-in supporter. |
Money | An amount of money in minor units (cents). |
Nag | A nag created by Sponsorkit::nag. |
NagOptions | Configuration for Sponsorkit::nag. |
Options | Configuration for Sponsorkit::new. |
SignInOptions | Options for Sponsorkit::sign_in. |
SponsorEntity | Who actually pays. |
Sponsorkit | A Sponsorkit client for one target set. |
Supporter | The signed-in supporter. |
Target | One normalized target. |
TargetSet | A canonical target set: deduplicated, sorted by string form, non-empty, at most one target per provider. |
Targets | The accounts to check, as given by the maintainer ({ github, open_collective }). |
Tier | A tier reference on a contribution. |
Enums#
| Name | Description |
|---|---|
CheckoutOutcome | The outcome of crate::Sponsorkit::open_checkout. |
ErrorCode | A stable, machine-readable error code. |
Flag | Contribution flags. |
Frequency | How often a contribution is charged. |
Kind | Recurring or one-time. |
Mock | Mock data for crate::Options::mock. |
MockPreset | A built-in mock preset. |
Provider | A contribution provider. |
SignInMethod | How sign_in (and open_checkout when signed out) gets the sign-in code. |
SponsorType | The type of a sponsor entity. |
Status | Contribution status. |
Via | Whether the supporter paid directly or through an organization. |
Type aliases#
| Name | Description |
|---|---|
BoxFuture | A boxed, sendable future, used by the hook types. |
OpenBrowserHook | Replacement for opening the system browser: receives the URL to open. |
PromptForCodeHook | Replacement for reading the paste-back code from stdin: receives the <site_base_url>/connect/?…&delivery=paste URL the user has to open and returns the code. |
Result | Shorthand for std::result::Result<T, sponsorkit::Error>. |
Functions and constants#
| Name | Kind | Description |
|---|---|---|
DEFAULT_API_BASE_URL | constant | The default API origin, used for every API call (/api/v1/…). |
DEFAULT_CACHE_TTL | constant | The default cache TTL (1 hour). |
DEFAULT_CHECKOUT_POLL_INTERVAL | constant | The default interval at which open_checkout polls for a new contribution (60 s). |
DEFAULT_CHECKOUT_TIMEOUT | constant | The default time open_checkout waits for a contribution (10 minutes). |
DEFAULT_REQUEST_TIMEOUT | constant | The default Options::request_timeout for every HTTP request (30 s). |
DEFAULT_SIGN_IN_TIMEOUT | constant | The default Options::sign_in_timeout (300 s). |
DEFAULT_SITE_BASE_URL | constant | The default site base URL (origin plus base path), used for every page URL: /connect/, /checkout/ and Sponsorkit::checkout_url. |
MAX_RETRY_AFTER | constant | The longest Retry-After the SDK honours (24 h); longer values are clamped. |
MIN_CACHE_TTL | constant | The minimum cache TTL accepted by Options::cache_ttl (60 s). |
MOCK_PRESETS_JSON | constant | The embedded preset file (identical to sdks/conformance/fixtures/mock-presets.json). |
base64url | function | Encodes bytes as base64url without padding. |
code_challenge | function | code_challenge = base64url(sha256(ascii(code_verifier))), without padding. |
generate_verifier | function | Generates a fresh code verifier: 32 random bytes, base64url (43 characters). |
mock_result | function | Expands a mock configuration into a result (rules 1–4 of mock-presets.json). |
parse_iso_duration | function | Parses an ISO-8601 duration such as P7D, PT12H, P1W or P1DT2H30M. |
Re-exports#
| Name | Re-exported from |
|---|---|
CancellationToken | tokio_util::sync::CancellationToken |
Functions#
base64url#
pub fn base64url(bytes: &[u8]) -> StringEncodes bytes as base64url without padding.
| Parameter | Type |
|---|---|
bytes | &[u8] |
Returns: String
code_challenge#
pub fn code_challenge(verifier: &str) -> Stringcode_challenge = base64url(sha256(ascii(code_verifier))), without padding.
| Parameter | Type |
|---|---|
verifier | &str |
Returns: String
generate_verifier#
pub fn generate_verifier() -> Result<String>Generates a fresh code verifier: 32 random bytes, base64url (43 characters).
Returns: Result<String> (see Result)
mock_result#
pub fn mock_result(targets: &TargetSet, mock: &Mock) -> ContributionResultExpands a mock configuration into a result (rules 1–4 of mock-presets.json).
| Parameter | Type |
|---|---|
targets | &TargetSet |
mock | &Mock |
Returns: ContributionResult
parse_iso_duration#
pub fn parse_iso_duration(s: &str) -> Result<Duration>Parses an ISO-8601 duration such as P7D, PT12H, P1W or P1DT2H30M.
Years and months are not supported (they have no fixed length); use days.
Returns an invalid_config error for anything else.
| Parameter | Type |
|---|---|
s | &str |
Returns: Result<Duration> (see Result)
Constants#
DEFAULT_API_BASE_URL#
pub const DEFAULT_API_BASE_URL: &str = "https://api.sponsorkit.io";The default API origin, used for every API call (/api/v1/…).
DEFAULT_CACHE_TTL#
pub const DEFAULT_CACHE_TTL: Duration = Duration::from_secs(3600);The default cache TTL (1 hour).
DEFAULT_CHECKOUT_POLL_INTERVAL#
pub const DEFAULT_CHECKOUT_POLL_INTERVAL: Duration = Duration::from_secs(60);The default interval at which open_checkout polls for a new contribution (60 s).
DEFAULT_CHECKOUT_TIMEOUT#
pub const DEFAULT_CHECKOUT_TIMEOUT: Duration = Duration::from_secs(600);The default time open_checkout waits for a contribution (10 minutes).
DEFAULT_REQUEST_TIMEOUT#
pub const DEFAULT_REQUEST_TIMEOUT: Duration = Duration::from_secs(30);The default Options::request_timeout for every HTTP request (30 s).
DEFAULT_SIGN_IN_TIMEOUT#
pub const DEFAULT_SIGN_IN_TIMEOUT: Duration = Duration::from_secs(300);The default Options::sign_in_timeout (300 s).
DEFAULT_SITE_BASE_URL#
pub const DEFAULT_SITE_BASE_URL: &str = "https://ffmathy.github.io/sponsorkit";The default site base URL (origin plus base path), used for every page
URL: /connect/, /checkout/ and Sponsorkit::checkout_url.
MAX_RETRY_AFTER#
pub const MAX_RETRY_AFTER: Duration = Duration::from_secs(86_400);The longest Retry-After the SDK honours (24 h); longer values are clamped.
MIN_CACHE_TTL#
pub const MIN_CACHE_TTL: Duration = Duration::from_secs(60);The minimum cache TTL accepted by Options::cache_ttl (60 s).
MOCK_PRESETS_JSON#
pub const MOCK_PRESETS_JSON: &str = include_str!("mock-presets.json");The embedded preset file (identical to sdks/conformance/fixtures/mock-presets.json).