Requests to OpenSanctions: one request function and a typed HttpError.

Values

USER_AGENT

USER_AGENT: Str "hot-dev-opensanctions-pkg/0.1 (+https://hot.dev)"

Functions

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

url "https://example.test/x"
response ::hot::http/HttpResponse({status: 404, headers: {}, body: "not here"})
e ::opensanctions::http/http-error(response, url)
e.status  // 404
e.message  // "HTTP 404 from https://example.test/x: not here"

request

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

One request with the package User-Agent. Returns the HttpResponse on 2xx, or an Err carrying HttpError (transport failures pass through as their own Err).

Example

::os ::opensanctions

key ::hot::ctx/get("opensanctions.api-key")
response ::opensanctions::http/request(
  "POST",
  ::os/match-url("https://api.opensanctions.org", "default", {limit: 5}),
  ::os/match-headers(key),
  to-json(::os/match-body("Acme Trading", {}))
)
response.body.responses.q.results

Types

HttpError

HttpError type {
    status: Int,
    url: Str,
    headers: Map,
    body: Any,
    message: Str
}

A non-2xx response from OpenSanctions. url is the requested URL (without credentials); message is a one-line summary.