-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAthens_ReportingAPI.R
More file actions
74 lines (61 loc) · 2.2 KB
/
OpenAthens_ReportingAPI.R
File metadata and controls
74 lines (61 loc) · 2.2 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
######
# TITLE: How to get statistical data from OpenAthens using R
#
# More information on fetching statistical reports from the OpenAthens reporting API can be found here:
# https://docs.openathens.net/libraries/fetching-statistics-reports-via-the-api
######
### Setup ###
# Load Packages
{
library(tidyverse)
library(httr)
library(jsonlite)
}
# Create "output" sub-directory if it doesn't exist
if (!dir.exists("output")) {
dir.create("output")
}
# Get the configuration info
if (!file.exists("config.csv")) {
writeLines("You need to create and populate config.csv!\nSee: config_EXAMPLE.csv")
} else {
config <- read_csv("config.csv",
locale = locale(encoding = "UTF-8"),
trim_ws=TRUE)
for (i in 1:ncol(config)) {
assign(names(config)[i], as.character(config[,i]))
}
rm(i)
}
### Setup End ###
# Call the API to tell it to prepare the report
prepreport <- GET(url = str_interp("${end_point}${domain}/${query}"),
add_headers("Content-Type" = "application/vnd.eduserv.iam.admin.organisation-v1+json",
"Authorization" = str_interp("OAApiKey ${api_key}")
)
)
if (status_code(prepreport) != 200) {
print(
paste("Error code",status_code(prepreport))
)
} else {
# Next we need the "href" value from the successful call to do another call to get the statistical data
# (The initial response is in JSON format, so we dig down until we find "href")
reportURL <- content(prepreport)$links %>%
data.frame()
reportURL <- reportURL[1,1]
# Wait a bit in case it takes some time for OpenAthens to create the report
Sys.sleep(15)
# Call the API again
result <- GET(url = reportURL,
add_headers("Content-Type" = "application/vnd.eduserv.iam.admin.organisation-v1+json",
"Authorization" = str_interp("OAApiKey ${api_key}")
)
)
# Convert the body to a dataframe
df <- read_csv(content(result, "text"))
# write the results to a .csv file
write_csv(df, file=paste0("output/OpenAthensReport_",
format(Sys.time(), "%Y-%m-%d_%H.%M"),
".csv"))
}