Downloads for the UK list file: a typed HttpError, text bodies
that survive any content type, HEAD-based change detection (ETag and
Last-Modified), and Range-chunked downloads, since the file is
larger than one response (http.max-response-bytes, 16 MiB by
default).
Values
DEFAULT_CHUNK_BYTES
DEFAULT_CHUNK_BYTES: Int 8388608
USER_AGENT
USER_AGENT: Str "hot-dev-uk-sanctions-pkg/0.1 (+https://hot.dev)"
Functions
body-text
fn (response: Any): Str
The response body as text: the decoded string when Hot kept it as one,
otherwise the body bytes decoded as UTF-8 (a CSV served as
application/octet-stream, or a body that happened to parse as JSON).
Example
::uk::sanctions::http/body-text({body: "<Designations/>"}) // "<Designations/>"
::uk::sanctions::http/body-text({}) // ""
byte-ranges
fn (size: Int, chunk: Int): Vec
Split size bytes into inclusive [start, end] ranges of at most
chunk bytes (pure). byte-ranges(10, 4) is [[0, 3], [4, 7], [8, 9]].
Example
::uk::sanctions::http/byte-ranges(10, 4) // [[0, 3], [4, 7], [8, 9]]
get-text
fn (url: Str): Map
fn (url: Str, chunk-bytes: Int): Map
GET the list file as text. A response over http.max-response-bytes
is fetched again in Range chunks of chunk-bytes, which the UK
server supports. If it stops supporting them, the Err says to raise
the cap. Returns {text, etag, last-modified}.
Example
url "https://sanctionslist.fcdo.gov.uk/docs/UK-Sanctions-List.xml"
out ::uk::sanctions::http/get-text(url)
out.etag
length(::uk::sanctions::xml/records(out.text, "Designation"))
// Smaller Range chunks, for a lower http.max-response-bytes
::uk::sanctions::http/get-text(url, 4194304)
head
fn (url: Str): Map
HEAD a list file and return {etag, last-modified, content-length, accept-ranges} (each null when not sent). Refreshes compare these
with the cached version. A conditional GET would save no request: a
changed file over the response cap still needs this HEAD for its size
and Range support.
Example
info ::uk::sanctions::http/head("https://sanctionslist.fcdo.gov.uk/docs/UK-Sanctions-List.xml")
info.etag
info.content-length
header
fn (response: Any, name: Str): Str?
A response header by lower-case name, or null.
Example
::uk::sanctions::http/header({headers: {etag: "abc"}}, "etag") // "abc"
::uk::sanctions::http/header({headers: {}}, "etag") // null
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
response ::hot::http/HttpResponse({status: 503, headers: {}, body: "down"})
e ::uk::sanctions::http/http-error(response, "https://example.com/list.xml")
e.status // 503
e.message // "HTTP 503 from https://example.com/list.xml: down"
is-size-limit-error
fn (result: Result): Bool
True when a request failed because the response exceeded
http.max-response-bytes.
Example
::uk::sanctions::http/is-size-limit-error(err("response exceeds http.max-response-bytes")) // true
::uk::sanctions::http/is-size-limit-error(err("connection refused")) // false
::uk::sanctions::http/is-size-limit-error("<Designations/>") // false
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
url "https://sanctionslist.fcdo.gov.uk/docs/UK-Sanctions-List.xml"
response ::uk::sanctions::http/request("HEAD", url, {}, "")
::uk::sanctions::http/header(response, "etag")
too-large-message
fn (url: Str): Str
The error for a file over http.max-response-bytes that can't be
fetched in Range chunks. Exposed for testing.
Example
msg ::uk::sanctions::http/too-large-message("https://example.com/list.xml")
contains(msg, "does not support Range requests") // true
Types
HttpError
HttpError type {
status: Int,
url: Str,
headers: Map,
body: Any,
message: Str
}
A non-2xx response from the UK list server. url is the requested URL (without credentials); message is a one-line summary.