Skip to content

Commit

Permalink
Merge pull request #25 from Public-Health-Scotland/use_httr_modify_url
Browse files Browse the repository at this point in the history
Use `httr::modify_url()` in favour of custom code
  • Loading branch information
csillasch authored Apr 17, 2024
2 parents a3abb91 + fefd15e commit 29398c3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Suggests:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Config/testthat/edition: 3
URL: https://github.com/Public-Health-Scotland/phsopendata,
https://public-health-scotland.github.io/phsopendata/
Expand Down
2 changes: 1 addition & 1 deletion R/get_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ get_dataset <- function(dataset_name, max_resources = NULL, rows = NULL) {
check_dataset_name(dataset_name)

# define query and try API call
query <- paste0("id=", dataset_name)
query <- list("id" = dataset_name)
content <- try(
phs_GET("package_show", query),
silent = TRUE
Expand Down
3 changes: 1 addition & 2 deletions R/get_resource.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ get_resource <- function(res_id, rows = NULL, row_filters = NULL, col_select = N
query[null_q_field] <- NULL

# fetch the data
q <- paste0(paste0(names(query), "=", query), collapse = "&")
res_content <- phs_GET("datastore_search", q)
res_content <- phs_GET("datastore_search", query)

# if the total number of rows is greater than the
# number of rows fetched
Expand Down
2 changes: 1 addition & 1 deletion R/get_resource_sql.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ get_resource_sql <- function(sql) {
}

# add query field prefix
query <- paste0("sql=", sql)
query <- list("sql" = sql)

# attempt get request
content <- phs_GET("datastore_search_sql", query)
Expand Down
5 changes: 2 additions & 3 deletions R/phs_GET.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#' Send a GET request to the PHS CKAN API
#'
#' @param action The API endpoint you want to use, e.g., "package_show" / "datastore_search".
#' @param query The query to pass to the endpoint defined by the action argument.
#' @inheritParams request_url
#' @param verbose TRUE or FALSE. If TRUE, a success message will be printed to the console.

Check warning on line 4 in R/phs_GET.R

View workflow job for this annotation

GitHub Actions / lint

file=R/phs_GET.R,line=4,col=81,[line_length_linter] Lines should not be more than 80 characters. This line is 91 characters.
#' @return content of a httr::GET request
#'
Expand All @@ -11,7 +10,7 @@ phs_GET <- function(action, query, verbose = FALSE) {

# attempt GET request
response <- httr::GET(
url,
url = url,
user_agent = httr::user_agent(
"https://github.com/Public-Health-Scotland/phsmethods"
)
Expand Down
24 changes: 15 additions & 9 deletions R/request_url.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ request_url <- function(action, query) {
))
}

# return dump URL
base_url <- "https://www.opendata.nhs.scot"

if (action == "dump") {
return(paste0(
"https://www.opendata.nhs.scot/datastore/dump/",
query, "?bom=true"
))
# return dump URL
url <- httr::modify_url(
url = base_url,
path = c("datastore/dump", query),
query = list(bom = "true")
)
} else {
url <- httr::modify_url(
url = base_url,
path = c("api/3/action", action),
query = query
)
}

# return standard API endpoint (i.e., not dump)
return(paste0(
"https://www.opendata.nhs.scot/api/3/action/",
action, "?", query
))
return(url)
}

0 comments on commit 29398c3

Please sign in to comment.