Transport for the GLEIF API: the settings resolver (per-call opts first, then context variables, then defaults), a typed HttpError, JSON GETs, and query-string building that drops unset values.

opts keyContext variableDefault
base-urlgleif.base-urlhttps://api.gleif.org/api/v1

GLEIF takes no credential, so a per-call base-url needs none.

Context Vars

Optional
  • gleif.base-url

Values

BASE_URL

BASE_URL: Str "https://api.gleif.org/api/v1"

PACKAGE_AGENT

PACKAGE_AGENT: Str "hot-dev-gleif-pkg/0.1 (+https://hot.dev)"

SOURCE

SOURCE: Str "gleif"

Functions

get-json

fn (url: Str, headers: Map): Any

GET a JSON resource and return the decoded body, or an Err carrying HttpError.

Example

body ::gleif::api/get-json("https://api.gleif.org/api/v1/lei-records/HWUPKR0MPOU8FGXBT394", {Accept: "application/vnd.api+json"})
body.data.attributes.entity.legalName.name

get-json-or-null

fn (url: Str, headers: Map): Any

get-json, but a 404 gives null (an absent parent or record) instead of an Err.

Example

body ::gleif::api/get-json-or-null("https://api.gleif.org/api/v1/lei-records/HWUPKR0MPOU8FGXBT394", {Accept: "application/vnd.api+json"})
if(is-some(body), body.data.id, "Not found")

given

fn (v: Any): Bool

True for a non-null value that is not a blank string.

Example

::gleif::api/given("  ")  // false
::gleif::api/given(null)  // false
::gleif::api/given(0)  // true

http-error

fn (response: ::hot::http/HttpResponse, url: Str): HttpError

Build an HttpError from a non-2xx response. Exposed for testing; every request uses it.

Example

::api ::gleif::api

response {
  status: 429,
  headers: {"retry-after": "30"},
  body: {errors: [{title: "Too many requests"}]}
}
e ::api/http-error(response, "https://api.gleif.org/api/v1/lei-records")
e.message  // "gleif: HTTP 429: Too many requests"
e.retry-after  // 30

is-not-found

fn (e: Any): Bool

True when e is an HttpError with status 404.

Example

::gleif::api/is-not-found({status: 404})  // true
::gleif::api/is-not-found({status: 429})  // false
::gleif::api/is-not-found("HTTP 404")  // false

path-segment

fn (value: Any): Str

Percent-encode one path segment.

Example

::gleif::api/path-segment("a/b c")  // "a%2Fb%20c"
::gleif::api/path-segment(42)  // "42"

query-string

fn (params: Map): Str

Build a ?a=1&b=2 suffix from a Map, dropping null and blank values (pure); "" when nothing remains. Keys may contain brackets (filter[fulltext], page[size]) and are encoded.

Example

::gleif::api/query-string({q: "apple inc", page: null, field: ""})  // "?q=apple+inc"
::gleif::api/query-string({"filter[fulltext]": "a&b"})  // "?filter%5Bfulltext%5D=a%26b"
::gleif::api/query-string({a: null})  // ""

request

fn (method: Str, url: Str, headers: Map): ::hot::http/HttpResponse

GET (or another method) a GLEIF URL with headers, returning the HttpResponse on 2xx or an Err carrying HttpError. A transport failure passes through as its own Err.

Example

response ::gleif::api/request("GET", "https://api.gleif.org/api/v1/lei-records/HWUPKR0MPOU8FGXBT394", {Accept: "application/vnd.api+json"})
response.status

resolve

fn (opts: Map): Map

Resolve settings for one call: {base-url}. A value in opts wins over context.

Example

cfg ::gleif::api/resolve({base-url: "https://gleif-proxy.example/api/v1"})

Types

HttpError

HttpError type {
    source: Str,
    status: Int,
    url: Str,
    headers: Map,
    body: Any,
    retry-after: Int?,
    message: Str
}

A non-2xx GLEIF response. source is "gleif"; url the requested URL (GLEIF takes no credentials in URLs); retry-after the parsed Retry-After seconds on 429s; message a one-line summary that never includes credentials.