client

package
v0.2.710 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2026 License: MIT Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const AccountTokenPrefix = "cfat_"

AccountTokenPrefix marks account-owned API tokens.

Variables

This section is empty.

Functions

func APIError

func APIError(err error) error

APIError turns an SDK error into a short "HTTP 403: message" error. Request headers (and so the token) are never included.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err is an API 404.

func IsTimeout added in v0.1.2

func IsTimeout(err error) bool

IsTimeout reports whether err is a context deadline or network timeout.

func ListAccounts

func ListAccounts(ctx context.Context, c *cloudflare.Client) ([]accounts.Account, error)

ListAccounts returns every account the token can see (GET /accounts).

It pages explicitly instead of using the SDK auto-pager: it stops on an empty page, at result_info.total_pages, or after maxAccountPages.

func Messages

func Messages(err error) string

Messages returns the API error messages from a Cloudflare error, joined.

func NewClient

func NewClient(token string) *cloudflare.Client

NewClient builds a Cloudflare SDK client that authenticates with token. It sends every request through api.NewHTTPClient, so the read-only guard applies to the SDK too.

The SDK also reads CLOUDFLARE_API_KEY / CLOUDFLARE_EMAIL from the environment; those headers are stripped so only the bearer token is used.

func ResolveZone

func ResolveZone(ctx context.Context, app *App, nameOrID string) (*zones.Zone, error)

ResolveZone accepts a zone name (example.com) or a 32-char zone ID and returns the zone.

func StatusCode

func StatusCode(err error) int

StatusCode returns the HTTP status of a Cloudflare API error, or 0.

Types

type App

type App struct {
	Client    *cloudflare.Client
	AccountID string
	// contains filtered or unexported fields
}

App holds the authenticated Cloudflare client and resolved account ID.

func New

func New(ctx context.Context) (*App, error)

New creates an App from the stored (or env) token and the effective account ID. If no account ID is known and the token sees exactly one account, that account is used and cached.

func (*App) VerifyToken added in v0.1.1

func (a *App) VerifyToken(ctx context.Context) (*TokenInfo, error)

VerifyToken verifies the App's token with the endpoint matching its type.

type TokenInfo added in v0.1.1

type TokenInfo struct {
	ID        string    `json:"id"`
	Status    string    `json:"status"`
	ExpiresOn time.Time `json:"expires_on,omitempty"`
	// Type is config.TokenTypeUser or config.TokenTypeAccount.
	Type string `json:"type"`
	// AccountID is the owning account, for account-owned tokens.
	AccountID string `json:"account_id,omitempty"`
}

TokenInfo describes a verified API token. It never holds the token itself.

func ValidateToken

func ValidateToken(ctx context.Context, token string, candidates []string, hint string) (*TokenInfo, error)

ValidateToken verifies a token and works out whether it is a user token or an account-owned token. See VerifyToken.

func VerifyToken added in v0.1.1

func VerifyToken(ctx context.Context, c *cloudflare.Client, token string, candidates []string, hint string) (*TokenInfo, error)

VerifyToken verifies a token against the right endpoint:

  • user tokens: GET /user/tokens/verify
  • account tokens: GET /accounts/{id}/tokens/verify

The user endpoint is tried first, unless the token starts with cfat_ or hint says "account". If it rejects the token (401), each candidate account ID is tried.

Jump to

Keyboard shortcuts

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