Skip to content

Commit

Permalink
throttle requests to 30 per minute
Browse files Browse the repository at this point in the history
  • Loading branch information
cole-brokamp committed Jul 6, 2024
1 parent 12f8f6b commit c43b3ab
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
3 changes: 2 additions & 1 deletion R/purple_air_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ purple_air_request <- function(purple_air_api_key = Sys.getenv("PURPLE_AIR_API_K
is_error = \(resp) httr2::resp_status(resp) != success_code,
body = \(resp) glue::glue_data(httr2::resp_body_json(resp), "{error}: {description} (API version: {api_version})")
) |>
httr2::req_url_query(!!!list(...), .multi = "comma")
httr2::req_url_query(!!!list(...), .multi = "comma") |>
httr2::req_throttle(30 / 60)
if (resource == "keys") req <- httr2::req_url_path_append(req, "keys")
if (resource == "organization") req <- httr2::req_url_path_append(req, "organization")
if (resource == "sensors") {
Expand Down
7 changes: 1 addition & 6 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ Get the latest data from several PurpleAir sensors:

Get historical data from a single PurpleAir sensor:

```{r}
#| echo: false
Sys.sleep(20)
```

```{r}
my_history <-
get_sensor_history(
Expand All @@ -100,7 +95,7 @@ my_history

and plot it:

```{r}
```{r "example_sensor_history_plot"}
my_history |>
tidyr::pivot_longer(cols = tidyr::starts_with("pm"), names_to = "pollutant", values_to = "concentration") |>
ggplot2::ggplot(ggplot2::aes(time_stamp, concentration, color = pollutant)) +
Expand Down
38 changes: 21 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use:

``` r
check_api_key(Sys.getenv("PURPLE_AIR_API_KEY"))
#> ✔ Using valid 'READ' key with version V1.0.14-0.0.57 of the PurpleAir API on 2024-07-06 00:13:55
#> ✔ Using valid 'READ' key with version V1.0.14-0.0.57 of the PurpleAir API on 2024-07-06 13:18:52
```

Get the latest data from a single PurpleAir sensor, defined by its
Expand All @@ -63,17 +63,19 @@ Get the latest data from a single PurpleAir sensor, defined by its
``` r
get_sensor_data(sensor_index = 175413,
fields = c("name", "last_seen", "pm2.5_cf_1", "pm2.5_atm"))
#> Waiting 2s for throttling delay ■■■■■■■■■■■■■■■
#> Waiting 2s for throttling delay ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#> $last_seen
#> [1] "2024-07-06 00:13:48 EDT"
#> [1] "2024-07-06 13:17:54 EDT"
#>
#> $name
#> [1] "JN-Clifton,OH"
#>
#> $pm2.5_atm
#> [1] 22.4
#> [1] 1.6
#>
#> $pm2.5_cf_1
#> [1] 22.4
#> [1] 1.6
```

Get the latest data from many PurpleAir sensors, defined by their sensor
Expand All @@ -82,11 +84,13 @@ keys,
``` r
get_sensors_data(x = as.integer(c(175257, 175413)),
fields = c("name", "last_seen", "pm2.5_cf_1", "pm2.5_atm"))
#> Waiting 2s for throttling delay ■■■■■■■■■■■■■■■
#> Waiting 2s for throttling delay ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
#> # A tibble: 2 × 5
#> sensor_index last_seen name pm2.5_atm pm2.5_cf_1
#> <int> <dttm> <chr> <dbl> <dbl>
#> 1 175257 2024-07-06 00:12:06 Lillard 25.8 25.8
#> 2 175413 2024-07-06 00:11:48 JN-Clifton,OH 23.3 23.3
#> 1 175257 2024-07-06 13:16:13 Lillard 1.2 1.2
#> 2 175413 2024-07-06 13:17:54 JN-Clifton,OH 1.6 1.6
```

a geographic bounding box,
Expand All @@ -110,16 +114,16 @@ my_history
#> # A tibble: 432 × 5
#> time_stamp pm1.0_cf_1 pm1.0_atm pm2.5_atm pm2.5_cf_1
#> <dttm> <dbl> <dbl> <dbl> <dbl>
#> 1 2024-07-04 22:30:00 2522. 1683. 1689. 2530.
#> 2 2024-07-04 23:00:00 2530. 1687. 1695. 2541.
#> 3 2024-07-04 07:20:00 2503. 1670. 1671. 2504.
#> 4 2024-07-04 13:10:00 2505. 1672. 1674. 2507.
#> 5 2024-07-04 19:50:00 9.73 9.73 12.1 12.1
#> 6 2024-07-04 13:50:00 2504. 1671. 1673. 2506.
#> 7 2024-07-03 21:00:00 2504. 1671. 1672. 2505.
#> 8 2024-07-02 07:00:00 2501. 1668. 1669. 2502.
#> 9 2024-07-02 16:10:00 1.62 1.62 1.94 1.94
#> 10 2024-07-03 10:20:00 14.6 14.6 17.4 17.4
#> 1 2024-07-04 22:00:00 2512. 1678. 1682. 2516.
#> 2 2024-07-04 21:20:00 2505. 1671. 1672. 2506.
#> 3 2024-07-04 20:30:00 16.5 14.2 16.5 18.9
#> 4 2024-07-04 21:40:00 2509. 1675. 1679. 2512.
#> 5 2024-07-04 22:30:00 2522. 1683. 1689. 2530.
#> 6 2024-07-04 21:10:00 2505. 1671. 1672. 2506.
#> 7 2024-07-04 20:40:00 31.9 25.3 27.9 34.6
#> 8 2024-07-04 23:30:00 2510. 1676. 1680. 2513.
#> 9 2024-07-04 23:50:00 2509. 1675. 1678. 2512.
#> 10 2024-07-04 23:20:00 2514. 1679. 1683. 2518.
#> # ℹ 422 more rows
```

Expand All @@ -132,4 +136,4 @@ my_history |>
ggplot2::geom_line()
```

<img src="man/figures/README-unnamed-chunk-11-1.png" width="100%" />
<img src="man/figures/README-example_sensor_history_plot-1.png" width="100%" />
File renamed without changes
9 changes: 9 additions & 0 deletions tests/testthat/test-get_sensor_history.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
test_that("get_sensor_history works", {
get_sensor_history(
sensor_index = 175413,
fields = c("pm1.0_cf_1", "pm1.0_atm", "pm2.5_cf_1", "pm2.5_atm"),
start_timestamp = as.POSIXct("2024-07-02"),
end_timestamp = as.POSIXct("2024-07-05")
) |>
expect_s3_class("tbl_df")
})

0 comments on commit c43b3ab

Please sign in to comment.