api

package
v1.18.0 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: MPL-2.0 Imports: 13 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

Overview

Package api is the HTTP client used by the gyro CLI: bearer auth, JSON envelope, retries with backoff on 429/5xx, automatic Idempotency-Key and client request ids, pagination.

Index

Constants

View Source
const DefaultURL = "https://www.gyrohosting.com/api/v1/client"

DefaultURL is the production client API base address.

Variables

This section is empty.

Functions

func Items

func Items(data json.RawMessage, itemsKey string) ([]json.RawMessage, error)

Items extracts the list from a data block.

func MaskToken

func MaskToken(t string) string

MaskToken shows only the prefix and the last 4 characters of a key.

func NewID

func NewID() string

NewID returns a random UUIDv4.

func Redact

func Redact(b []byte) []byte

Redact masks secret-looking JSON fields (used for --debug logging).

Types

type Client

type Client struct {
	BaseURL    string
	Token      string
	UserAgent  string
	Lang       string // Accept-Language
	HTTP       *http.Client
	MaxRetries int
	Debug      io.Writer // when set, requests/responses are logged here (Authorization masked)
	Sleep      func(time.Duration)
}

Client talks to the Gyro Public API.

func New

func New(baseURL, token, userAgent string) *Client

New returns a client with sane defaults.

func (*Client) All

func (c *Client) All(ctx context.Context, path string, q url.Values, itemsKey string, maxPages int) ([]json.RawMessage, error)

All walks every page of a paginated GET (meta.next_page) and returns the concatenated items. itemsKey is "" for {"data": [...]} or e.g. "items" for {"data": {"items": [...]}}.

func (*Client) Do

func (c *Client) Do(ctx context.Context, r Request) (*Response, error)

Do performs the call. GETs (and writes carrying an Idempotency-Key, which the server replays instead of repeating) are retried on network errors, 429 and 5xx with exponential backoff (Retry-After honoured).

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, q url.Values, out any) (*Response, error)

Get decodes data of a GET into out.

func (*Client) URL

func (c *Client) URL(path string, q url.Values) string

URL builds the absolute URL of a request (used by --dry-run printing too).

type Error

type Error struct {
	Status    int            `json:"status"`
	Code      string         `json:"code"`
	Message   string         `json:"message"`
	Details   map[string]any `json:"details,omitempty"`
	RequestID string         `json:"request_id,omitempty"`
	Method    string         `json:"-"`
	Path      string         `json:"-"`
}

Error is an API error ({"error": {...}}) or a transport failure after retries.

func AsError

func AsError(err error) (*Error, bool)

AsError unwraps an *Error.

func (*Error) Error

func (e *Error) Error() string

type PageMeta

type PageMeta struct {
	Total    int `json:"total"`
	Page     int `json:"page"`
	Limit    int `json:"limit"`
	NextPage int `json:"next_page"`
}

PageMeta is the pagination block of list answers.

type Request

type Request struct {
	Method string
	Path   string // relative to BaseURL, e.g. /cloud/servers
	Query  url.Values
	Body   any
	// IdempotencyKey makes a write safe to retry: the server stores the first answer for 24 h and replays it.
	// The CLI sets one for money operations (and whenever the spec requires the header). It is NOT sent for
	// calls that return one-time secrets (root password, console ticket), because the server withholds
	// secrets on idempotent requests.
	IdempotencyKey string
}

Request describes one call.

type Response

type Response struct {
	Status    int
	Data      json.RawMessage
	Meta      *PageMeta
	RequestID string
	Replayed  bool
	Header    http.Header
}

Response is a decoded success answer.

Jump to

Keyboard shortcuts

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