Transport for the NPI Registry API: the settings resolver (per-call opts first, then context variables, then defaults), a typed HttpError, JSON GETs that turn the API's Errors body into an Err, and query-string building that drops unset values.

opts keyContext variableDefault
base-urlnppes.base-urlhttps://npiregistry.cms.hhs.gov/api

The API takes no credential, so a per-call base-url needs none.

Context Vars

Optional
  • nppes.base-url

Values

BASE_URL

BASE_URL: Str "https://npiregistry.cms.hhs.gov/api"

PACKAGE_AGENT

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

SOURCE

SOURCE: Str "nppes"

Functions

decode-body

fn (response: ::hot::http/HttpResponse, url: Str): Any

The decoded JSON of a 2xx response, or an Err carrying HttpError when it holds Errors or isn't a JSON object (pure). Exposed for testing.

Example

response ::hot::http/HttpResponse({status: 200, headers: {}, body: "{\"result_count\":0,\"results\":[]}"})
::nppes::api/decode-body(response, "https://npi-proxy.example/api/")  // {result_count: 0, results: []}

get-json

fn (url: Str, headers: Map): Any

GET a JSON resource and return the decoded body, or an Err carrying HttpError.

Example

page ::nppes::api/get-json("https://npiregistry.cms.hhs.gov/api/?version=2.1&number=1881018208", {})
first(page.results).number

given

fn (v: Any): Bool

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

Example

::nppes::api/given("MN")  // true
::nppes::api/given("  ")  // false
::nppes::api/given(null)  // false

http-error

fn (response: ::hot::http/HttpResponse, url: Str): HttpError

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

Example

response ::hot::http/HttpResponse({status: 429, headers: {retry-after: "30"}, body: "slow down"})
e ::nppes::api/http-error(response, "https://npiregistry.cms.hhs.gov/api/?version=2.1")
e.retry-after  // 30
e.message  // "nppes: HTTP 429: slow down"

not-json-error

fn (response: ::hot::http/HttpResponse, url: Str): HttpError

An HttpError for a 2xx response that is not a JSON object, such as a maintenance page, which would otherwise read as no such provider. Exposed for testing.

Example

response ::hot::http/HttpResponse({status: 200, headers: {}, body: "<html></html>"})
e ::nppes::api/not-json-error(response, "https://npi-proxy.example/api/")
e.status  // 200
e.body  // null

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

::nppes::api/query-string({version: "2.1", city: null, state: "  "})  // "?version=2.1"
::nppes::api/query-string({city: null})  // ""

request

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

GET (or another method) an NPI Registry URL with headers, returning the HttpResponse on 2xx or an Err carrying HttpError. A transport failure passes through as its own Err.

Example

response ::nppes::api/request("GET", "https://npiregistry.cms.hhs.gov/api/?version=2.1&number=1881018208", {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 ::nppes::api/resolve({base-url: "https://npi-proxy.example/api"})

Types

HttpError

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

A failed NPI Registry response. source is "nppes"; url the requested URL (the API takes no credentials in URLs); retry-after the parsed Retry-After seconds on 429s; message a one-line summary. The API reports bad criteria with status 200 and an Errors body, so status can be 200: body.Errors then lists {description, field, number}. It is also 200 when the site answered with something other than JSON (see decode-body).