Transport for the LegiScan Pull API: the settings resolver (per-call opts first, then context variables, then defaults), a typed HttpError, key redaction, and the GET that turns LegiScan's status: "ERROR" responses into Errs.

opts keyContext variableDefault
api-keylegiscan.api-keynone: calls fail without it
base-urllegiscan.base-urlhttps://api.legiscan.com

The key travels in the URL, as LegiScan offers no header. Every HttpError and transport Err is redacted before it leaves this namespace. Per-call credentials. api-key in opts is masked in call records and never written to context. Present but blank or null, it is an error, never a fallback to context. A per-call base-url needs an api-key in the same opts: the context key is never sent to a per-call URL.

Context Vars

Optional
  • legiscan.api-key
  • legiscan.base-url

Values

BASE_URL

BASE_URL: Str "https://api.legiscan.com"

PACKAGE_AGENT

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

REDACTED

REDACTED: Str "REDACTED"

SOURCE

SOURCE: Str "legiscan"

Functions

check-body

fn (response: ::hot::http/HttpResponse, body: Any, url: Str, api-key: Str?): Any

body when its status is "OK", otherwise an Err carrying HttpError. The API reports errors with HTTP 200 and status: "ERROR", so a 2xx alone says nothing (pure).

Example

::a ::legiscan::api
HttpResponse ::hot::http/HttpResponse
response HttpResponse({status: 200, headers: {}, body: ""})
url "https://api.legiscan.com/?op=getBill"

::a/check-body(response, {status: "OK", bill: {bill_id: 7}}, url, null).bill.bill_id  // 7

failed ::a/check-body(response, {status: "ERROR", alert: {message: "Unknown bill"}}, url, null)
is-err(failed)  // true
err-message(failed)  // "legiscan: Unknown bill"

decode-body

fn (response: ::hot::http/HttpResponse): Any

The decoded JSON of a response, or its body as it came when that is not JSON, which check-body turns into an Err (pure).

Example

::a ::legiscan::api
HttpResponse ::hot::http/HttpResponse
json HttpResponse({status: 200, headers: {}, body: `{"status": "OK"}`})
::a/decode-body(json)  // {status: "OK"}

html HttpResponse({status: 200, headers: {}, body: "<html>maintenance</html>"})
::a/decode-body(html)  // "<html>maintenance</html>"

get-op

fn (cfg: Map, op: Str, params: Map): Any

Run one operation and return the decoded body, or an Err carrying HttpError for a non-2xx or a status: "ERROR" body. Each call spends one query.

Example

::a ::legiscan::api
cfg ::a/resolve({})
body ::a/get-op(cfg, "getBill", {id: 1167968})
if(is-err(body), err-message(body), body.bill.title)

given

fn (v: Any): Bool

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

Example

::legiscan::api/given("abc")  // true
::legiscan::api/given("  ")  // false
::legiscan::api/given(null)  // false

http-error

fn (response: ::hot::http/HttpResponse, url: Str, api-key: Str?): HttpError

Build a redacted HttpError from a response. Exposed for testing; every request uses it.

Example

HttpResponse ::hot::http/HttpResponse
response HttpResponse({status: 429, headers: {retry-after: "30"}, body: "Slow down"})
e ::legiscan::api/http-error(response, "https://api.legiscan.com/?key=abc&op=getBill", "abc")
e.url  // "https://api.legiscan.com/?key=REDACTED&op=getBill"
e.retry-after  // 30
e.message  // "legiscan: HTTP 429: Slow down"

operation-url

fn (cfg: Map, op: Str, params: Map): Str

The Pull API URL for one operation (pure). It holds the key: never show it, use redact.

Example

::a ::legiscan::api
cfg {api-key: "abc", base-url: "https://api.legiscan.com"}
::a/redact(::a/operation-url(cfg, "getBill", {id: 7}), cfg.api-key)
// "https://api.legiscan.com/?key=REDACTED&op=getBill&id=7"

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.

Example

::legiscan::api/query-string({op: "getBill", id: 7, state: null})  // "?op=getBill&id=7"
::legiscan::api/query-string({state: ""})  // ""

redact

fn (text: Str, api-key: Str?): Str

text with the key= query value blanked and every literal copy of api-key replaced (pure). api-key may be null.

Example

::legiscan::api/redact("https://api.legiscan.com/?key=abc&op=getBill", "abc")
// "https://api.legiscan.com/?key=REDACTED&op=getBill"

redact-value

fn (value: Any, api-key: Str?): Any

redact over a whole response value: a string, or every string inside a Map or Vec (pure).

Example

::legiscan::api/redact-value({url: "https://api.legiscan.com/?key=abc&op=getBill"}, "abc")
// {url: "https://api.legiscan.com/?key=REDACTED&op=getBill"}

request

fn (url: Str, api-key: Str?): ::hot::http/HttpResponse

GET url, returning the HttpResponse on 2xx or an Err carrying HttpError. A transport failure is an Err with its text redacted, since the client names the URL in it.

Example

::a ::legiscan::api
cfg ::a/resolve({})
response ::a/request(::a/operation-url(cfg, "getSessionList", {state: "CA"}), cfg.api-key)
if(is-err(response), err-message(response), response.status)

require-api-key

fn (cfg: Map): Str

The API key from resolved settings, or an Err naming where to set it.

Example

cfg ::legiscan::api/resolve({})
key ::legiscan::api/require-api-key(cfg)
if(is-err(key), err-message(key), "key is set")

resolve

fn (opts: Map): Map

Resolve settings for one call: {api-key, base-url}. Values in opts win over context; an api-key in opts is used for that call only and comes back unchanged. An Err when api-key is in opts but blank or null, or when opts has base-url without api-key.

Example

cfg ::legiscan::api/resolve({api-key: ::hot::ctx/get("tenant.legiscan-key")})

Types

HttpError

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

A failed LegiScan response: a non-2xx status, or an HTTP 200 whose body has status: "ERROR", as LegiScan reports its own errors that way. source is "legiscan"; url the requested URL with the key redacted; alert LegiScan's alert.message, when there is one; retry-after the parsed Retry-After seconds on 429s; message a one-line summary. No field carries the API key.