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
- func Items(data json.RawMessage, itemsKey string) ([]json.RawMessage, error)
- func MaskToken(t string) string
- func NewID() string
- func Redact(b []byte) []byte
- type Client
- func (c *Client) All(ctx context.Context, path string, q url.Values, itemsKey string, maxPages int) ([]json.RawMessage, error)
- func (c *Client) Do(ctx context.Context, r Request) (*Response, error)
- func (c *Client) Get(ctx context.Context, path string, q url.Values, out any) (*Response, error)
- func (c *Client) URL(path string, q url.Values) string
- type Error
- type PageMeta
- type Request
- type Response
Constants ¶
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.
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 (*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 ¶
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).
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.
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.