remote

package
v0.18.2 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: MIT Imports: 20 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 remote talks to a self-hosted ZenNotes server over its HTTP API, the same routes the desktop and web clients use.

Index

Constants

View Source
const HeadersEnv = "ZENNOTES_REMOTE_HEADERS"

HeadersEnv is the variable zn, zn tui and zn mcp read the headers from.

View Source
const MaxHeaders = 20

MaxHeaders is more than any gateway needs; a pasted file is likelier.

Variables

This section is empty.

Functions

func ApplyHeaders added in v0.18.0

func ApplyHeaders(h http.Header, headers []Header)

ApplyHeaders adds the headers to h. They never replace one already set.

func ConnectionErrorMessage

func ConnectionErrorMessage(baseURL string, err error) string

ConnectionErrorMessage phrases a transport-level failure. On macOS a server on the local network needs the Local Network permission, and without it the attempt looks exactly like a server that is down.

func GatewayRefusedMessage added in v0.18.0

func GatewayRefusedMessage(baseURL string) string

GatewayRefusedMessage names a gateway in front of the server that turned a request away. The ZenNotes server answers in plain text, so an HTML 403 came from something in front of it.

func IsNoteChanged added in v0.18.2

func IsNoteChanged(err error) bool

IsNoteChanged reports a write refused because the note moved on the server.

func IsSha256Hex added in v0.18.2

func IsSha256Hex(value string) bool

IsSha256Hex reports a value worth sending as a base: 64 hex digits.

func NormalizeBaseURL

func NormalizeBaseURL(value string) string

NormalizeBaseURL turns `localhost:7878` into `http://localhost:7878` and drops any trailing slash.

func StatusOf

func StatusOf(err error) int

StatusOf is the HTTP status behind an error, or 0 when it carries none.

Types

type AbsenceAwareReader

type AbsenceAwareReader struct {
	// contains filtered or unexported fields
}

AbsenceAwareReader tells "this file is absent" apart from "this request failed" against a server whose answer for the two might be the same.

Servers from 2.20 on answer 404 for a missing file and reserve 500 for real failures. Older ones answered 500 for both, and a client refusing to read 500 as absence cannot open a single database against one. Rather than guess from a version, the reader asks the server what it does: it requests a path that cannot exist and looks at the status, once per connection.

func NewAbsenceAwareReader

func NewAbsenceAwareReader(read func(ctx context.Context, rel string) (string, error)) *AbsenceAwareReader

NewAbsenceAwareReader wraps a raw read.

func (*AbsenceAwareReader) ReadFileTextOrNull

func (r *AbsenceAwareReader) ReadFileTextOrNull(ctx context.Context, rel string) (*string, error)

ReadFileTextOrNull returns nil for an absent file and an error for everything else.

type ChangeEvent added in v0.5.0

type ChangeEvent struct {
	Kind  string `json:"kind"`
	Path  string `json:"path"`
	Scope string `json:"scope,omitempty"`
	Err   error  `json:"-"`
}

ChangeEvent is the desktop/server watcher protocol.

type Client

type Client struct {
	BaseURL   string
	AuthToken string
	// Headers go with every request and the change feed's handshake, for an
	// access gateway in front of the server (see headers.go).
	Headers []Header
	// contains filtered or unexported fields
}

Client is a fetch-only client for one server.

func NewClient

func NewClient(baseURL, authToken string) *Client

NewClient binds a client to a server and an optional token. It sends the extra headers from ZENNOTES_REMOTE_HEADERS; a value that does not parse fails every request with the reason, before anything is sent.

func NewClientWithTransport added in v0.17.0

func NewClientWithTransport(baseURL string, transport http.RoundTripper) *Client

NewClientWithTransport is a client whose requests go through transport instead of the network, for a server that calls its own API in-process. It sends no token: the transport is what vouches for the request.

func (*Client) ArchiveNote

func (c *Client) ArchiveNote(ctx context.Context, rel string) (vault.NoteMeta, error)

func (*Client) Capabilities

func (c *Client) Capabilities(ctx context.Context) (map[string]any, error)

