Result
Result is what GetFinancialContribution returns.
On this page
Result is what GetFinancialContribution returns. It serializes to exactly the protocol JSON shape.
type Result struct {
SignedIn bool `json:"signedIn"`
Disabled bool `json:"disabled"`
Mock bool `json:"mock"`
Targets []string `json:"targets"`
Supporter *Supporter `json:"supporter"`
Contributions []Contribution `json:"contributions"`
FetchedAt *string `json:"fetchedAt"`
Stale bool `json:"stale"`
FromCache bool `json:"fromCache"`
Unavailable []Provider `json:"unavailable,omitempty"`
}Fields#
SignedIn#
SignedIn bool `json:"signedIn"`SignedIn is true when a session token is stored. When false, no request was made.
Disabled#
Disabled bool `json:"disabled"`Disabled is true when SPONSORKIT_DISABLE is set: no network, no disk, always signed out.
Mock#
Mock bool `json:"mock"`Mock is true when the result comes from mock mode.
Targets#
Targets []string `json:"targets"`Targets are the canonical target strings, e.g. ["github:octocat"].
Supporter#
Supporter *Supporter `json:"supporter"`Supporter is the signed-in user, or nil when signed out.
Contributions#
Contributions []Contribution `json:"contributions"`Contributions are the supporter's contributions to the targets, from both providers. Empty when signed out.
FetchedAt#
FetchedAt *string `json:"fetchedAt"`FetchedAt is when the API produced this data (ISO-8601), or nil when nothing was fetched yet.
Stale#
Stale bool `json:"stale"`Stale is true when some provider could not be reached and its data (if any) comes from the local cache.
FromCache#
FromCache bool `json:"fromCache"`FromCache is true when the whole result was served from the local cache.
Unavailable#
Unavailable []Provider `json:"unavailable,omitempty"`Unavailable lists providers the API could not query when this result was fetched. It is kept when the result is served from the cache.
Functions#
VerifySession#
func VerifySession(ctx context.Context, token string, targets Targets, opts ...VerifyOption) (*Result, error)VerifySession is the backend helper. A frontend forwards its session token (Client.SessionToken); the backend calls VerifySession, which fetches GET /api/v1/contributions with the token and checks that the session's targets equal targets. It uses no local storage or cache.
Errors: invalid_config (malformed targets), session_invalid (rejected or malformed token), targets_mismatch (the session belongs to another target set) and network_error (Sponsorkit could not be reached, or answered 5xx, 429 or an unexpected response; *Error.RetryAfter carries Retry-After).
Example#
package main
import (
"fmt"
"net/http"
sponsorkit "github.com/ffMathy/sponsorkit/sdks/go"
)
func main() {
http.HandleFunc("/api/pro-feature", func(w http.ResponseWriter, r *http.Request) {
token := r.Header.Get("X-Sponsorkit-Session") // forwarded from Client.SessionToken()
fc, err := sponsorkit.VerifySession(r.Context(), token, sponsorkit.Targets{GitHub: "octocat"})
switch sponsorkit.ErrorCodeOf(err) {
case "":
case sponsorkit.CodeSessionInvalid, sponsorkit.CodeTargetsMismatch:
http.Error(w, "sign in again", http.StatusUnauthorized)
return
default:
http.Error(w, "try again later", http.StatusServiceUnavailable)
return
}
if len(fc.Active()) == 0 {
http.Error(w, "sponsors only", http.StatusPaymentRequired)
return
}
fmt.Fprintln(w, "welcome, sponsor")
})
}Methods#
Active#
func (r *Result) Active() []ContributionActive returns the contributions whose status is "active". Helpers only filter; they never sum across providers or currencies.
ByProvider#
func (r *Result) ByProvider(p Provider) []ContributionByProvider returns the contributions made through provider p.
Direct#
func (r *Result) Direct() []ContributionDirect returns the contributions paid by the user themselves, excluding organization sponsorships.
MarshalJSON#
func (r Result) MarshalJSON() ([]byte, error)MarshalJSON encodes nil slices as [].
OneTime#
func (r *Result) OneTime() []ContributionOneTime returns the one-time contributions (any status).
Recurring#
func (r *Result) Recurring() []ContributionRecurring returns the recurring contributions (any status).