Transport for feeds: the settings resolver (per-call opts first, then context variables, then defaults), a typed HttpError, the conditional GET, and decoding a response body to text.

opts keyContext variableDefault
user-agentfeed.user-agenthot-dev-feed-pkg/0.1 (+https://hot.dev)

Per-call User-Agent. user-agent in opts is masked in call records and never written to context, because some publishers (sec.gov among them) want a contact email in it. Present but blank or null, it is an error, never a fallback to context.

Context Vars

Optional
  • feed.user-agent

Values

ACCEPT

ACCEPT: Str "application/rss+xml, application/atom+xml, application/xml;q=0.9, text/xml;q=0.8, */*;q=0.5"

PACKAGE_AGENT

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

SOURCE

SOURCE: Str "feed"

Functions

declared-encoding

fn (bytes: Bytes): Str

The lower-cased encoding of an XML declaration at the start of bytes, or "utf-8" when there is none (pure).

Example

::feed::api/declared-encoding(Bytes("<?xml version='1.0' encoding='ISO-8859-1'?><rss/>"))  // "iso-8859-1"
::feed::api/declared-encoding(Bytes("<rss/>"))                                             // "utf-8"

decode

fn (body: Str | Bytes): Str

A feed body as text (pure). A Str comes back unchanged. Bytes are read in the encoding their XML declaration names: UTF-8 (the default), US-ASCII or ISO-8859-1, which EDGAR's feeds declare. Any other encoding, or bytes invalid in the declared one, is an Err.

Example

::feed::api/decode("<rss/>")                               // "<rss/>"
::feed::api/decode(Bytes([60, 114, 115, 115, 47, 62]))     // "<rss/>"

given

fn (v: Any): Bool

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

Example

::feed::api/given("  ")  // false
::feed::api/given("x")   // 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

response {status: 404, headers: {}, body: "Not Found"}
e ::feed::api/http-error(response, "https://example.com/feed.xml")
e.status   // 404
e.message  // "feed: HTTP 404: Not Found"

latin1-to-str

fn (bytes: Bytes): Str

ISO-8859-1 bytes as text (pure): every byte is the code point of the same value, so each one above 127 becomes two UTF-8 bytes.

Example

::feed::api/latin1-to-str(Bytes([99, 97, 102, 233]))  // "café"

request

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

GET a feed URL with headers, returning the HttpResponse on 2xx or 304, or an Err carrying HttpError. A transport failure passes through as its own Err.

Example

response ::feed::api/request(
  "https://www.sec.gov/news/pressreleases.rss",
  {User-Agent: "Acme Corp ops@acme.example"}
)
response.status

request-headers

fn (cfg: Map, opts: Map): Map

The headers for one fetch (pure): User-Agent, Accept, and If-None-Match / If-Modified-Since from opts.etag and opts.last-modified when they are set.

Example

headers ::feed::api/request-headers({user-agent: "Acme Corp ops@acme.example"}, {etag: "abc"})
get(headers, "User-Agent")     // "Acme Corp ops@acme.example"
get(headers, "If-None-Match")  // "abc"

resolve

fn (opts: Map): Map

Resolve settings for one call: {user-agent}. A value in opts wins over context; it is used for that call only. An Err when user-agent is in opts but blank or null.

Example

cfg ::feed::api/resolve({user-agent: "Acme Corp ops@acme.example"})

Types

HttpError

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

A non-2xx feed response other than 304. source is "feed"; url the requested URL, as given; retry-after the parsed Retry-After seconds on 429s; message a one-line summary that never includes the User-Agent.