-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathget_details.R
58 lines (46 loc) · 1.93 KB
/
get_details.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# =============================================================================
# Get the status of the generation of the export file
# =============================================================================
# Args:
# API_URL: URL for API endpoint for export
# user: API user ID
# password: password for API user
# attempt: number of times the details of the export has been called
#
# Returns into the global environment:
# A data frame that has all the details returned from the server
# about the status of the export
get_details <- function(export_URL,
user,
password) {
load_pkg <- function(x) {
if (!require(x, character.only = TRUE)) {
install.packages(x, repos = 'https://cloud.r-project.org/', dep = TRUE)
}
require(x, character.only = TRUE)
}
# load packages
load_pkg("httr")
load_pkg("jsonlite")
load_pkg("dplyr")
load_pkg("lubridate")
#-- GET EXPORT STATUS DETAILS --#
# Make URL for details
details_query <- paste0(export_URL, "/details")
# Get status of export from server
statusExport <- httr::GET(details_query, authenticate(user, password))
# Get start time of export
request_time <- as.POSIXct(headers(statusExport)$date,
format = "%a, %d %b %Y %H:%M:%S", tz = "GMT")
# convert start time into UTC for standardization with server response time
request_time <- lubridate::with_tz(request_time, "UTC")
# Convert server response in JSON to data frame
export_details <- jsonlite::fromJSON(content(statusExport, as = "text"),
flatten = TRUE)
# add time of last request sent to data frame
export_details$request_time <- request_time
# Time of last update of status from server.
# NOTE: This is not the same as the time the details query was sent
export_details$LastUpdateDate <- lubridate::ymd_hms(export_details$LastUpdateDate, tz = "UTC")
return(export_details)
}