Capabilities is what the server says it supports.

func (*Client) CreateFolder

func (c *Client) CreateFolder(ctx context.Context, folder vault.NoteFolder, subpath string) error

func (*Client) CreateNote

func (c *Client) CreateNote(ctx context.Context, folder vault.NoteFolder, title, subpath string) (vault.NoteMeta, error)

func (*Client) DeleteFolder

func (c *Client) DeleteFolder(ctx context.Context, folder vault.NoteFolder, subpath string) error

func (*Client) DeleteNote

func (c *Client) DeleteNote(ctx context.Context, rel string) error

func (*Client) DeleteTemplate

func (c *Client) DeleteTemplate(ctx context.Context, sourcePath string) error

func (*Client) DuplicateNote

func (c *Client) DuplicateNote(ctx context.Context, rel string) (vault.NoteMeta, error)

func (*Client) EmptyTrash

func (c *Client) EmptyTrash(ctx context.Context) error

func (*Client) Get

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

Get performs a JSON GET.

func (*Client) GetCurrentVault

func (c *Client) GetCurrentVault(ctx context.Context) (*vault.VaultInfo, error)

GetCurrentVault is the vault the server is serving, or nil.

func (*Client) GetRaw

func (c *Client) GetRaw(ctx context.Context, path string) ([]byte, error)

GetRaw performs a GET and returns the body untouched.

func (*Client) GetVaultSettings

func (c *Client) GetVaultSettings(ctx context.Context) (map[string]any, error)

GetVaultSettings is the server's vault.json as it reports it.

func (*Client) ImportAsset added in v0.5.0

func (c *Client) ImportAsset(ctx context.Context, note, name string, body io.Reader) (vault.ImportedAsset, error)

func (*Client) ListAssets

func (c *Client) ListAssets(ctx context.Context) ([]vault.AssetMeta, error)

func (*Client) ListFolders

func (c *Client) ListFolders(ctx context.Context) ([]vault.FolderEntry, error)

func (*Client) ListNotes

func (c *Client) ListNotes(ctx context.Context) ([]vault.NoteMeta, error)

func (*Client) ListTemplates

func (c *Client) ListTemplates(ctx context.Context) ([]vault.CustomTemplateFile, error)

func (*Client) MoveNote

func (c *Client) MoveNote(ctx context.Context, rel string, folder vault.NoteFolder, subpath string) (vault.NoteMeta, error)

func (*Client) MoveToTrash

func (c *Client) MoveToTrash(ctx context.Context, rel string) (vault.NoteMeta, error)

func (*Client) Post

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

Post performs a JSON POST; a nil body sends `{}`.

func (*Client) ReadAsset

func (c *Client) ReadAsset(ctx context.Context, rel string) ([]byte, error)

ReadAsset fetches an attachment's bytes.

func (*Client) ReadComments

func (c *Client) ReadComments(ctx context.Context, rel string) ([]vault.NoteComment, error)

ReadComments loads a note's comment sidecar from the server.

func (*Client) ReadNote

func (c *Client) ReadNote(ctx context.Context, rel string) (vault.NoteContent, error)

ReadNote carries the server's sha256 of the note in NoteMeta.Sha256 when the server sends one (supportsConditionalNoteWrites), for a later write's base.

func (*Client) ReadTemplate

func (c *Client) ReadTemplate(ctx context.Context, sourcePath string) (string, error)

func (*Client) RenameFolder

func (c *Client) RenameFolder(ctx context.Context, folder vault.NoteFolder, oldSubpath, newSubpath string) (string, error)

func (*Client) RenameNote

func (c *Client) RenameNote(ctx context.Context, rel, nextTitle string) (vault.NoteMeta, error)

func (*Client) RestoreFromTrash

func (c *Client) RestoreFromTrash(ctx context.Context, rel string) (vault.NoteMeta, error)

func (*Client) ScanTasks

func (c *Client) ScanTasks(ctx context.Context, includeExcluded bool) ([]vault.Task, error)

func (*Client) ScanTasksForPath

