Documentation
¶
Overview ¶
Package entitlements loads an entitlements-api catalog seed through its admin API, the same path an administrator uses, so every row is validated and audited. The seed and price files are supplied by the caller (they live in the entitlements-api repo); this package holds no catalog data.
Index ¶
Constants ¶
const ( DefaultDelay = 300 * time.Millisecond DefaultMaxRetries = 6 )
Pacing and retry defaults. The service's standard rate limit is 30 requests per 60 seconds per client, and it answers 429 without a Retry-After header, so a rate-limited request waits out an exponential backoff that reaches a full window.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// BaseURL includes any ingress prefix, e.g. https://host/api/1/entitlements.
BaseURL string
// Token is an administrator's bearer token. It is only ever sent, never logged.
Token string
HTTP *http.Client
// ReplacePrices lets the overlay replace a product's existing, different price. Off by
// default: replacing a live price is deliberate, so it is reported as skipped instead.
ReplacePrices bool
// Delay is the pause before each request after the first, to stay under the rate limit.
Delay time.Duration
// MaxRetries bounds how many times one request is retried after a 429.
MaxRetries int
// Logf receives progress notes such as a rate-limit wait. Nil discards them.
Logf func(format string, args ...any)
// Sleep waits for d or until ctx ends; nil uses a real timer. Tests replace it.
Sleep func(ctx context.Context, d time.Duration) error
// contains filtered or unexported fields
}
Client talks to a running entitlements-api admin API.
func (*Client) Load ¶
Load creates what is missing: keys, tiers, the default tier (only when none is set), then products, each carrying its overlay price when it has one. An existing product that has no price gets the overlay price; one with a different price is left and reported as skipped. It never overwrites anything else, so re-running it cannot undo an administrator's edit. prices may be nil. DryRun reports without writing.
type Doc ¶
type Doc struct {
Keys []Key `json:"keys"`
DefaultTier string `json:"default_tier"`
Tiers []Tier `json:"tiers"`
Products []Product `json:"products"`
}
Doc is a seed file. Stripe price IDs are left empty until prices exist in Stripe; set them through the admin API afterwards.
type Key ¶
type Key struct {
Key string `json:"key"`
Type string `json:"type"`
Description string `json:"description"`
TTLSeconds int `json:"ttl_seconds"`
Max *int64 `json:"max,omitempty"`
}
Key mirrors the admin API's key body.
type Prices ¶
Prices is a per-environment overlay mapping product IDs to Stripe price IDs. Price IDs differ between Stripe test and live mode, so they live outside the seed.
func ParsePrices ¶
ParsePrices decodes an overlay, rejecting unknown fields and empty prices. A price ID is an opaque string: Stripe generates price_... values, but one can be set by hand (members_10).
type Product ¶
type Product struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Billing string `json:"billing"`
Repeatable bool `json:"repeatable,omitempty"`
PlanGroup string `json:"plan_group,omitempty"`
StripePriceID string `json:"stripe_price_id,omitempty"`
Entitlements map[string]any `json:"entitlements"`
}
Product mirrors the admin API's product body.
type Report ¶
type Report struct {
Created []string
Existing []string
// Skipped rows were deliberately not changed.
Skipped []string
// Replaced lists live prices swapped for the overlay's (or, in a dry run, that would be),
// each as "price <product> (<old> -> <new>)". Only populated with Client.ReplacePrices.
Replaced []string
}
Report says what a load did. Existing rows are left exactly as they are.
type StatusError ¶ added in v0.6.1
StatusError is an unexpected response from the service. It keeps the HTTP status so the CLI can choose an exit code from it.
func (*StatusError) Error ¶ added in v0.6.1
func (e *StatusError) Error() string
func (*StatusError) HTTPStatus ¶ added in v0.6.1
func (e *StatusError) HTTPStatus() int
HTTPStatus is the response status.