Skip to content

Commit e5adfe0

Browse files
committed
Add code for get_document(s)
1 parent 017b5d1 commit e5adfe0

File tree

11 files changed

+111
-124
lines changed

11 files changed

+111
-124
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,4 @@ Remotes:
4040
JBGruber/dockr
4141
License: MIT + file LICENSE
4242
Encoding: UTF-8
43-
RoxygenNote: 7.3.1
43+
RoxygenNote: 7.3.2

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ export(delete_documents)
99
export(delete_index)
1010
export(delete_index_user)
1111
export(delete_user)
12+
export(get_document)
13+
export(get_documents)
1214
export(get_fields)
1315
export(list_index_users)
1416
export(list_indexes)
1517
export(list_users)
1618
export(modify_index)
1719
export(modify_index_user)
1820
export(modify_user)
19-
export(ping)
2021
export(query_aggregate)
2122
export(query_documents)
2223
export(refresh_index)

R/index.R

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,32 @@ get_fields <- function(index, credentials = NULL) {
268268
purrr::list_rbind()
269269
}
270270

271+
272+
#' Retrieve a single document
273+
#'
274+
#' @param index The index to get fields for
275+
#' @param doc_id A single document_id
276+
#' @param fields Optional character vector listing the fields to retrieve
277+
#' @param credentials The credentials to use. If not given, uses last login information
278+
#'
279+
#' @returns A tibble with one row containing the requested fields
280+
#' @export
281+
get_document <- function(index, doc_id, fields, credentials = NULL) {
282+
res <- request(credentials, c("index", index, "documents", doc_id), query=list(fields=fields), query.multi="comma")
283+
tibble::as_tibble(res) |> tibble::add_column(.id=doc_id, .before=1)
284+
}
285+
286+
#' Retrieve multiple documents using a purrr map over get_document
287+
#'
288+
#' @param index The index to get fields for
289+
#' @param doc_ids A vector of document_ids
290+
#' @param fields Optional character vector listing the fields to retrieve
291+
#' @param credentials The credentials to use. If not given, uses last login information
292+
#' @param ...Other options to pass to map, e.g. .progress
293+
#' @returns A tibble with one row containing the requested fields
294+
#' @export
295+
get_documents <- function(index, doc_ids, fields, credentials = NULL, ...) {
296+
purrr::map(doc_ids, function(doc_id) get_document(index, doc_id, fields, credentials), ...) |>
297+
purrr::list_rbind()
298+
}
299+

R/lib.R

Lines changed: 26 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ pkg.env <- new.env()
22

