tier

package
v0.5.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2022 License: BSD-3-Clause Imports: 7 Imported by: 0

Documentation

Overview

Package tier contains the local client for interacting with the tier sidecar API.

For more information, please see https://tier.run/docs.

Index

Examples

Constants

View Source
const Inf = 1<<63 - 1

Variables

This section is empty.

Functions

This section is empty.

Types

type Answer added in v0.4.2

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

An Answer is the response to any question for Can. It can be used in a few forms to shorten the logic necessary to know if a program should proceed to perform a user request based on their entitlements.

func (Answer) Err added in v0.4.2

func (c Answer) Err() error

Err returns the error, if any, that occurred during the call to Can.

func (Answer) OK added in v0.4.2

func (c Answer) OK() bool

OK reports if the program should proceed with a user request or not. To prevent total failure if Can needed to reach the sidecar and was unable to, OK will fail optimistically and report true. If the opposite is desired, clients can check Err.

func (Answer) Report added in v0.4.2

func (c Answer) Report() error

Report is the same as calling ReportN(1).

func (Answer) ReportN added in v0.4.2

func (c Answer) ReportN(n int) error

ReportN reports usage of n units for the feature and org provided to Can.

type Client

type Client struct {
	HTTPClient *http.Client
}
Example
package main

import (
	"io"
	"log"
	"net/http"

	"tier.run/client/tier"
)

func main() {
	c := &tier.Client{}

	m := http.NewServeMux()
	m.HandleFunc("/convert", func(w http.ResponseWriter, r *http.Request) {
		org := orgFromSession(r)
		ans := c.Can(r.Context(), org, "feature:convert")
		if ans.OK() {
			w.WriteHeader(http.StatusTooManyRequests)
			return
		}
		defer ans.Report()
		temp := convert(r.FormValue("temp"))
		io.WriteString(w, temp)
	})

	m.HandleFunc("/subscribe", func(w http.ResponseWriter, r *http.Request) {
		org := orgFromSession(r)
		plan := r.FormValue("plan")
		if err := c.Subscribe(r.Context(), org, plan); err != nil {
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	})

	log.Fatal(http.ListenAndServe(":https", m))
}

func orgFromSession(r *http.Request) string {
	return "org:example"
}

func convert(temp string) string {
	return "80F"
}
Output:

func (*Client) Can added in v0.4.2

func (c *Client) Can(ctx context.Context, org, feature string) Answer

Can is a convenience function for checking if an org has used more of a feature than they are entitled to and optionally reporting usage post check and consumption.

If reporting consumption is not required, it can be used in the form:

if c.Can(ctx, "org:acme", "feature:convert").OK() { ... }

reporting usage post consumption looks like:

ans := c.Can(ctx, "org:acme", "feature:convert")
if !ans.OK() {
  return ""
}
defer ans.Report() // or ReportN
return convert(temp)
Example (Basic)
package main

import (
	"context"

	"tier.run/client/tier"
)

func main() {
	c := &tier.Client{}

	// Check if the user can convert a temperature.
	if c.Can(context.Background(), "org:example", "feature:convert").OK() {
		// The user can convert a temperature.
	} else {
		// The user cannot convert a temperature.
	}
}
Output:

Example (Report)
package main

import (
	"context"
	"fmt"

	"tier.run/client/tier"
)

func main() {
	c := &tier.Client{}
	ans := c.Can(context.Background(), "org:example", "feature:convert")
	if !ans.OK() {
		// The user cannot convert a temperature.
		return
	}
	defer ans.Report() // report consumption after the conversion
	fmt.Println(convert(readInput()))
}

func convert(temp string) string {
	return "80F"
}

func readInput() string {
	return "30C"
}
Output:

func (*Client) LookupLimit added in v0.4.2

func (c *Client) LookupLimit(ctx context.Context, org, feature string) (limit, used int, err error)

LookupLimit reports the current usage and limits for the provided org and feature. If the feature is not currently available to the org, both limit and used are zero and no error is reported.

It reports an error if any.

func (*Client) LookupLimits added in v0.3.1

func (c *Client) LookupLimits(ctx context.Context, org string) (apitypes.UsageResponse, error)

LookupLimits reports the current usage and limits for the provided org.

func (*Client) LookupPhase added in v0.4.2

func (c *Client) LookupPhase(ctx context.Context, org string) (apitypes.PhaseResponse, error)

LookupPhase reports information about the current phase the provided org is scheduled in.

func (*Client) Pull

func (c *Client) Pull(ctx context.Context) (apitypes.Model, error)

Pull fetches the complete pricing model from Stripe.

func (*Client) PullJSON added in v0.4.2

func (c *Client) PullJSON(ctx context.Context) ([]byte, error)

PullJSON fetches the complete pricing model from Stripe and returns the raw JSON response.

func (*Client) Push

Pull fetches the complete pricing model from Stripe.

func (*Client) PushJSON added in v0.4.2

func (c *Client) PushJSON(ctx context.Context, m []byte) (apitypes.PushResponse, error)

func (*Client) Report added in v0.4.2

func (c *Client) Report(ctx context.Context, org, feature string, n int) error

Report reports a usage of n for the provided org and feature at the current time.

func (*Client) ReportUsage

func (c *Client) ReportUsage(ctx context.Context, r apitypes.ReportRequest) error

ReportUsage reports usage based on the provided ReportRequest fields.

func (*Client) Subscribe

func (c *Client) Subscribe(ctx context.Context, org string, featuresAndPlans ...string) error

Subscribe subscribes the provided org to the provided feature or plan, effective immediately.

Any in-progress scheduled is overwritten and the customer is billed with prorations immediately.

func (*Client) WhoAmI added in v0.5.0

func (c *Client) WhoAmI(ctx context.Context) (apitypes.WhoAmIResponse, error)

func (*Client) WhoIs

func (c *Client) WhoIs(ctx context.Context, org string) (apitypes.WhoIsResponse, error)

WhoIS reports the Stripe ID for the given organization.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL