Skip to content

Commit c19594b

Browse files
authored
Merge pull request #1656 from jameslamb/feat/retries
added retry logic to HTTP requests
2 parents 693ddbf + 0f1f181 commit c19594b

10 files changed

+56
-23
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Depends:
2626
Imports:
2727
tools,
2828
scales,
29-
httr,
29+
httr (>= 1.3.0),
3030
jsonlite (>= 1.6),
3131
magrittr,
3232
digest,

NAMESPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -241,14 +241,14 @@ importFrom(htmlwidgets,saveWidget)
241241
importFrom(htmlwidgets,shinyRenderWidget)
242242
importFrom(htmlwidgets,shinyWidgetOutput)
243243
importFrom(htmlwidgets,sizingPolicy)
244-
importFrom(httr,GET)
245-
importFrom(httr,PATCH)
246-
importFrom(httr,POST)
244+
importFrom(httr,RETRY)
247245
importFrom(httr,add_headers)
246+
importFrom(httr,authenticate)
248247
importFrom(httr,config)
249248
importFrom(httr,content)
250249
importFrom(httr,stop_for_status)
251250
importFrom(httr,warn_for_status)
251+
importFrom(httr,write_disk)
252252
importFrom(jsonlite,parse_json)
253253
importFrom(jsonlite,read_json)
254254
importFrom(jsonlite,toJSON)

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
## IMPROVEMENTS
88

9+
* All HTTP requests are now retried upon failure (#1656)
10+
911
## BUG FIXES
1012

1113
* `ggplotly()` now handles `element_blank()` and `factor()` labels in positional scales correctly (#1731 and #1772).

R/api_exports.R

+13-6
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@
3131
#' @param endpoint the endpoint (i.e., location) for the request.
3232
#' To see a list of all available endpoints, call `api()`.
3333
#' Any relevant query parameters should be included here (see examples).
34-
#' @param verb name of the HTTP verb to use (as in, [httr::VERB()]).
35-
#' @param body body of the HTTP request(as in, [httr::VERB()]).
34+
#' @param verb name of the HTTP verb to use (as in, [httr::RETRY()]).
35+
#' @param body body of the HTTP request(as in, [httr::RETRY()]).
3636
#' If this value is not already converted to JSON
3737
#' (via [jsonlite::toJSON()]), it uses the internal `to_JSON()`
3838
#' to ensure values are "automatically unboxed" (i.e., vec.
3939
#'
4040
#' @param ... For `api()`, these arguments are passed onto
41-
#' [httr::VERB()]. For `api_create()`, these arguments are
41+
#' [httr::RETRY()]. For `api_create()`, these arguments are
4242
#' included in the body of the HTTP request.
4343
#'
4444
#' @export
@@ -187,9 +187,16 @@ api <- function(endpoint = "/", verb = "GET", body = NULL, ...) {
187187
body <- to_JSON(body)
188188
}
189189

190-
resp <- httr::VERB(
191-
verb = verb, url = url, api_headers(), api_auth(),
192-
body = body, ...
190+
resp <- httr::RETRY(
191+
verb = verb,
192+
url = url,
193+
api_headers(),
194+
api_auth(),
195+
body = body,
196+
times = 5,
197+
terminate_on = c(400, 401, 403, 404),
198+
terminate_on_success = TRUE,
199+
...
193200
)
194201

195202
structure(process(resp), class = "api")

R/imports.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' @importFrom tidyr unnest
77
#' @importFrom viridisLite viridis
88
#' @importFrom jsonlite toJSON parse_json read_json
9-
#' @importFrom httr GET POST PATCH content config add_headers stop_for_status warn_for_status
9+
#' @importFrom httr RETRY content config add_headers authenticate stop_for_status warn_for_status write_disk
1010
#' @importFrom htmlwidgets createWidget sizingPolicy saveWidget onRender prependContent
1111
#' @importFrom lazyeval f_eval is_formula all_dots is_lang f_new
1212
#' @importFrom tibble as_tibble

R/orca.R

+7-3
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,13 @@ orca_serve <- function(port = 5151, mathjax = FALSE, safe = FALSE, request_limit
192192
height = height,
193193
scale = scale
194194
)
195-
res <- httr::POST(
196-
paste0("http://127.0.0.1:", port),
197-
body = to_JSON(bod)
195+
res <- httr::RETRY(
196+
verb = "POST",
197+
url = paste0("http://127.0.0.1:", port),
198+
body = to_JSON(bod),
199+
times = 5,
200+
terminate_on = c(400, 401, 403, 404),
201+
terminate_on_success = TRUE
198202
)
199203
httr::stop_for_status(res)
200204
httr::warn_for_status(res)

R/plotly_IMAGE.R

+11-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#' @param format The desired image format 'png', 'jpeg', 'svg', 'pdf', 'eps', or 'webp'
1010
#' @param scale Both png and jpeg formats will be scaled beyond the specified width and height by this number.
1111
#' @param out_file A filename for writing the image to a file.
12-
#' @param ... arguments passed onto `httr::POST`
12+
#' @param ... arguments passed onto `httr::RETRY`
1313
#' @export
1414
#' @examples \dontrun{
1515
#' p <- plot_ly(x = 1:10)
@@ -34,9 +34,16 @@ plotly_IMAGE <- function(x, width = 1000, height = 500, format = "png",
3434
filename = Sys.time()
3535
)
3636
base_url <- file.path(get_domain("api"), "v2", "images")
37-
resp <- httr::POST(
38-
base_url, body = to_JSON(bod), api_headers(), api_auth(),
39-
if (!missing(out_file)) httr::write_disk(out_file, overwrite = TRUE),
37+
resp <- httr::RETRY(
38+
verb = "POST",
39+
url = base_url,
40+
body = to_JSON(bod),
41+
times = 5,
42+
terminate_on = c(400, 401, 403, 404),
43+
terminate_on_success = TRUE,
44+
api_headers(),
45+
api_auth(),
46+
if (!missing(out_file)) httr::write_disk(out_file, overwrite = TRUE),
4047
...
4148
)
4249
con <- process(append_class(resp, "api_image"))

R/signup.R

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,14 @@ signup <- function(username, email, save = TRUE) {
4545
version = as.character(packageVersion("plotly"))
4646
)
4747
base_url <- file.path(get_domain(), "apimkacct")
48-
resp <- httr::POST(base_url, body = bod)
48+
resp <- httr::RETRY(
49+
verb = "POST",
50+
base_url,
51+
body = bod,
52+
times = 5,
53+
terminate_on = c(400, 401, 403, 404),
54+
terminate_on_success = TRUE
55+
)
4956
con <- process(append_class(resp, "signup"))
5057
if (save) {
5158
# store API key as an environment variable in .Rprofile

inst/plotlyjs.R

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
library(httr)
22
# download latest GitHub release
33
# for a particular version: `zip <- "https://github.com/plotly/plotly.js/archive/v1.33.1.zip"`
4-
x <- GET('https://api.github.com/repos/plotly/plotly.js/releases/latest')
4+
x <- httr::RETRY(
5+
verb = "GET",
6+
url = 'https://api.github.com/repos/plotly/plotly.js/releases/latest',
7+
times = 5,
8+
terminate_on = c(400, 401, 403, 404),
9+
terminate_on_success = TRUE
10+
)
511
zip <- content(x)$zipball_url
612
tmp <- tempfile(fileext = ".zip")
713
download.file(zip, tmp)

man/api.Rd

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)