33
#' Helper function to get credentials from argument or pkg.env
44
#' @noRd
5-
get_credentials = function(credentials = NULL) {
5+
get_credentials = function(credentials=NULL) {
66

77
if (is.null(credentials)) {
88
if (!is.null(pkg.env$current_server)) {
99
credentials = amcat_get_token(pkg.env$current_server)
1010
credentials$host <- pkg.env$current_server
11+
credentials$authorization <- pkg.env$authorization
1112
} else {
1213
stop("Please use amcat_login() first")
1314
}
@@ -18,13 +19,13 @@ get_credentials = function(credentials = NULL) {
1819
#' Helper function to execute a request to this API
1920
#' @noRd
2021
request_response <- function(credentials,
21-
url,
22-
method = "GET",
23-
body = NULL,
24-
error_on_404 = TRUE,
25-
max_tries = NULL,
26-
auto_unbox = TRUE,
27-
...) {
22+
url,
23+
method = "GET",
24+
body = NULL,
25+
query = NULL,
26+
query.multi = "error",
27+
error_on_404 = TRUE,
28+
...) {
2829

2930
# current httr2 version has a bug in req_url_path that can't handle objects of
3031
# length != 1, already fixed on gh
@@ -40,23 +41,20 @@ request_response <- function(credentials,
4041
error_on_404 ,
4142
httr2::resp_status(resp) >= 400),
4243
body = amcat_error_body
43-
) |>
44-
# for uploads, we sometimes get 500/502 when elastic is processing new documents
45-
# in these cases amcat4 reports a server error because the connection times out.
46-
# It makes sense to wait a little and retry
47-
httr2::req_retry(max_tries = max_tries,
48-
is_transient = function(x) httr2::resp_status(x) %in% c(429, 500, 502, 503))
44+
)
4945

46+
if (!is.null(query)) {
47+
req <- do.call(httr2::req_url_query, c(list(req), .multi=query.multi, query))
48+
}
5049
if (!is.null(body)) {
5150
req <- req |>
52-
httr2::req_body_json(body, auto_unbox = auto_unbox)
51+
httr2::req_body_json(body)
5352
}
5453

5554
if (credentials$authorization != "no_auth") {
5655
req <- req |>
5756
httr2::req_auth_bearer_token(credentials$access_token)
5857
}
59-
6058
httr2::req_perform(req)
6159
}
6260

@@ -87,60 +85,40 @@ make_path <- function(...) {
8785
#' Custom error message for requests
8886
#' @noRd
8987
amcat_error_body <- function(resp) {
90-
88+
# resp <<- resp
9189
if (grepl("json", httr2::resp_content_type(resp), fixed = TRUE)) {
9290
ebody <- httr2::resp_body_json(resp)
93-
94-
if (purrr::pluck_exists(ebody, "message")) {
95-
return(purrr::pluck(ebody, "message"))
96-
} else if (purrr::pluck_exists(ebody, "detail")) {
97-
return(purrr::pluck(ebody, "detail"))
98-
} else if (is.list(ebody$detail$body$error)) {
99-
error <- purrr::map_chr(names(ebody$detail$body$error), function(n) {
100-
paste0(tools::toTitleCase(n), ": ", ebody$detail$body$error[[n]])
101-
})
102-
} else {
103-
# TODO: find a cleaner way to parse this
104-
msg <- try(ebody[["detail"]][[1]][["msg"]], silent = TRUE)
105-
if (methods::is(msg, "try-error")) msg <- NULL
106-
detail <- try(toString(ebody[["detail"]][[1]][["loc"]]), silent = TRUE)
107-
if (methods::is(detail, "try-error")) detail <- toString(ebody[["detail"]])
108-
error <- paste0(msg, detail, .sep = ": ")
109-
}
110-
91+
# TODO: find a cleaner way to parse this
92+
msg <- try(ebody[["detail"]][[1]][["msg"]])
93+
if (methods::is(msg, "try-error")) msg <- NULL
94+
detail <- try(toString(ebody[["detail"]][[1]][["loc"]]))
95+
if (methods::is(detail, "try-error")) detail <- toString(ebody[["detail"]])
96+
error <- c(
97+
ebody$error,
98+
paste0(msg, detail, .sep = ": ")
99+
)
111100
} else {
112-
# if no further information is returned, revert to httr2 default by
113-
# returning NULL
114101
error <- NULL
115102
}
116103

117104
if (httr2::resp_status(resp) == 401)
118105
error <- glue::glue(error, " (hint: see ?amcat_login on how to get a fresh token)")
119-
120106
return(error)
121107
}
122108

123109

124110
#' Helper function to convert date columns in date format
125111
#' @noRd
126112
convert_datecols <- function(df, index) {
127-
type <- NULL
128113
datecols <- dplyr::filter(get_fields(index), type == "date")$name
129114

130-
for (date_col in intersect(colnames(df), datecols)) {
131-
# AmCAT / elastic does not standardize date input/output, so try different formats
132-
# (and maybe complain to whoever is in charge of AmCAT?)
133-
df[[date_col]] <- lubridate::parse_date_time(df[[date_col]], orders=c("ymdHMSz", "ymdHMS", "ymdHM", "ymd"))
134-
}
115+
for (date_col in intersect(colnames(df), datecols))
116+
df[[date_col]] <- strptime(df[[date_col]], format = "%Y-%m-%dT%H:%M:%S")
135117
df
136118
}
137119

138120

139121
#' Truncate id columns when printing
140-
#'
141-
#' @param x id column in a data.frame with amcat4 data.
142-
#' @inheritParams rlang::args_dots_used
143-
#'
144122
#' @export
145123
#' @importFrom pillar pillar_shaft
146124
#' @method pillar_shaft id_col
@@ -154,47 +132,3 @@ pillar_shaft.id_col <- function(x, ...) {
154132
}
155133

156134

157-
#' @title Check if an amcat instance is reachable
158-
#'
159-
#' @description Check if a server is reachable by sending a request to its
160-
#' config endpoint.
161-
#'
162-
#' @param server A character string of the server URL. If missing the server for
163-
#' the logged in session is tried.
164-
#'
165-
#' @return A logical value indicating if the server is reachable.
166-
#'
167-
#' @export
168-
#'
169-
#' @examples
170-
#' \dontrun{
171-
#' ping("http://localhost/amcat")
172-
#' }
173-
ping <- function(server) {
174-
if (missing(server)) server <- pkg.env$current_server
175-
tryCatch({
176-
httr2::request(server) |>
177-
httr2::req_url_path_append("config") |>
178-
httr2::req_error(is_error = function(resp) FALSE) |>
179-
httr2::req_perform() |>
180-
(\(resp) !is.null(httr2::resp_body_json(resp)$resource))()
181-
}, error = function(resp) FALSE)
182-
}
183-
184-
185-
#' Helper function to safely turn results into a tibble without unnesting list fields
186-
#' @noRd
187-
safe_bind_rows <- function(l) {
188-
purrr::map(l, function(tbl) {
189-
purrr::map(tbl, function(c) {
190-
if (is.list(c) & length(c) > 1) {
191-
return(list(c))
192-
} else {
193-
return(c)
194-
}
195-
}) |>
196-
tibble::as_tibble()
197-
}) |>
198-
dplyr::bind_rows()
199-
}
200-

amcat4r.Rproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Version: 1.0
2+
ProjectId: 0ca4ea7c-2377-4438-8752-161a6ff85428
23

34
RestoreWorkspace: Default
45
SaveWorkspace: Default

man/create_index.Rd

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_document.Rd

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/get_documents.Rd

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/pillar_shaft.id_col.Rd

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ping.Rd

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)