Documentation
¶
Overview ¶
Package api is a small HTTP client for the Hemmelig API.
Index ¶
- func CodeOf(err error) string
- func StatusOf(err error) int
- type Bytes
- type Client
- func (c *Client) Do(ctx context.Context, r Request, out any) error
- func (c *Client) Get(ctx context.Context, path string, query url.Values, out any) error
- func (c *Client) Raw(ctx context.Context, r Request) (*Response, error)
- func (c *Client) Send(ctx context.Context, method, path string, body, out any) error
- type Error
- type Request
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bytes ¶
type Bytes []byte
Bytes is binary data in the JSON form of the Hemmelig API.
The API sends and receives Uint8Array values as objects with numeric keys, for example {"0":12,"1":255}. Bytes also reads the Node.js Buffer form {"type":"Buffer","data":[...]} and plain arrays.
func (Bytes) MarshalJSON ¶
MarshalJSON writes the object form that the API expects.
func (*Bytes) UnmarshalJSON ¶
UnmarshalJSON reads the object form, the Buffer form or an array.
type Client ¶
type Client struct {
// BaseURL is the instance URL without a trailing slash, for example
// https://hemmelig.app.
BaseURL string
// APIKey authenticates with `Authorization: Bearer <key>` when it is set.
APIKey string
// Cookie is the session cookie header from `hemmelig login`.
Cookie string
// OnCookie runs when the server replaces the session cookie, for
// example after 2FA changes. The CLI stores the new value.
OnCookie func(cookie string)
// UserAgent identifies the CLI.
UserAgent string
HTTP *http.Client
}
Client calls one Hemmelig instance.
func (*Client) Do ¶
Do sends the request, fails for a status outside 2xx, and decodes a JSON response into out when out is not nil.
type Error ¶
type Error struct {
Status int
Message string
// Code is the machine-readable error code, when the API sends one.
Code string
}
Error is an HTTP error from the API.
type Request ¶
type Request struct {
Method string
// Path is relative to BaseURL, for example /api/secrets.
Path string
Query url.Values
Headers map[string]string
// JSON is marshaled as the request body when it is not nil.
JSON any
// Body is sent as-is when JSON is nil.
Body io.Reader
ContentLength int64
ContentType string
// NoAuth sends the request without the API key or the session cookie.
NoAuth bool
}
Request describes one API call.