~hrbrmstr/playdoh

ref: 15206ff4a198ed5fb2a37646aa2e66d09cf6d75a playdoh/R/doh-get.R -rw-r--r-- 1.5 KiB
15206ff4boB Rudis GET only; done-ish 1 year, 6 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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#' Make a DoH Request (GET/REST)
#'
#' Issue a `GET` REST API query of type `type` for `name` to the
#' DoH endpoint specified at `server_path`.
#'
#' @param name name to query for
#' @param type DNS query type (defaults to "`A`")
#' @param server_path full URL path to the DoH server quer endpoint (defaults to Quad9).
#' @param extra_params any special `GET` query parameter needed for a given server API endpoint.
#'        this should be a named `list`.
#' @return `NULL` (if the query failed) or a `data.frame` (tibble)
#' @references <https://tools.ietf.org/id/draft-ietf-doh-dns-over-https-05.html>
#' @export
#' @examples
#' doh_get("rud.is", "A")
doh_get <- function(name, type = "a", extra_params = list(), service_path = "https://9.9.9.9/dns-query") {

  stopifnot(is.list(extra_params))

  extra_params[["name"]] <- tolower(name)
  extra_params[["type"]] <- tolower(type[1])

  httr::GET(
    url = service_path,
    query = extra_params,
    .PLAYDOH_UA
  ) -> res

  httr::stop_for_status(res)

  out <- httr::content(res, as = "text", encoding = "UTF-8")
  out <- jsonlite::fromJSON(out)

  # will be NULL or a data frame
  ret <- out[["Answer"]]

  # cleanup column names and add query metadata if query was OK
  if (length(ret)) {

    colnames(ret) <- tolower(colnames(ret))

    attr(ret, "question") <- out[["Question"]]
    attr(ret, "flags") <- out[c("Status", "TC", "RD", "RA", "AD", "CD")]
    attr(ret, "edns_client_subnet") <- out[["edns_client_subnet"]]
    attr(ret, "comment") <- out[["comment"]]

  }

  ret

}