Transport for the GDELT DOC 2.0 API: the settings resolver (per-call
opts first, then context variables, then defaults), typed
HttpError and QueryError, a JSON GET that detects GDELT's plain
text errors, and query-string building that drops unset values.
| opts key | Context variable | Default |
|---|---|---|
base-url | gdelt.base-url | https://api.gdeltproject.org/api/v2/doc/doc |
GDELT takes no credential, so a per-call base-url needs none.
Context Vars
gdelt.base-url
Values
BASE_URL
BASE_URL: Str "https://api.gdeltproject.org/api/v2/doc/doc"
PACKAGE_AGENT
PACKAGE_AGENT: Str "hot-dev-gdelt-pkg/0.1 (+https://hot.dev)"
SOURCE
SOURCE: Str "gdelt"
Functions
get-json
fn (url: Str, headers: Map): Any
GET a JSON resource and return the decoded body, or an Err carrying
HttpError or QueryError.
Example
body ::gdelt::api/get-json("https://api.gdeltproject.org/api/v2/doc/doc?query=climate&mode=ArtList&format=json", {})
::gdelt/article-list(body)
given
fn (v: Any): Bool
True for a non-null value that is not a blank string.
Example
::gdelt::api/given(" ") // false
::gdelt::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: 429, headers: {}, body: "Please limit requests"}
e ::gdelt::api/http-error(response, "https://api.gdeltproject.org/api/v2/doc/doc")
e.status // 429
e.message // "gdelt: HTTP 429: Please limit requests"
parse-body
fn (body: Any, url: Str): Any
The decoded body of a 2xx response (pure). A Map or Vec passes
through and JSON text is decoded. An Err carrying QueryError for
anything else, or for JSON that doesn't parse.
Example
url "https://api.gdeltproject.org/api/v2/doc/doc"
::gdelt::api/parse-body("{\"articles\": []}", url) // {articles: []}
is-err(::gdelt::api/parse-body("Your query was too short.", url)) // true
query-error
fn (text: Str, url: Str): QueryError
Build a QueryError from GDELT's text. Exposed for testing.
Example
e ::gdelt::api/query-error("Your query was too short.", "https://api.gdeltproject.org/api/v2/doc/doc")
e.text // "Your query was too short."
e.message // "gdelt: Your query was too short."
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
::gdelt::api/query-string({mode: "ArtList", format: "json", sort: null}) // "?mode=ArtList&format=json"
::gdelt::api/query-string({sort: " "}) // ""
request
fn (method: Str, url: Str, headers: Map): ::hot::http/HttpResponse
GET a GDELT URL with headers, returning the HttpResponse on 2xx
or an Err carrying HttpError. A transport failure passes through
as its own Err.
Example
response ::gdelt::api/request(
"GET",
"https://api.gdeltproject.org/api/v2/doc/doc?query=climate&mode=ArtList&format=json",
{Accept: "application/json"}
)
response.status
resolve
fn (opts: Map): Map
Resolve settings for one call: {base-url}. A value in opts wins
over context.
Example
cfg ::gdelt::api/resolve({base-url: "https://gdelt-proxy.example/doc"})
Types
HttpError
HttpError type {
source: Str,
status: Int,
url: Str,
headers: Map,
body: Any,
retry-after: Int?,
message: Str
}
A non-2xx GDELT response. source is "gdelt"; url the requested
URL (GDELT takes no credentials); retry-after the parsed
Retry-After seconds on 429s, when GDELT sends one; message a
one-line summary.
QueryError
QueryError type {
source: Str,
url: Str,
text: Str,
message: Str
}
A 2xx GDELT response that is not JSON, which is how GDELT is reported
to reject a query. source is "gdelt";
url the requested URL; text GDELT's whole message; message a
one-line summary.