Skip to content

Commit 7f64bab

Browse files
authored
Merge pull request #4 from eea/develop
Fixes & improvements
2 parents 0f0679c + dfb8f69 commit 7f64bab

File tree

5 files changed

+44
-33
lines changed

5 files changed

+44
-33
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: hdar
22
Type: Package
33
Title: 'REST' API Client for Accessing Data on 'WEkEO HDA V2'
4-
Version: 1.0.2
4+
Version: 1.0.4
55
URL: https://www.wekeo.eu/
66
BugReports: https://github.com/eea/hdar/issues
77
Authors@R: person("Matteo", "Mattiuzzi", email = "matteo.mattiuzzi@eea.europa.eu", role = c("aut", "cre"))

R/client.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Client <- R6::R6Class("Client",
156156
class = "card-header", id = paste0("heading", index),
157157
htmltools::tags$h2(
158158
class = "mb-0",
159-
htmltools::tags$button(feature$title,
159+
htmltools::tags$button(feature$term_id,
160160
class = "btn btn-link",
161161
type = "button",
162162
`data-toggle` = "collapse",

R/paginator.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ Paginator <- R6::R6Class("Paginator",
99
results <- list()
1010
start_index <- 0
1111

12-
params <- list(
13-
"startIndex" = 0,
14-
"itemsPerPage" = items_per_page
15-
)
12+
if (!is.null(limit) && limit < items_per_page) {
13+
items_per_page <- limit
14+
}
1615

1716
tryCatch(
1817
{
@@ -26,7 +25,7 @@ Paginator <- R6::R6Class("Paginator",
2625
}
2726
}
2827

29-
if ((!is.null(limit) && length(results) > limit) ||
28+
if ((!is.null(limit) && length(results) >= limit) ||
3029
length(results) >= resp$properties$totalResults || length(results) == 0) {
3130
break
3231
}
@@ -47,16 +46,17 @@ Paginator <- R6::R6Class("Paginator",
4746
client = NULL,
4847
request_type = NULL,
4948
get_page = function(request, start_index = 0, items_per_page = 10) {
50-
params <- list(
51-
"startIndex" = start_index,
52-
"itemsPerPage" = items_per_page
53-
)
5449

5550
req <- request
5651
if (private$request_type == "POST") {
5752
req <- req %>%
58-
httr2::req_body_json_modify(params)
53+
httr2::req_body_json_modify(startIndex = start_index,
54+
itemsPerPage = items_per_page)
5955
} else {
56+
params <- list(
57+
startIndex = start_index,
58+
itemsPerPage = items_per_page
59+
)
6060
req <- req %>%
6161
httr2::req_url_query(!!!params)
6262
}

R/search_results.R

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ SearchResults <- R6::R6Class("SearchResults",
4646
#' @param selected_indexes Optional; indices of the specific results to download.
4747
#' @param stop_at_failure Optional; controls whether the download process of multiple files should immediately stop upon encountering the first failure.
4848
#' @param force Optional; forces the download even if the file already exists in the specified output directory.
49+
#' @param prompt Optional; enables all user prompts for decisions during file downloads. Defaults to true.
4950
#' @return Nothing returned but downloaded files are saved at the specified location.
5051
#' @export
51-
download = function(output_dir, selected_indexes, stop_at_failure = TRUE, force = FALSE) {
52-
if (self$total_count == 0 || !private$prompt_user_confirmation(self$total_size)) {
52+
download = function(output_dir, selected_indexes, stop_at_failure = TRUE, force = FALSE, prompt = TRUE) {
53+
if (self$total_count == 0 || (prompt && !private$prompt_user_confirmation(self$total_size))) {
5354
return(NULL)
5455
}
5556

@@ -127,24 +128,8 @@ SearchResults <- R6::R6Class("SearchResults",
127128
},
128129
download_resource = function(download_id, output_dir, force = FALSE) {
129130
url <- paste0(private$client$apiUrl, "/dataaccess/download/", download_id)
130-
req <- httr2::request(url) %>%
131-
httr2::req_method("GET")
132-
133131

134-
resp <- private$client$send_request(req, TRUE)
135-
if (resp$status_code != 200) {
136-
stop(paste("Couldn't download: ", url))
137-
}
138-
139-
# Extract the file name from the Content-Disposition header
140-
content_disposition <- httr2::resp_headers(resp, "content-disposition")
141-
filename <- if (!is.null(content_disposition)) {
142-
gsub('.*filename="?([^"]+)"?.*', "\\1", content_disposition)
143-
} else {
144-
"downloaded_file"
145-
}
146-
147-
# Define the full file path
132+
filename <- private$check_resource_name(url)
148133
file_path <- file.path(output_dir, filename)
149134

150135
# Check if the file already exists and force flag is FALSE
@@ -153,8 +138,15 @@ SearchResults <- R6::R6Class("SearchResults",
153138
return(NA)
154139
}
155140

156-
writeBin(httr2::resp_body_raw(resp), file_path)
141+
req <- httr2::request(url) %>%
142+
httr2::req_method("GET")
157143

144+
resp <- private$client$send_request(req, TRUE)
145+
if (resp$status_code != 200) {
146+
stop(paste("Couldn't download: ", url))
147+
}
148+
149+
writeBin(httr2::resp_body_raw(resp), file_path)
158150
return(NA)
159151
},
160152
prompt_user_confirmation = function(total_size) {
@@ -172,6 +164,25 @@ SearchResults <- R6::R6Class("SearchResults",
172164
} else {
173165
return(TRUE)
174166
}
167+
},
168+
check_resource_name = function(url) {
169+
req <- httr2::request(url) %>%
170+
httr2::req_method("HEAD")
171+
172+
resp <- private$client$send_request(req, TRUE)
173+
if (resp$status_code != 200) {
174+
stop(paste("Couldn't download: ", url))
175+
}
176+
177+
# Extract the file name from the Content-Disposition header
178+
content_disposition <- httr2::resp_headers(resp, "content-disposition")
179+
filename <- if (!is.null(content_disposition)) {
180+
gsub('.*filename="?([^"]+)"?.*', "\\1", content_disposition)
181+
} else {
182+
"downloaded_file"
183+
}
184+
185+
return(filename)
175186
}
176187
)
177188
)

vignettes/hdar.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ The basic usage of the datasets method is straightforward. You can retrieve a li
129129
all_datasets <- client$datasets()
130130
131131
# list all datasets IDs on WEkEO
132-
sapply(filtered_datasets,FUN = function(x){x$dataset_id})
132+
sapply(all_datasets,FUN = function(x){x$dataset_id})
133133
```
134134

135135
## Filtering Datasets

0 commit comments

Comments
 (0)