# Backend {#sec-backend}
```{r}
#| label: setup-common-01
#| include: false
source ("includes/_common.R" )
```
## `S7` Classes
### `class_catalog`
```{r}
#| label: class_catalog
class_catalog <- new_class (
name = "class_catalog" ,
properties = list (
groups = class_list),
validator = function (self)
if (! rlang:: is_named (self@ groups)) "all @groups must be named"
)
class_catalog (groups = list (npi = 1 , ccn = "clinicians" ))
try (class_catalog (groups = list (1 , "clinicians" )))
```
## National Downloadable File
```{r}
#| label: url_building
ndf_identifier <- \(x) {
kit:: nswitch (x,
"affiliations" , "27ea-46a8" ,
"clinicians" , "mj5m-pzi6" ,
default = NA_character_ ,
nThread = 4 L)
}
httr2:: url_parse ("https://data.cms.gov/provider-data/api/1/metastore/schemas/dataset/items/" )
url_tmp <- httr2:: url_parse ("https://data.cms.gov/provider-data/api/1/metastore/schemas/dataset/items/" ) |>
httr2:: url_build () |>
httr2:: request ()
httr2:: req_template (
url_tmp,
"/{fname}?show-reference-ids=true" ,
fname = ndf_identifier ("affiliations" ))
httr2:: url_parse ("https://data.cms.gov/provider-data/api/1/metastore/schemas/dataset/items/" ) |>
httr2:: url_build () |>
httr2:: request () |>
httr2:: req_url_path_append (ndf_identifier ("affiliations" )) |>
httr2:: req_url_path_append ("?show-reference-ids=true" ) |>
httr2:: req_perform () |>
httr2:: resp_body_json (check_type = FALSE , simplifyVector = TRUE )
```
```{r}
#| label: meta_ndf
meta_ndf <- \(fname) {
stopifnot (curl:: has_internet ())
x <- request (
paste0 (
"https://data.cms.gov/" ,
"provider-data/api/1/" ,
"metastore/schemas/" ,
"dataset/items/" ,
switch (fname,
affiliations = "27ea-46a8" ,
clinicians = "mj5m-pzi6" ,
stop ("Invalid argument" )),
"?show-reference-ids=true" )) |>
req_perform () |>
resp_body_json (
check_type = FALSE ,
simplifyVector = TRUE )
distro <- gelm (gelm (x, "distribution" ), "identifier" )
y <- request (
paste0 (
"https://data.cms.gov/" ,
"provider-data/api/1/" ,
"datastore/query/" ,
distro,
"?limit=1&offset=100&" ,
"count=true&results=true&" ,
"schema=true&keys=true&" ,
"format=json&rowIds=true" )) |>
req_perform () |>
resp_body_json (
check_type = FALSE ,
simplifyVector = TRUE )
z <- gelm (gelm (gelm (y, "schema" ), distro), "fields" )
list (
title = gelm (x, "title" ),
description = gelm (x, "description" ),
identifier = gelm (gelm (x, "identifier" ), "identifier" ),
distribution = distro,
issued = gelm (x, "issued" ),
modified = gelm (x, "modified" )[[1 ]],
released = gelm (x, "released" ),
dimensions = paste0 (gelm (gelm (y, "count" ), is.numeric), " x " , gelm (gelm (z, "record_number" ), "length" )),
fields = names (z),
site = gelm (x, "landingPage" ),
csv = gelm (gelm (gelm (x, "distribution" ), "data" ), "downloadURL" , m = "df" ),
dictionary = paste0 (
"https://data.cms.gov/provider-data/sites/default/files/data_dictionaries/physician/DOC_Data_Dictionary.pdf"
)
)
}
```
### Examples
```{r}
#| label: meta_affiliations
meta_ndf ("affiliations" ) |>
purse ()
```
```{r}
#| label: meta_clinicians
meta_ndf ("clinicians" ) |>
purse ()
```