Debian Codegraph @

man/rest_api.Rd

raw
1 % Generated by roxygen2: do not edit by hand
2 % Please edit documentation in R/app.R, R/github.R
3 \name{rest_api}
4 \alias{rest_api}
5 \alias{rest_api_raw}
6 \alias{github_api}
7 \title{Get data from a REST API}
8 \usage{
9 rest_api(...)
10
11 rest_api_raw(root, endpoint, token = "", params = list(), headers = NULL)
12
13 github_api(
14 endpoint,
15 token = "",
16 params = list(),
17 headers = NULL,
18 raw = !loadable("jsonlite")
19 )
20 }
21 \arguments{
22 \item{...}{Arguments to be passed to \code{rest_api_raw()}.}
23
24 \item{root}{The API root URL.}
25
26 \item{endpoint}{The API endpoint.}
27
28 \item{token}{A named character string (e.g., \code{c(token = "xxxx")}), which
29 will be used to create an authorization header of the form
30 \samp{Authorization: NAME TOKEN} for the API call, where \samp{NAME} is the
31 name of the string and \samp{TOKEN} is the string. If the string does not
32 have a name, \samp{Basic} will be used as the default name.}
33
34 \item{params}{A list of query parameters to be sent with the API call.}
35
36 \item{headers}{A named character vector of HTTP headers, e.g., \code{c(Accept = "application/vnd.github.v3+json")}.}
37
38 \item{raw}{Whether to return the raw response or parse the response with
39 \pkg{jsonlite}.}
40 }
41 \value{
42 A character vector (the raw JSON response) or an R object parsed from
43 the JSON text.
44 }
45 \description{
46 Read data from a REST API and optionally with an authorization token in the
47 request header. The function \code{rest_api_raw()} returns the raw text of
48 the response, and \code{rest_api()} will parse the response with
49 \code{jsonlite::fromJSON()} (assuming that the response is in the JSON
50 format).
51 }
52 \details{
53 These functions are simple wrappers based on \code{\link[=url]{url()}} and
54 \code{\link[=read_utf8]{read_utf8()}}. Specifically, the \code{headers} argument is
55 passed to \code{url()}, and \code{read_utf8()} will send a \samp{GET} request
56 to the API server. This means these functions only support the \samp{GET}
57 method. If you need to use other HTTP methods (such as \samp{POST}), you have
58 to use other packages such as \pkg{curl} and \pkg{httr}.
59
60 \code{github_api()} is a wrapper function based on
61 \code{rest_api_raw()} to obtain data from the GitHub API:
62 \url{https://docs.github.com/en/rest}. You can provide a personal access
63 token (PAT) via the \code{token} argument, or via one of the environment
64 variables \var{GITHUB_PAT}, \var{GITHUB_TOKEN}, \var{GH_TOKEN}. A PAT
65 allows for a much higher rate limit in API calls. Without a token, you can
66 only make 60 calls in an hour.
67 }
68 \examples{\dontshow{if (interactive()) \{}
69 # a normal GET request
70 xfun::rest_api("https://mockhttp.org", "/get")
71 xfun::rest_api_raw("https://mockhttp.org", "/get")
72
73 # send the request with an auth header
74 xfun::rest_api("https://mockhttp.org", "/headers", "OPEN SESAME!")
75
76 # with query parameters
77 xfun::rest_api("https://mockhttp.org", "/response-headers", params = list(foo = "bar"))
78
79 # get the rate limit info from GitHub
80 xfun::github_api("/rate_limit")
81 \dontshow{\}}
82 }