Title: | Client for the 'DKAN' API |
---|---|
Description: | Provides functions to facilitate access to the 'DKAN' API (<https://dkan.readthedocs.io/en/latest/apis/index.html>), including the 'DKAN' REST API (metadata), and the 'DKAN' datastore API (data). Includes functions to list, create, retrieve, update, and delete datasets and resources nodes. It also includes functions to search and retrieve data from the 'DKAN' datastore. |
Authors: | Tony Fujs [aut, cre], Meera Desai [aut], Karthik Ramanathan [aut] |
Maintainer: | Tony Fujs <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.3 |
Built: | 2025-01-14 06:22:01 UTC |
Source: | CRAN |
Attach a file to an existing resource node
attach_file_to_node(nid, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), file_path, attach = 1)
attach_file_to_node(nid, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), file_path, attach = 1)
nid |
character: DKAN resource node ID |
url |
character: The DKAN site URL |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
file_path |
character: path to file on local machine |
attach |
numeric: Setting the attach parameter to 0 ensures that the file will replace any existing file on the resource. Setting it to 1 will result in a rejected request if the resource already has an attached file (but it will work if the resource file upload field is empty). |
Create a DKAN node
create_node(url = get_url(), body, credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
create_node(url = get_url(), body, credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
url |
character: The DKAN site URL |
body |
json: A JSON object to be passed to the body of the request |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
as |
character: Output format. Options are: 'json' or 'list' |
... |
Other optional parameters passed to the underlying GET request |
dkan_node object
## Not run: create_node(url = "http://demo.getdkan.com", body = {"title": "TEST DATASET"}) ## End(Not run)
## Not run: create_node(url = "http://demo.getdkan.com", body = {"title": "TEST DATASET"}) ## End(Not run)
Delete an existing DKAN node
delete_node(nid, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
delete_node(nid, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
nid |
character: DKAN node ID |
url |
character: The DKAN site URL |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
as |
character: Output format. Options are: 'json' or 'list' |
... |
Other optional parameters passed to the underlying GET request |
character or dkan_node object
## Not run: delete_node(nid = 1, url = "http://demo.getdkan.com") ## End(Not run)
## Not run: delete_node(nid = 1, url = "http://demo.getdkan.com") ## End(Not run)
Close connection with DKAN API initiated by dkanr_setup()
dkanr_logout(url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()))
dkanr_logout(url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()))
url |
Character: API root URL. Default value is the url returned by get_url() |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
json
Get or set dkanr DKAN settings
dkanr_settings() get_url() get_cookie() get_token()
dkanr_settings() get_url() get_cookie() get_token()
dkanr_settings
prints your base url, API cookie and token (if used)
dkanr_setup
sets your production settings
dkanr_settings()
dkanr_settings()
Configure default DKAN settings
dkanr_setup(url = "http://demo.getdkan.com", username = NULL, password = NULL)
dkanr_setup(url = "http://demo.getdkan.com", username = NULL, password = NULL)
url |
A DKAN URL (optional), default: "http://demo.getdkan.com" |
username |
Username (optional, character) |
password |
Password (optional, character) |
dkanr_setup
sets DKAN connection details. dkanr
functions
default to use the default URL and API key unless specified explicitly.
## Not run: DKAN users without admin/editor privileges could run: dkanr_setup(url = "http://demo.getdkan.com") Privileged DKAN editor/admin users can run: dkanr_setup(url = "http://demo.getdkan.com", username = "your-username", password = "your-password") Not specifying the default DKAN URL will reset the DKAN URL to its default "http://demo.getdkan.com": dkanr_setup() ## End(Not run)
## Not run: DKAN users without admin/editor privileges could run: dkanr_setup(url = "http://demo.getdkan.com") Privileged DKAN editor/admin users can run: dkanr_setup(url = "http://demo.getdkan.com", username = "your-username", password = "your-password") Not specifying the default DKAN URL will reset the DKAN URL to its default "http://demo.getdkan.com": dkanr_setup() ## End(Not run)
Returns TRUE is a data file has been imported in the DKAN datastore, returns FALSE otherwise
ds_is_available(metadata)
ds_is_available(metadata)
metadata |
list: Resource level metadata |
logical vector
## Not run: metadata <- retrieve_node(nid ='140366', as = 'list') ds_is_available(metadata) ## End(Not run)
## Not run: metadata <- retrieve_node(nid ='140366', as = 'list') ds_is_available(metadata) ## End(Not run)
Search csv resource file stored in the datastore.
ds_search(resource_id, fields = NULL, filters = NULL, limit = 100, offset = 0, sort = NULL, q = NULL, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()))
ds_search(resource_id, fields = NULL, filters = NULL, limit = 100, offset = 0, sort = NULL, q = NULL, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()))
resource_id |
character: DKAN resource ID |
fields |
character vector: fields to be returned by the request |
filters |
named character vector: filters to be applied when retrieving records |
limit |
numeric: Number of records to retrieve |
offset |
numeric: Number of results to skip in the beginning |
sort |
named character vector: field to be sorted by |
q |
character: full text query filter |
url |
character: The DKAN site URL |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
data.frame or character
## Not run: ds_search(resource_id = '10c578a6-63c4-40bd-a55d-0c27bf276283', fields = c('Country','City','Region','Population'), filters = list('Country'=c('co','my'), 'Region'=c('04','09','22')), limit = 20, offset = 0, sort = c('Country'='asc'), q = "puertica", url = get_url(), as = 'df') ## End(Not run)
## Not run: ds_search(resource_id = '10c578a6-63c4-40bd-a55d-0c27bf276283', fields = c('Country','City','Region','Population'), filters = list('Country'=c('co','my'), 'Region'=c('04','09','22')), limit = 20, offset = 0, sort = c('Country'='asc'), q = "puertica", url = get_url(), as = 'df') ## End(Not run)
Search csv resource file stored in the datastore.
ds_search_all(resource_id, fields = NULL, filters = NULL, num_records = NULL, offset = 0, sort = NULL, q = NULL, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "df")
ds_search_all(resource_id, fields = NULL, filters = NULL, num_records = NULL, offset = 0, sort = NULL, q = NULL, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "df")
resource_id |
character: DKAN resource ID |
fields |
character vector: fields to be returned by the request |
filters |
named character vector: filters to be applied when retrieving records |
num_records |
numeric: Number of records to retrieve |
offset |
numeric: Number of results to skip in the beginning |
sort |
named character vector: field to be sorted by and the order of sorting |
q |
character: full text query filter |
url |
character: The DKAN site URL |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
as |
character: output format - json, list or data frame |
character, list, or data frame
## Not run: ds_search_all(resource_id = '10c578a6-63c4-40bd-a55d-0c27bf276283', fields = c('Country','City','Region','Population'), filters = list('Country'=c('co','my'), 'Region'=c('04','09','22')), num_records = 20, offset = 0, limit = 100, sort = c('Country'='asc'), q = "puertica", url = get_url(), as = 'df') ## End(Not run)
## Not run: ds_search_all(resource_id = '10c578a6-63c4-40bd-a55d-0c27bf276283', fields = c('Country','City','Region','Population'), filters = list('Country'=c('co','my'), 'Region'=c('04','09','22')), num_records = 20, offset = 0, limit = 100, sort = c('Country'='asc'), q = "puertica", url = get_url(), as = 'df') ## End(Not run)
Helper function to capture error messages returned by the API
err_handler(x)
err_handler(x)
x |
response: The API response |
Helper function to build API query string
filters_to_text_query(filters, text)
filters_to_text_query(filters, text)
filters |
named vector: filters to be applied |
text |
vector: The key word used for filtering |
Returns all the resource node ids attached to a specific node id
get_resource_nids(metadata)
get_resource_nids(metadata)
metadata |
list: Dataset level metadata |
character vector
## Not run: metadata <- retrieve_node(nid ='140177', as = 'list') get_resource_nids(metadata) ## End(Not run)
## Not run: metadata <- retrieve_node(nid ='140177', as = 'list') get_resource_nids(metadata) ## End(Not run)
Returns the download link or external page URL for a specific resource
get_resource_url(metadata)
get_resource_url(metadata)
metadata |
list: Resource level metadata |
character vector
## Not run: metadata <- retrieve_node(nid ='140366', as = 'list') get_resource_url(metadata) ## End(Not run)
## Not run: metadata <- retrieve_node(nid ='140366', as = 'list') get_resource_url(metadata) ## End(Not run)
dkan_list class helpers
is.dkan_list(x)
is.dkan_list(x)
x |
Variety of things, character, list, or dkan_list class object as.dkan_list <- function(x, ...) UseMethod("as.dkan_list") #' @export as.dkan_list.character <- function(x, ...) retrieve_node(x, ...) #' @export as.dkan_list.dkan_list <- function(x, ...) x #' @export as.dkan_list.list <- function(x, ...) structure(x, class = "dkan_list") |
dkan_node class helpers
is.dkan_node(x)
is.dkan_node(x)
x |
Variety of things, character, list, or dkan_node class object # create item class from only an item ID as.dkan_node("22") # gives back itself (x <- as.dkan_node("22")) as.dkan_node(x) as.dkan_node <- function(x, ...) UseMethod("as.dkan_node") #' @export as.dkan_node.character <- function(x, ...) retrieve_node(x, ...) #' @export as.dkan_node.dkan_node <- function(x, ...) x #' @export as.dkan_node.list <- function(x, ...) structure(x, class = "dkan_node") |
Returns list of nodes
list_nodes(url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), fields = NULL, filters = NULL, pagesize = NULL, page = NULL, as = "json", ...)
list_nodes(url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), fields = NULL, filters = NULL, pagesize = NULL, page = NULL, as = "json", ...)
url |
character: The DKAN site URL |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
fields |
character vector: fields to be returned by the request |
filters |
named character vector: filters to be applied to the search |
pagesize |
numeric: Number of records to get per page (max = 20). |
page |
numeric: The zero-based index of the page to get, defaults to 0. |
as |
character: Output format. Options are: 'json', 'list', or 'df' (data frame) |
... |
Other optional parameters to be passed to the underlying GET request |
json, dkan_node (list), or data frame
## Not run: list_nodes(url = "http://demo.getdkan.com") list_nodes(url = "http://demo.getdkan.com", fields = c('nid', 'type', 'uri'), filters = c(type='resource')) ## End(Not run)
## Not run: list_nodes(url = "http://demo.getdkan.com") list_nodes(url = "http://demo.getdkan.com", fields = c('nid', 'type', 'uri'), filters = c(type='resource')) ## End(Not run)
Returns list of nodes
list_nodes_all(url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), fields = NULL, filters = NULL, as = "df", ...)
list_nodes_all(url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), fields = NULL, filters = NULL, as = "df", ...)
url |
character: The DKAN site URL |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
fields |
character vector: fields to be returned by the request |
filters |
named character vector: filters to be applied to the search |
as |
character: Output format. Options are: 'json', 'list' or 'df' (data frame) |
... |
Other optional parameters to be passed to the underlying GET request |
json, dkan_node (list), or data frame
## Not run: list_nodes_all(url = "http://demo.getdkan.com", fields = c('nid', 'type'), filters = c(type = 'resource')) ## End(Not run)
## Not run: list_nodes_all(url = "http://demo.getdkan.com", fields = c('nid', 'type'), filters = c(type = 'resource')) ## End(Not run)
Returns metadata associated with an existing DKAN Node ID.
retrieve_node(nid, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
retrieve_node(nid, url = get_url(), credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
nid |
character: DKAN node ID |
url |
character: The DKAN site URL |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
as |
character: Output format. Options are: 'json' or 'list' |
... |
Other optional parameters passed to the underlying GET request |
dkan_node object
## Not run: retrieve_node(nid = 1, url = "http://demo.getdkan.com") ## End(Not run)
## Not run: retrieve_node(nid = 1, url = "http://demo.getdkan.com") ## End(Not run)
Update an existing DKAN node
update_node(nid, url = get_url(), body, credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
update_node(nid, url = get_url(), body, credentials = list(cookie = dkanr::get_cookie(), token = dkanr::get_token()), as = "json", ...)
nid |
character: DKAN node ID |
url |
character: The DKAN site URL |
body |
json: A JSON object to be passed to the body of the request |
credentials |
Optional list parameter. Default values are Cookie and Token generated by dkan_setup() |
as |
character: Output format. Options are: 'json' or 'list' |
... |
Other optional parameters passed to the underlying GET request |
dkan_node object
## Not run: update_node(nid = 1, url = "http://demo.getdkan.com") ## End(Not run)
## Not run: update_node(nid = 1, url = "http://demo.getdkan.com") ## End(Not run)