Documentation
¶
Overview ¶
Package format is the file format of the HTTP client, version 1 (SPEC §10.33 "The format"): the types of a collection, a folder, a request and an environment, reading them from text with comments, and writing them.
Reading accepts comments and trailing commas (extension/jsonc). Writing a file that exists goes through the edits of the comment-keeping JSON editor, so the comments, the layout and the keys a user added by hand stay; a new file is written with the keys in the fixed order of the SPEC, two spaces and a final line break. A file of a newer version is read, and never written. A secret variable never has its value in text (D45).
Nothing here knows the workspace except ResolvePath and the file helpers, which keep every path inside the workspace folder.
Index ¶
- Constants
- func BodyExtension(bodyType string) string
- func BodyFileName(stem, bodyType string) string
- func CheckPaths(folder string, r Request) error
- func EnvironmentFileName(stem string) string
- func IsNewer(err error) bool
- func Marshal[T Document](doc T) (string, error)
- func Parse[T Document](text string) (T, error)
- func ReadFile(ctx context.Context, path string) (string, error)
- func RequestFileName(stem string) string
- func RequestStem(fileName string) (string, bool)
- func ResolvePath(folder, p string) (string, error)
- func SafeName(name string) string
- func UniqueName(base string, taken func(name string) bool) string
- func Update[T Document](oldText string, doc T) (string, error)
- func ValidSiblingName(name string) bool
- func VariablesFileName(stem string) string
- func WriteFile(ctx context.Context, path, text string) (err error)
- type Auth
- type Body
- type Check
- type Collection
- type Document
- type Environment
- type ExtractRule
- type Folder
- type Pair
- type Position
- type Request
- type Settings
- type Variable
Constants ¶
const ( // Dir is the folder of the HTTP client in the workspace folder. Dir = ".emetos/http" // CollectionsDir and EnvironmentsDir are the folders of Dir that hold the collections and the environments. CollectionsDir = "collections" EnvironmentsDir = "environments" // CollectionFile and FolderFile are the files that describe a collection and a folder of one. CollectionFile = "collection.jsonc" FolderFile = "folder.jsonc" // RequestSuffix and EnvironmentSuffix end the file name of a request and of an environment. RequestSuffix = ".request.jsonc" EnvironmentSuffix = ".jsonc" )
The folders and file names of a project (SPEC §10.33).
const ( AuthInherit = "inherit" AuthNone = "none" AuthBasic = "basic" AuthBearer = "bearer" AuthAPIKey = "apikey" AuthDigest = "digest" AuthOAuth2 = "oauth2" )
The types of an Auth.
const ( BodyNone = "none" BodyJSON = "json" BodyXML = "xml" BodyText = "text" BodyHTML = "html" BodyJavaScript = "javascript" BodyGraphQL = "graphql" BodyForm = "form" BodyMultipart = "multipart" BodyBinary = "binary" )
The types of a Body.
const MaxFileSize = 8 << 20
MaxFileSize is the largest file the client reads.
const MaxNameLength = 100
MaxNameLength is the most bytes of a safe name.
const Version = 1
Version is the version of the format this package reads and writes (the value of the key "emetosHttp").
Variables ¶
This section is empty.
Functions ¶
func BodyExtension ¶
BodyExtension is the extension of the body file of a body type, with its dot; it is empty for a type that has no body file.
func BodyFileName ¶
BodyFileName is the name of the body file of the request with the stem, or "" for a type with no body file.
func CheckPaths ¶
CheckPaths checks every file path a request names (the path of a binary body and the files of multipart fields) with ResolvePath, and returns the first error.
func EnvironmentFileName ¶
EnvironmentFileName is the file name of the environment whose stem is stem.
func Marshal ¶
Marshal writes a new file for the document: the keys in the order of the SPEC, two spaces, a final line break. A secret variable has no value in it. It fails with HttpFileValueInvalid for a value the format does not allow (the key is in the metadata) and HttpFileEncodeFailed.
func Parse ¶
Parse reads a file of the client from its text, with comments and trailing commas allowed. Keys it does not know are ignored.
A file of a newer version (emetosHttp above Version) is read as far as it can be and returned together with an error that has the tag HttpFileNewerVersion (IsNewer tells it): the caller shows the file read-only. The other errors, which come with the line and column in their metadata ("line", "column", "offset"), have the tags HttpFileSyntax (the text is not JSON with comments), HttpFileNotObject, HttpFileVersionMissing, HttpFileVersionInvalid and HttpFileValueInvalid (a key holds a value of the wrong shape, or one the format does not allow; "key" names it).
func ReadFile ¶
ReadFile reads a file of the client as UTF-8 text. The errors have the tags HttpFileReadFailed (the cause is kept, so errors.Is(err, fs.ErrNotExist) tells a missing file), HttpFileTooLarge and HttpFileNotText.
func RequestFileName ¶
RequestFileName is the file name of the request whose stem is stem.
func RequestStem ¶
RequestStem is the stem of a request file name, and whether the name is one.
func ResolvePath ¶
ResolvePath returns the absolute path of p, a path relative to the workspace folder (an absolute one must lie inside it), and refuses a path that leaves the folder, by a ".." or by a link that points out of it. A path that does not exist yet is checked through its nearest folder that does. The errors have the tags HttpPathInvalid (empty, or with a NUL) and HttpPathOutsideWorkspace.
func SafeName ¶
SafeName makes a name fit for a file name (SPEC §10.33): lowercase, letters and digits and "_" kept, everything else (spaces, slashes, dots, punctuation) becomes "-", runs of "-" become one and none is left at the ends. A name with nothing left is "untitled". The result is never "." or "..", has no separator and is at most MaxNameLength bytes.
func UniqueName ¶
UniqueName returns base, or base-2, base-3, ... for the first of them that taken does not report as taken.
func Update ¶
Update returns the text of a file that holds the document, made from the old text of the file by edits: a value that did not change is not touched, the comments and the layout stay, and so do the keys the format does not know and the unknown keys of the objects of the tables. A key the document lacks that the format knows is removed; a new key goes where the order of the SPEC puts it among the keys the file has. A secret variable's value is removed from the text.
It fails with HttpFileValueInvalid (a value the format does not allow), HttpFileSyntax, HttpFileNotObject (the old text is not a file of the client), HttpFileNewerVersion (the old file is of a newer version: it is never written), HttpFileEncodeFailed and the tags of the JSON editor.
func ValidSiblingName ¶
ValidSiblingName reports whether name can be the name of a body file: a plain file name, no folders.
func VariablesFileName ¶
VariablesFileName is the name of the file of the GraphQL variables of the request with the stem.
Types ¶
type Auth ¶
type Auth struct {
Type string `json:"type"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Token string `json:"token,omitempty"`
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
In string `json:"in,omitempty"`
Grant string `json:"grant,omitempty"`
TokenURL string `json:"tokenUrl,omitempty"`
AuthURL string `json:"authUrl,omitempty"`
ClientID string `json:"clientId,omitempty"`
ClientSecret string `json:"clientSecret,omitempty"`
Scope string `json:"scope,omitempty"`
Audience string `json:"audience,omitempty"`
PKCE bool `json:"pkce,omitempty"`
AddTo string `json:"addTo,omitempty"`
}
Auth is the authorization of a collection, folder or request. Which fields count depends on Type.
type Body ¶
type Body struct {
Type string `json:"type"`
File string `json:"file,omitempty"`
Variables string `json:"variables,omitempty"`
Fields []Pair `json:"fields,omitempty"`
Path string `json:"path,omitempty"`
}
Body is the body of a request. The text of a raw body is in the file named File, next to the request file; the variables of a GraphQL body are in the file named Variables.
type Check ¶
type Check struct {
Subject string `json:"subject"`
Path string `json:"path,omitempty"`
Header string `json:"header,omitempty"`
Op string `json:"op"`
Value any `json:"value,omitempty"`
}
Check is an assertion about a response.
type Collection ¶
type Collection struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
// Order lists the entries (requests by file name, folders by folder name) in the order they are shown.
Order []string `json:"order,omitempty"`
Variables []Variable `json:"variables,omitempty"`
Auth *Auth `json:"auth,omitempty"`
Headers []Pair `json:"headers,omitempty"`
}
Collection is collection.jsonc, and (as Folder) folder.jsonc of a folder of a collection.
type Document ¶
type Document interface {
Collection | Request | Environment
}
Document is a type of file the client reads and writes: Collection (and Folder), Request and Environment.
type Environment ¶
type Environment struct {
Name string `json:"name"`
Variables []Variable `json:"variables,omitempty"`
}
Environment is a file of environments/.
type ExtractRule ¶
type ExtractRule struct {
From string `json:"from"`
Path string `json:"path,omitempty"`
Header string `json:"header,omitempty"`
Cookie string `json:"cookie,omitempty"`
Regex string `json:"regex,omitempty"`
To string `json:"to"`
Scope string `json:"scope,omitempty"`
}
ExtractRule takes a value out of a response into a variable.
type Folder ¶
type Folder = Collection
Folder is folder.jsonc: it has the keys of a collection (a folder has variables too: SPEC §10.33 "As built (step 27.1)").
type Pair ¶
type Pair struct {
Name string
Value string
File string
Description string
// Enabled is true unless the file says "enabled": false.
Enabled bool
}
Pair is a name and a value of the tables of the request tab (a parameter, a header, a form field) and may have a description. A multipart field that sends a file has File and no Value.
func (Pair) MarshalJSON ¶
MarshalJSON writes the pair with "enabled" only when it is false, and "value" unless the pair is a file.
func (*Pair) UnmarshalJSON ¶
UnmarshalJSON reads a pair; a missing "enabled" is true.
type Position ¶
type Position struct {
Line, Column, Offset int
}
Position is a place in a text: the line and column (both from 1, the column counted in characters) and the byte offset.
func PositionOf ¶
PositionOf returns the place of the byte offset in the text.
type Request ¶
type Request struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Method string `json:"method"`
URL string `json:"url"`
Params []Pair `json:"params,omitempty"`
Headers []Pair `json:"headers,omitempty"`
Auth *Auth `json:"auth,omitempty"`
Body *Body `json:"body,omitempty"`
Settings *Settings `json:"settings,omitempty"`
Extract []ExtractRule `json:"extract,omitempty"`
Checks []Check `json:"checks,omitempty"`
}
Request is a .request.jsonc file.
type Settings ¶
type Settings struct {
TimeoutMs *int `json:"timeoutMs,omitempty"`
FollowRedirects *bool `json:"followRedirects,omitempty"`
MaxRedirects *int `json:"maxRedirects,omitempty"`
VerifyTLS *bool `json:"verifyTls,omitempty"`
}
Settings are the per-request settings; a nil value means the default of the setting of the editor.
type Variable ¶
type Variable struct {
Name string
// Value is the initial value; it is empty for a secret.
Value string
// Enabled is true unless the file says "enabled": false.
Enabled bool
// Secret says that the value is in the keyring.
Secret bool
}
Variable is a variable of an environment or a collection. The value of a secret variable is never written to a file (D45).
func (Variable) MarshalJSON ¶
MarshalJSON writes the variable: no "value" for a secret, "enabled" only when false, "secret" only when true.
func (*Variable) UnmarshalJSON ¶
UnmarshalJSON reads a variable. A value in the file of a secret variable is dropped: it is not used and the next save removes it.