api

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: MIT Imports: 12 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 a small HTTP client for the Hemmelig API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CodeOf

func CodeOf(err error) string

CodeOf returns the error code of an API error, or "" for other errors.

func StatusOf

func StatusOf(err error) int

StatusOf returns the HTTP status of an API error, or 0 for other errors.

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

func (b Bytes) MarshalJSON() ([]byte, error)

MarshalJSON writes the object form that the API expects.

func (*Bytes) UnmarshalJSON

func (b *Bytes) UnmarshalJSON(data []byte) error

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

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

Do sends the request, fails for a status outside 2xx, and decodes a JSON response into out when out is not nil.

func (*Client) Get

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

Get is a shortcut for a GET request.

func (*Client) Raw

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

Raw sends the request and returns the response for any status.

func (*Client) Send

func (c *Client) Send(ctx context.Context, method, path string, body, out any) error

Send is a shortcut for a request with a JSON body.

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.

func (*Error) Error

func (e *Error) Error() string

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.

type Response

type Response struct {
	Status  int
	Headers http.Header
	Body    io.ReadCloser
}

Response is a raw API response. Close Body when you are done.

Jump to

Keyboard shortcuts

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