~hrbrmstr/epidata

ref: dc17d09a82427dfd2aadefdc7701fe872c6b320a epidata/R/epi_query.r -rw-r--r-- 698 bytes
dc17d09ahrbrmstr fix CRAN errors 3 months ago
                                                                                
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
# Do the hard work of formatting the parameters and issuing the query
epi_query <- function(args) {

  qs <- paste(sprintf("%s=%s", names(args), args), collapse="&")

  httr::POST(
    "https://www.epi.org/wordpress/wp-admin/admin-ajax.php",
    httr::add_headers(
      `X-Requested-With` = "XMLHttpRequest",
      `Referer` = "https://www.epi.org/data/"
    ),
    .EPIDATA_UA,
    encode = "form",
    body = list(
      action = "epi_getdata",
      queryString = qs
    )
  ) -> res

  if (httr::status_code(res) != 200) {
    warning("EPI API is unresponsive. Please try again later.", call. = FALSE)
    return(NULL)
  }

  jsonlite::fromJSON(httr::content(res, as="text"), flatten=TRUE)

}