class_path <- new_class(
name = "class_path",
properties = list(
head = new_property(
class_character,
default = "",
setter = function(self, value) {
self@head <- value
self
}),
guid = new_property(
NULL | class_character,
setter = function(self, value) {
self@guid <- value
self
}),
uuid = new_property(
NULL | class_character,
setter = function(self, value) {
self@uuid <- value
self
}),
tail = new_property(
class_character,
default = "",
setter = function(self, value) {
self@tail <- value
self
})
),
validator = function(self) {
if (length(self@head) != 1L) "@head must be length 1"
if (length(self@tail) != 1L) "@tail must be length 1"
if (not_null(self@guid)) {
if (length(self@guid) != 1L) "@guid must be length 1"
if (!grepl("(?:[0-9a-fA-F]){8}-?(?:[0-9a-fA-F]){4}-?(?:[0-9a-fA-F]){4}-?(?:[0-9a-fA-F]){4}-?(?:[0-9a-fA-F]){12}", self@guid)) "@guid invalid"
}
if(not_null(self@uuid)) {
if (length(self@guid) != 1L) "@uuid must be length 1"
if (nchar(self@uuid) == 9 & !grepl("(?:[0-9a-z]){4}-?(?:[0-9a-zA-F]){4}", self@uuid)) "@uuid invalid"
}
}
)
3 URL Object
GUID
GUID is an acronym for Globally Unique Identifier and used for resource identification. The term is generally used instead of UUID when working with Microsoft technologies.
class_path
new_path <- \(url) {
url <- adaR::ada_get_pathname(url = url)
class_path(
head = stringi::stri_extract(
url,
regex = "^.*(?=[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12})"),
guid = stringi::stri_extract(
url,
regex = "(?:[0-9a-fA-F]){8}-?(?:[0-9a-fA-F]){4}-?(?:[0-9a-fA-F]){4}-?(?:[0-9a-fA-F]){4}-?(?:[0-9a-fA-F]){12}"),
tail = stringi::stri_extract(
url,
regex = "(?<=[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}).*$")
)
}
new_path(urls[1])
<class_path>
@ head: chr "/data-api/v1/dataset/"
@ guid: chr "9887a515-7552-4693-bf58-735c77af46d7"
@ uuid: NULL
@ tail: chr "/data/stats"
new_path(urls[2])
<class_path>
@ head: chr "/data-api/v1/dataset/"
@ guid: chr "9887a515-7552-4693-bf58-735c77af46d7"
@ uuid: NULL
@ tail: chr "/data-viewer"
new_path(urls[3])
<class_path>
@ head: chr NA
@ guid: chr NA
@ uuid: NULL
@ tail: chr NA
class_url
new_url <- \(url) {
class_url(
scheme = ada_get_protocol(url),
hostname = ada_get_host(url),
path = new_path(url),
query = list()
)
}
new_url(urls[1])
<class_url>
@ scheme : chr "https:"
@ hostname: chr "data.cms.gov"
@ path : <class_path>
.. @ head: chr "/data-api/v1/dataset/"
.. @ guid: chr "9887a515-7552-4693-bf58-735c77af46d7"
.. @ uuid: NULL
.. @ tail: chr "/data/stats"
@ query : list()
new_url(urls[2])
<class_url>
@ scheme : chr "https:"
@ hostname: chr "data.cms.gov"
@ path : <class_path>
.. @ head: chr "/data-api/v1/dataset/"
.. @ guid: chr "9887a515-7552-4693-bf58-735c77af46d7"
.. @ uuid: NULL
.. @ tail: chr "/data-viewer"
@ query : list()
new_url(urls[3])
<class_url>
@ scheme : chr "https:"
@ hostname: chr "data.cms.gov"
@ path : <class_path>
.. @ head: chr NA
.. @ guid: chr NA
.. @ uuid: NULL
.. @ tail: chr NA
@ query : list()
Examples
urlparse::url_parse_v2(urls) |>
handle_na() |>
select(path, raw_query)
path
1 /data-api/v1/dataset/9887a515-7552-4693-bf58-735c77af46d7/data/stats
2 /data-api/v1/dataset/9887a515-7552-4693-bf58-735c77af46d7/data-viewer
3 /provider-data/api/1/datastore/query/mj5m-pzi6/0
raw_query
1 <NA>
2 size=5000&offset=10000
3 offset=0&limit=2000
httr2::url_parse(
base_url = "https://data.cms.gov/data-api/v1/dataset",
url = "9887a515-7552-4693-bf58-735c77af46d7/data-viewer") |>
httr2::url_modify_query(
offset = 0,
limit = 2000) |>
httr2::url_modify(path = "data-api/v1/9887a515-7552-4693-bf58-735c77af46d7/data/stats")
Re-setting part of the path:
ex <- class_url(
path = class_path(
head = "data-api/v1/dataset",
guid = "9887a515-7552-4693-bf58-735c77af46d7",
tail = "data-viewer"))
ex
<class_url>
@ scheme : chr "https"
@ hostname: chr "data.cms.gov"
@ path : <class_path>
.. @ head: chr "data-api/v1/dataset"
.. @ guid: chr "9887a515-7552-4693-bf58-735c77af46d7"
.. @ uuid: NULL
.. @ tail: chr "data-viewer"
@ query : list()
ex@path@tail <- "data/stats"
ex
<class_url>
@ scheme : chr "https"
@ hostname: chr "data.cms.gov"
@ path : <class_path>
.. @ head: chr "data-api/v1/dataset"
.. @ guid: chr "9887a515-7552-4693-bf58-735c77af46d7"
.. @ uuid: NULL
.. @ tail: chr "data/stats"
@ query : list()
With httr2:
httr2::url_parse(
base_url = "https://data.cms.gov/provider-data/",
url = "api/1/datastore/query/mj5m-pzi6/0") |>
httr2::url_modify_query(
offset = 0,
limit = 2000)
With S7:
ex2 <- class_url(
path = class_path(
head = "/provider-data/api/1/datastore/query/",
uuid = "mj5m-pzi6",
tail = "0"))
ex2
<class_url>
@ scheme : chr "https"
@ hostname: chr "data.cms.gov"
@ path : <class_path>
.. @ head: chr "/provider-data/api/1/datastore/query/"
.. @ guid: NULL
.. @ uuid: chr "mj5m-pzi6"
.. @ tail: chr "0"
@ query : list()