-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Expand file tree
/
Copy pathauth_api.R
More file actions
261 lines (242 loc) · 10.1 KB
/
auth_api.R
File metadata and controls
261 lines (242 loc) · 10.1 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
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
#' Echo Server API
#'
#' Echo Server API
#'
#' The version of the OpenAPI document: 0.1.0
#' Contact: team@openapitools.org
#' Generated by: https://openapi-generator.tech
#'
#' @docType class
#' @title Auth operations
#' @description AuthApi
#' @format An \code{R6Class} generator object
#' @field api_client Handles the client-server communication.
#'
#' @examples
#' \dontrun{
#' #################### TestAuthHttpBasic ####################
#'
#' library(openapi)
#'
#' #To test HTTP basic authentication
#' api_instance <- AuthApi$new()
#'
#' # Configure HTTP basic authorization: http_auth
#' api_instance$api_client$username <- Sys.getenv("USERNAME")
#' api_instance$api_client$password <- Sys.getenv("PASSWORD")
#'
#' # to save the result into a file, simply add the optional `data_file` parameter, e.g.
#' # result <- api_instance$TestAuthHttpBasic(data_file = "result.txt")
#' result <- api_instance$TestAuthHttpBasic()
#' dput(result)
#'
#'
#' #################### TestAuthHttpBearer ####################
#'
#' library(openapi)
#'
#' #To test HTTP bearer authentication
#' api_instance <- AuthApi$new()
#'
#' # Configure HTTP bearer authorization: http_bearer_auth
#' api_instance$api_client$bearer_token <- Sys.getenv("BEARER_TOKEN")
#'
#' # to save the result into a file, simply add the optional `data_file` parameter, e.g.
#' # result <- api_instance$TestAuthHttpBearer(data_file = "result.txt")
#' result <- api_instance$TestAuthHttpBearer()
#' dput(result)
#'
#'
#' }
#' @importFrom R6 R6Class
#' @importFrom base64enc base64encode
#' @export
AuthApi <- R6::R6Class(
"AuthApi",
public = list(
api_client = NULL,
#' @description
#' Initialize a new AuthApi.
#'
#' @param api_client An instance of API client.
initialize = function(api_client) {
if (!missing(api_client)) {
self$api_client <- api_client
} else {
self$api_client <- ApiClient$new()
}
},
#' @description
#' To test HTTP basic authentication
#'
#' @param data_file (optional) name of the data file to save the result
#' @param ... Other optional arguments
#'
#' @return character
TestAuthHttpBasic = function(data_file = NULL, ...) {
local_var_response <- self$TestAuthHttpBasicWithHttpInfo(data_file = data_file, ...)
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
return(local_var_response$content)
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
return(local_var_response)
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
return(local_var_response)
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
return(local_var_response)
}
},
#' @description
#' To test HTTP basic authentication
#'
#' @param data_file (optional) name of the data file to save the result
#' @param ... Other optional arguments
#'
#' @return API response (character) with additional information such as HTTP status code, headers
TestAuthHttpBasicWithHttpInfo = function(data_file = NULL, ...) {
args <- list(...)
query_params <- list()
header_params <- c()
form_params <- list()
file_params <- list()
local_var_body <- NULL
oauth_scopes <- NULL
is_oauth <- FALSE
local_var_url_path <- "/auth/http/basic"
# HTTP basic auth
if (!is.null(self$api_client$username) || !is.null(self$api_client$password)) {
header_params["Authorization"] <- paste("Basic", base64enc::base64encode(charToRaw(paste(self$api_client$username, self$api_client$password, sep = ":"))))
}
# The Accept request HTTP header
local_var_accepts <- list("text/plain")
# The Content-Type representation header
local_var_content_types <- list()
local_var_resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, local_var_url_path),
method = "POST",
query_params = query_params,
header_params = header_params,
form_params = form_params,
file_params = file_params,
accepts = local_var_accepts,
content_types = local_var_content_types,
body = local_var_body,
is_oauth = is_oauth,
oauth_scopes = oauth_scopes,
...)
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
# save response in a file
if (!is.null(data_file)) {
self$api_client$WriteFile(local_var_resp, data_file)
}
deserialized_resp_obj <- tryCatch(
self$api_client$DeserializeResponse(local_var_resp, "character"),
error = function(e) {
stop("Failed to deserialize response")
}
)
local_var_resp$content <- deserialized_resp_obj
return(local_var_resp)
}
local_var_error_msg <- local_var_resp$response_as_text()
if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
ApiResponse$new(content = paste("Server returned ", local_var_resp$status_code, " response status code."),
response = local_var_resp,
status_code = local_var_resp$status_code)
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
ApiResponse$new(content = "API client error",
response = local_var_resp,
status_code = local_var_resp$status_code)
} else if (local_var_resp$status_code >= 500 && local_var_resp$status_code <= 599) {
if (is.null(local_var_resp$response) || all(local_var_resp$response == "")) {
local_var_resp$response <- "API server error"
}
return(local_var_resp)
}
},
#' @description
#' To test HTTP bearer authentication
#'
#' @param data_file (optional) name of the data file to save the result
#' @param ... Other optional arguments
#'
#' @return character
TestAuthHttpBearer = function(data_file = NULL, ...) {
local_var_response <- self$TestAuthHttpBearerWithHttpInfo(data_file = data_file, ...)
if (local_var_response$status_code >= 200 && local_var_response$status_code <= 299) {
return(local_var_response$content)
} else if (local_var_response$status_code >= 300 && local_var_response$status_code <= 399) {
return(local_var_response)
} else if (local_var_response$status_code >= 400 && local_var_response$status_code <= 499) {
return(local_var_response)
} else if (local_var_response$status_code >= 500 && local_var_response$status_code <= 599) {
return(local_var_response)
}
},
#' @description
#' To test HTTP bearer authentication
#'
#' @param data_file (optional) name of the data file to save the result
#' @param ... Other optional arguments
#'
#' @return API response (character) with additional information such as HTTP status code, headers
TestAuthHttpBearerWithHttpInfo = function(data_file = NULL, ...) {
args <- list(...)
query_params <- list()
header_params <- c()
form_params <- list()
file_params <- list()
local_var_body <- NULL
oauth_scopes <- NULL
is_oauth <- FALSE
local_var_url_path <- "/auth/http/bearer"
# Bearer token
if (!is.null(self$api_client$bearer_token)) {
header_params["Authorization"] <- paste("Bearer", self$api_client$bearer_token, sep = " ")
}
# The Accept request HTTP header
local_var_accepts <- list("text/plain")
# The Content-Type representation header
local_var_content_types <- list()
local_var_resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, local_var_url_path),
method = "POST",
query_params = query_params,
header_params = header_params,
form_params = form_params,
file_params = file_params,
accepts = local_var_accepts,
content_types = local_var_content_types,
body = local_var_body,
is_oauth = is_oauth,
oauth_scopes = oauth_scopes,
...)
if (local_var_resp$status_code >= 200 && local_var_resp$status_code <= 299) {
# save response in a file
if (!is.null(data_file)) {
self$api_client$WriteFile(local_var_resp, data_file)
}
deserialized_resp_obj <- tryCatch(
self$api_client$DeserializeResponse(local_var_resp, "character"),
error = function(e) {
stop("Failed to deserialize response")
}
)
local_var_resp$content <- deserialized_resp_obj
return(local_var_resp)
}
local_var_error_msg <- local_var_resp$response_as_text()
if (local_var_resp$status_code >= 300 && local_var_resp$status_code <= 399) {
ApiResponse$new(content = paste("Server returned ", local_var_resp$status_code, " response status code."),
response = local_var_resp,
status_code = local_var_resp$status_code)
} else if (local_var_resp$status_code >= 400 && local_var_resp$status_code <= 499) {
ApiResponse$new(content = "API client error",
response = local_var_resp,
status_code = local_var_resp$status_code)
} else if (local_var_resp$status_code >= 500 && local_var_resp$status_code <= 599) {
if (is.null(local_var_resp$response) || all(local_var_resp$response == "")) {
local_var_resp$response <- "API server error"
}
return(local_var_resp)
}
}
)
)