@@ -2,12 +2,13 @@ pkg.env <- new.env()
2
2
3
3
# ' Helper function to get credentials from argument or pkg.env
4
4
# ' @noRd
5
- get_credentials = function (credentials = NULL ) {
5
+ get_credentials = function (credentials = NULL ) {
6
6
7
7
if (is.null(credentials )) {
8
8
if (! is.null(pkg.env $ current_server )) {
9
9
credentials = amcat_get_token(pkg.env $ current_server )
10
10
credentials $ host <- pkg.env $ current_server
11
+ credentials $ authorization <- pkg.env $ authorization
11
12
} else {
12
13
stop(" Please use amcat_login() first" )
13
14
}
@@ -18,13 +19,13 @@ get_credentials = function(credentials = NULL) {
18
19
# ' Helper function to execute a request to this API
19
20
# ' @noRd
20
21
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
+ ... ) {
28
29
29
30
# current httr2 version has a bug in req_url_path that can't handle objects of
30
31
# length != 1, already fixed on gh
@@ -40,23 +41,20 @@ request_response <- function(credentials,
40
41
error_on_404 ,
41
42
httr2 :: resp_status(resp ) > = 400 ),
42
43
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
+ )
49
45
46
+ if (! is.null(query )) {
47
+ req <- do.call(httr2 :: req_url_query , c(list (req ), .multi = query.multi , query ))
48
+ }
50
49
if (! is.null(body )) {
51
50
req <- req | >
52
- httr2 :: req_body_json(body , auto_unbox = auto_unbox )
51
+ httr2 :: req_body_json(body )
53
52
}
54
53
55
54
if (credentials $ authorization != " no_auth" ) {
56
55
req <- req | >
57
56
httr2 :: req_auth_bearer_token(credentials $ access_token )
58
57
}
59
-
60
58
httr2 :: req_perform(req )
61
59
}
62
60
@@ -87,60 +85,40 @@ make_path <- function(...) {
87
85
# ' Custom error message for requests
88
86
# ' @noRd
89
87
amcat_error_body <- function (resp ) {
90
-
88
+ # resp <<- resp
91
89
if (grepl(" json" , httr2 :: resp_content_type(resp ), fixed = TRUE )) {
92
90
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
+ )
111
100
} else {
112
- # if no further information is returned, revert to httr2 default by
113
- # returning NULL
114
101
error <- NULL
115
102
}
116
103
117
104
if (httr2 :: resp_status(resp ) == 401 )
118
105
error <- glue :: glue(error , " (hint: see ?amcat_login on how to get a fresh token)" )
119
-
120
106
return (error )
121
107
}
122
108
123
109
124
110
# ' Helper function to convert date columns in date format
125
111
# ' @noRd
126
112
convert_datecols <- function (df , index ) {
127
- type <- NULL
128
113
datecols <- dplyr :: filter(get_fields(index ), type == " date" )$ name
129
114
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" )
135
117
df
136
118
}
137
119
138
120
139
121
# ' 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
- # '
144
122
# ' @export
145
123
# ' @importFrom pillar pillar_shaft
146
124
# ' @method pillar_shaft id_col
@@ -154,47 +132,3 @@ pillar_shaft.id_col <- function(x, ...) {
154
132
}
155
133
156
134
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
-
0 commit comments