-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver_metadata.R
268 lines (230 loc) · 8.87 KB
/
server_metadata.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# server endpoint ----
#' Returns the supported openEO API versions
#'
#' The function queries the back-end for its supported versions. The endpoint [/.well-known/openeo](https://openeo.org/documentation/1.0/developers/api/reference.html#operation/connect)
#' is called on the given host URL and the JSON result is coerced into a `tibble`.
#'
#' @param url the URL as string pointing to the base host of the back-end
#'
#' @return a `data.frame` or a `tibble` containing all supported API versions of the back-end
#' @export
api_versions = function(url) {
tryCatch({
if (endsWith(url, "/"))
url = substr(url, 1, nchar(url) - 1)
endpoint = "/.well-known/openeo"
info = request(paste0(url, endpoint))
info = req_headers(info,`Content-Type` = "application/json")
info = req_perform(info)
if (info$status == 200) {
vlist = resp_body_json(info)
if (is.raw(vlist)) {
vlist=jsonlite::fromJSON(rawToChar(vlist),simplifyDataFrame = FALSE)
}
class(vlist) = "VersionsList"
if (isNamespaceLoaded("tibble")) {
return(tibble::as_tibble(vlist))
}
return(as.data.frame(vlist))
} else {
stop("Host is not reachable. Please check the URL.")
}
}, error = .capturedErrorToMessage)
}
#' Capabilities overview
#'
#' The function queries the connected openEO service for general information about the service.
#'
#' @param con A connected OpenEO client (optional), if omitted [active_connection()] is used
#'
#' @return capabilities object
#'
#' @export
capabilities = function(con=NULL) {
tryCatch({
con = .assure_connection(con)
con$stopIfNotConnected()
return(con$getCapabilities())
},
error = function(e) {
warning(e$message)
return(invisible(e$message))
})
}
#' List the openEO endpoints
#'
#' The client queries the version resolved back-end for its endpoint capabilities and returns it as
#' a tibble.
#'
#' @param con A connected openEO client (optional) otherwise [active_connection()]
#' is used.
#'
#' @return `data.frame` or `tibble` (if available)
#'
#' @export
list_features = function(con=NULL) {
tryCatch({
con = .assure_connection(con)
return(con$api.mapping[c("endpoint", "operation", "available")])
}, error = .capturedErrorToMessage)
}
#' Supported Input/Output formats
#'
#' The function queries the openEO service for supported I/O formats as a `FileFormatList` object.
#'
#' @details The `FileFormatList` object is a named list, which is organized into 'input' and 'output'. For each category a different
#' named list with the `FileFormat` is indexed by its format ID.
#'
#' @param con openEO client object (optional) otherwise [active_connection()]
#' is used.
#' @return a FileFormatList object
#' @export
list_file_formats = function(con=NULL) {
tryCatch({
tag = "formats"
con = .assure_connection(con)
# optional sending of bearer otherwise no authentication required
formats = con$request(tag = tag, authorized = con$isLoggedIn())
class(formats) = "FileFormatList"
if (length(formats$input) > 0) {
input_formats = names(formats$input)
modified_input_formats = lapply(input_formats, function(format_name) {
f = formats$input[[format_name]]
f$type = "input"
f$name = format_name
class(f) = "FileFormat"
return(f)
})
names(modified_input_formats) = input_formats
}
if (length(formats$output) > 0) {
output_formats = names(formats$output)
modified_output_formats = lapply(output_formats, function(format_name) {
f = formats$output[[format_name]]
f$type = "output"
f$name = format_name
class(f) = "FileFormat"
return(f)
})
names(modified_output_formats) = output_formats
formats$output = modified_output_formats
}
return(formats)
}, error = .capturedErrorToMessage)
}
#' Returns the web service types of the back-end
#'
#' The function queries the back-end for the supported web service types usable by the client and returns a named list of
#' `ServiceType` indexed by the service type ID. ServiceTypes can be used when creating a supported web service
#' from the user defined process (process graph).
#'
#' @param con a connected openEO client object (optional) otherwise [active_connection()]
#' is used.
#' @return a `ServiceTypeList`
#' @export
list_service_types = function(con=NULL) {
tryCatch({
con = .assure_connection(con)
con$stopIfNotConnected()
tag = "ogc_services"
services = con$request(tag = tag, authorized = con$isLoggedIn())
services_type_names = names(services)
services = lapply(services_type_names, function(service_name) {
service = services[[service_name]]
service$name = service_name
class(service) = "ServiceType"
return(service)
})
names(services) = services_type_names
class(services) = "ServiceTypeList"
return(services)
}, error = .capturedErrorToMessage)
return(services)
}
#' Visualize the terms of service
#'
#' If the service provides information about their terms of service in the capabilities, the function opens a new RStudio
#' viewer panel and visualizes the HTML content of the link.
#'
#' @param con a connected openEO client object (optional) otherwise [active_connection()]
#' is used.
#' @return a list of the link identifying the terms of service from the service capabilities or NULL
#' @export
terms_of_service = function(con = NULL) {
tryCatch({
con = .assure_connection(con)
con$stopIfNotConnected()
capabilities = con$getCapabilities()
sel = lapply(capabilities$links, function(link) {
if (link$rel == "terms-of-service") {
return(link)
} else {
return(NULL)
}
})
sel = as.list(unlist(sel))
if (length(sel) == 0) {
.no_information_by_backend("terms of service")
return(invisible(NULL))
} else {
req = request(sel$href)
req = req_headers(req,`Content-Type`="text/html")
res = req_perform(req)
# the viewer does not render it nicely so redirecting to the internet browser
utils::browseURL(sel$href)
return(invisible(sel))
}
}, error = .capturedErrorToMessage)
}
#' Visualize the privacy policy
#'
#' If the service provides information about their privacy policy in their capabilities, the function opens a browser window
#' to visualize the web page.
#'
#' @param con a connected openEO client object (optional) otherwise [active_connection()]
#' is used.
#' @return a list of the link identifying the privacy policy from the service capabilities or NULL
#' @export
privacy_policy = function(con = NULL) {
tryCatch({
con = .assure_connection(con)
con$stopIfNotConnected()
capabilities = con$getCapabilities()
sel = lapply(capabilities$links, function(link) {
if (link$rel == "privacy-policy") {
return(link)
} else {
return(NULL)
}
})
sel = as.list(unlist(sel))
if (length(sel) == 0) {
.no_information_by_backend("privacy policy")
return(invisible(NULL))
} else {
req = request(sel$href)
req = req_headers(req,`Content-Type`="text/html")
res = req_perform(req)
# the viewer does not render it nicely so redirecting to the internet browser
utils::browseURL(sel$href)
return(invisible(sel))
}
}, error = .capturedErrorToMessage)
}
#' OGC conformance
#'
#' Queries the openEO service for the conformance. As stated in the API it is highly optional and only available if the service
#' wants to achieve full compatibility with OGC API clients. This function queries the /conformance endpoint and returns it results
#' as a list object translated from JSON using the jsonlite package.
#'
#' @param con a connected openEO client object (optional) otherwise [active_connection()]
#' is used.
#' @export
conformance = function(con=NULL) {
tryCatch({
con = .assure_connection(con)
con$stopIfNotConnected()
tag = "ogc_conformance"
return(con$request(tag = tag))
}, error = .capturedErrorToMessage)
}