fetcher

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package fetcher talks to the (unofficial) claude.ai Usage endpoint.

This is the ONE file that needs patching when Anthropic changes the endpoint shape. Keep it small and self-contained.

Endpoint captured 2026-05-13 from claude.ai/settings/usage:

GET https://claude.ai/api/organizations/<org_uuid>/usage

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoCredential = errors.New("no session cookie; run `claude-usage login`")
	ErrAuthExpired  = errors.New("session expired; run `claude-usage login` again")
	ErrRateLimited  = errors.New("claude.ai rate-limited the request; try again in a minute")
)

Sentinel errors that callers (CLI / tray) can branch on.

Functions

This section is empty.

Types

type Bucket

type Bucket struct {
	PercentUsed float64   `json:"utilization"`
	ResetsAt    time.Time `json:"resets_at"`
}

Bucket is one row on the Usage page.

type Client

type Client struct {
	HTTP          *http.Client
	SessionCookie string // value of `sessionKey` cookie on claude.ai
	OrgID         string // organization UUID; required by the endpoint
	UserAgent     string

	// Mock makes Fetch return data from samples/usage-response.json instead
	// of hitting the network. Useful before Phase 0 is complete.
	Mock bool

	// MockPath overrides where mock data is read from. Defaults to
	// "samples/usage-response.json" relative to the working directory.
	MockPath string
}

Client fetches Usage snapshots.

func New

func New(sessionCookie, orgID string) *Client

New returns a Client with reasonable defaults.

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context) (*Usage, error)

Fetch returns the current Usage snapshot.

func (*Client) FetchOrgID

func (c *Client) FetchOrgID(ctx context.Context) (string, error)

FetchOrgID returns the UUID of the user's first (or only) organization. Used to auto-fill config.OrgID when the user hasn't set one manually.

type NamedBucket

type NamedBucket struct {
	Name string
	Bucket
}

NamedBucket pairs a Bucket with its display label.

type Usage

type Usage struct {
	Session Bucket `json:"five_hour"`
	Weekly  Bucket `json:"seven_day"`
	Sonnet  Bucket `json:"seven_day_sonnet"`
	Design  Bucket `json:"seven_day_omelette"`

	// FetchedAt is set by the fetcher; not part of the upstream payload.
	FetchedAt time.Time `json:"-"`
}

Usage is the normalized snapshot returned by Fetch.

Fields map to claude.ai /api/organizations/<id>/usage payload (captured 2026-05-13). Bucket objects in that payload can be either {utilization, resets_at} or JSON null; both decode to a zero-valued Bucket here, which renderers treat as 0% / "no reset known".

func (Usage) Buckets

func (u Usage) Buckets() []NamedBucket

Buckets returns the four buckets in display order. Stable for iteration.

Jump to

Keyboard shortcuts

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