Skip to content

Commit

Permalink
run styler to fix formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
rcannood committed Oct 30, 2024
1 parent 2f8225b commit 5570ed3
Show file tree
Hide file tree
Showing 28 changed files with 1,527 additions and 1,405 deletions.
146 changes: 91 additions & 55 deletions R/api_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,24 +145,26 @@ ApiClient <- R6::R6Class(
CallApi = function(url, method, query_params, header_params, form_params,
file_params, accepts, content_types,
body, stream_callback = NULL, ...) {

resp <- self$Execute(url, method, query_params, header_params,
form_params, file_params,
accepts, content_types,
body, stream_callback = stream_callback, ...)
form_params, file_params,
accepts, content_types,
body,
stream_callback = stream_callback, ...
)

if (is.null(self$max_retry_attempts)) {
self$max_retry_attempts <- 3
}

if (!is.null(self$retry_status_codes)) {

for (i in 1 : self$max_retry_attempts) {
for (i in 1:self$max_retry_attempts) {
if (resp$status_code %in% self$retry_status_codes) {
Sys.sleep((2 ^ i) + stats::runif(n = 1, min = 0, max = 1))
Sys.sleep((2^i) + stats::runif(n = 1, min = 0, max = 1))
resp <- self$Execute(url, method, query_params, header_params,
form_params, file_params, accepts, content_types,
body, stream_callback = stream_callback, ...)
form_params, file_params, accepts, content_types,
body,
stream_callback = stream_callback, ...
)
} else {
break
}
Expand Down Expand Up @@ -200,72 +202,100 @@ ApiClient <- R6::R6Class(
}

# set HTTP accept header
accept = self$select_header(accepts)
accept <- self$select_header(accepts)
if (!is.null(accept)) {
headers['Accept'] = accept
headers["Accept"] <- accept
}

# set HTTP content-type header
content_type = self$select_header(content_types)
content_type <- self$select_header(content_types)
if (!is.null(content_type)) {
headers['Content-Type'] = content_type
headers["Content-Type"] <- content_type
}

if (typeof(stream_callback) == "closure") { # stream data
if (method == "GET") {
httr::GET(url, query = query_params, headers, http_timeout,
httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
httr::GET(url,
query = query_params, headers, http_timeout,
httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...
)
} else if (method == "POST") {
httr::POST(url, query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
httr::POST(url,
query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...
)
} else if (method == "PUT") {
httr::PUT(url, query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
httr::PUT(url,
query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...
)
} else if (method == "PATCH") {
httr::PATCH(url, query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
httr::PATCH(url,
query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...
)
} else if (method == "HEAD") {
httr::HEAD(url, query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
httr::HEAD(url,
query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...
)
} else if (method == "DELETE") {
httr::DELETE(url, query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...)
httr::DELETE(url,
query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), write_stream(stream_callback), ...
)
} else {
err_msg <- "Http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`."
rlang::abort(message = err_msg,
.subclass = "ApiException",
ApiException = ApiException$new(status = 0, reason = err_msg))
rlang::abort(
message = err_msg,
.subclass = "ApiException",
ApiException = ApiException$new(status = 0, reason = err_msg)
)
}
} else { # no streaming
if (method == "GET") {
httr_response <- httr::GET(url, query = query_params, headers, http_timeout,
httr::user_agent(self$`user_agent`), ...)
httr_response <- httr::GET(url,
query = query_params, headers, http_timeout,
httr::user_agent(self$`user_agent`), ...
)
} else if (method == "POST") {
httr_response <- httr::POST(url, query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
httr::user_agent(self$`user_agent`), ...)
httr_response <- httr::POST(url,
query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
httr::user_agent(self$`user_agent`), ...
)
} else if (method == "PUT") {
httr_response <- httr::PUT(url, query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...)
httr_response <- httr::PUT(url,
query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...
)
} else if (method == "PATCH") {
httr_response <- httr::PATCH(url, query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...)
httr_response <- httr::PATCH(url,
query = query_params, headers, body = body,
httr::content_type("application/json"), http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...
)
} else if (method == "HEAD") {
httr_response <- httr::HEAD(url, query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...)
httr_response <- httr::HEAD(url,
query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...
)
} else if (method == "DELETE") {
httr_response <- httr::DELETE(url, query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...)
httr_response <- httr::DELETE(url,
query = query_params, headers, http_timeout,
http_timeout, httr::user_agent(self$`user_agent`), ...
)
} else {
err_msg <- "Http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`."
rlang::abort(message = err_msg,
.subclass = "ApiException",
ApiException = ApiException$new(status = 0, reason = err_msg))
rlang::abort(
message = err_msg,
.subclass = "ApiException",
ApiException = ApiException$new(status = 0, reason = err_msg)
)
}

# return ApiResponse
Expand Down Expand Up @@ -309,16 +339,20 @@ ApiClient <- R6::R6Class(

# To handle the "map" type
if (startsWith(return_type, "map(")) {
inner_return_type <- regmatches(return_type,
regexec(pattern = "map\\((.*)\\)", return_type))[[1]][2]
inner_return_type <- regmatches(
return_type,
regexec(pattern = "map\\((.*)\\)", return_type)
)[[1]][2]
return_obj <- lapply(names(obj), function(name) {
self$deserializeObj(obj[[name]], inner_return_type, pkg_env)
})
names(return_obj) <- names(obj)
} else if (startsWith(return_type, "array[")) {
# To handle the "array" type
inner_return_type <- regmatches(return_type,
regexec(pattern = "array\\[(.*)\\]", return_type))[[1]][2]
inner_return_type <- regmatches(
return_type,
regexec(pattern = "array\\[(.*)\\]", return_type)
)[[1]][2]
if (c(inner_return_type) %in% primitive_types) {
return_obj <- vector("list", length = length(obj))
if (length(obj) > 0) {
Expand All @@ -331,8 +365,10 @@ ApiClient <- R6::R6Class(
return_obj <- vector("list", length = nrow(obj))
if (nrow(obj) > 0) {
for (row in 1:nrow(obj)) {

Check warning on line 367 in R/api_client.R

View workflow job for this annotation

GitHub Actions / R-CMD-check

file=R/api_client.R,line=367,col=27,[seq_linter] 1:nrow(...) is likely to be wrong in the empty edge case. Use seq_len(nrow(...)) instead.
return_obj[[row]] <- self$deserializeObj(obj[row, , drop = FALSE],
inner_return_type, pkg_env)
return_obj[[row]] <- self$deserializeObj(
obj[row, , drop = FALSE],
inner_return_type, pkg_env
)
}
}
}
Expand Down
Loading

0 comments on commit 5570ed3

Please sign in to comment.