Skip to content

Commit

Permalink
rate limit requests client-side to 30/minute
Browse files Browse the repository at this point in the history
  • Loading branch information
adamhsparks committed Sep 1, 2021
1 parent 424dfa6 commit 09c80c7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Imports:
crul,
lubridate,
jsonlite,
ratelimitr,
readr,
tibble
RoxygenNote: 7.1.1.9001
Expand All @@ -62,7 +63,8 @@ Suggests:
knitr,
rmarkdown,
testthat,
spelling
spelling,
vcr
VignetteBuilder: knitr
X-schema.org-applicationCategory: Tools
X-schema.org-keywords: NASA, meteorological-data, weather, global, weather,
Expand Down
12 changes: 8 additions & 4 deletions R/get_power.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
#' object. All options offered by the official \acronym{POWER} \acronym{API}
#' are supported. Requests are formed to submit one request per point. There
#' is no need to make synchronous requests for multiple parameters for a
#' single point or regional request.
#' single point or regional request. Requests are limited to 30 unique
#' requests per 60 seconds. \CRANpkg{nasapower} attempts to enforce this
#' client-side.
#'
#' @param community A character vector providing community name: \dQuote{ag},
#' \dQuote{re} or \dQuote{sb}. See argument details for more.
Expand Down Expand Up @@ -311,9 +313,11 @@ get_power <- function(community,
)

response <-
.send_query(.query_list = query_list,
.temporal_api = temporal_api,
.url = power_url)
.rate_limited_query(.query_list = query_list,
.temporal_api = temporal_api,
.url = power_url)

response$raise_for_status()

# create meta object
power_data <- readr::read_lines(response$parse("UTF8"))
Expand Down
5 changes: 5 additions & 0 deletions R/internal_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,8 @@
}) # nocov end
return(response)
}

# create a rate-limited query function that respects the POWER API limits
.rate_limited_query <-
ratelimitr::limit_rate(.send_query, ratelimitr::rate(n = 30,
period = 60))
4 changes: 3 additions & 1 deletion man/get_power.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 comment on commit 09c80c7

@camwur
Copy link

@camwur camwur commented on 09c80c7 Sep 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, thanks for your prompt update to this code!

Please sign in to comment.