func (c *Client) ScanTasksForPath(ctx context.Context, rel string, includeExcluded bool) ([]vault.Task, error)

func (*Client) SearchText

func (c *Client) SearchText(ctx context.Context, query string) ([]vault.TextSearchMatch, error)

func (*Client) SearchTextUpTo added in v0.17.0

func (c *Client) SearchTextUpTo(ctx context.Context, query string, limit int) ([]vault.TextSearchMatch, error)

SearchTextUpTo asks for more matches than a server returns by default, which only a current one honours. Zero takes its default.

func (*Client) SetVaultSettings

func (c *Client) SetVaultSettings(ctx context.Context, settings map[string]any) (map[string]any, error)

SetVaultSettings writes the whole settings object back.

func (*Client) UnarchiveNote

func (c *Client) UnarchiveNote(ctx context.Context, rel string) (vault.NoteMeta, error)

func (*Client) WatchChanges added in v0.5.0

func (c *Client) WatchChanges(ctx context.Context) (<-chan ChangeEvent, error)

func (*Client) WriteComments

func (c *Client) WriteComments(ctx context.Context, rel string, comments []vault.NoteComment) ([]vault.NoteComment, error)

WriteComments replaces a note's comments on the server.

func (*Client) WriteNote

func (c *Client) WriteNote(ctx context.Context, rel, body string) (vault.NoteMeta, error)

WriteNote replaces a note's body unconditionally, last writer wins.

func (*Client) WriteNoteIfUnchanged added in v0.18.2

func (c *Client) WriteNoteIfUnchanged(ctx context.Context, rel, body, baseSha256 string) (vault.NoteMeta, error)

WriteNoteIfUnchanged writes a note only if it is still the version baseSha256 names: the sha256 a read or a write of it returned. A server with conditional writes refuses with 409 and writes nothing when the note changed or is gone since, which comes back as a *NoteChangedError. An empty base, or one that is not a hex SHA-256, sends none and the write is unconditional; an older server ignores the field, so it is only worth sending a value the server itself handed out.

func (*Client) WriteTemplate

func (c *Client) WriteTemplate(ctx context.Context, input vault.WriteTemplateInput) (vault.CustomTemplateFile, error)
type Header struct {
	Name  string
	Value string
}

Header is one extra request header.

func EnvHeaders added in v0.18.0

func EnvHeaders() ([]Header, error)

EnvHeaders is HeadersFromEnv for this process, read once. A value that does not parse is an error every request returns before it is sent: running on without the headers would only trade this message for the gateway's 403.

func HeadersFromEnv added in v0.18.0

func HeadersFromEnv(getenv func(string) string) ([]Header, error)

HeadersFromEnv reads HeadersEnv through getenv. Unset or blank means none. A JSON env block (an MCP client's config) can only carry a line break as the two characters `\n`, so those count as one.

func ParseHeaders added in v0.18.0

func ParseHeaders(text string) ([]Header, error)

ParseHeaders reads `Name: value` lines. Blank lines and lines starting with `#` are skipped. The first problem wins and names its line; nothing is half-accepted.

type NoteChangedError added in v0.18.2

type NoteChangedError struct {
	Path string
}

NoteChangedError is a write the server refused because the note is no longer the version the write named, or no longer exists. Nothing was written. The message is the desktop's, word for word.

func (*NoteChangedError) Error added in v0.18.2

func (e *NoteChangedError) Error() string

type RequestError

type RequestError struct {
	Status  int
	Message string
}

RequestError is a non-2xx answer, with the status attached so a 404 (absent file) can be told from a real failure.

func (*RequestError) Error

func (e *RequestError) Error() string

Directories

Path Synopsis
Package remotetest is an in-memory ZenNotes server for tests: the note routes a read, change and write needs, with or without conditional note writes (znserver supportsConditionalNoteWrites).
Package remotetest is an in-memory ZenNotes server for tests: the note routes a read, change and write needs, with or without conditional note writes (znserver supportsConditionalNoteWrites).

Jump to

Keyboard shortcuts

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