The most basic form of API interaction: Querying a specific URL and getting
its parsed result. If the response is empty, the function returns an empty
tibble(), and if there are date-time variables
present in the response, they are converted to POSIXct
via
lubridate::ymd_hms()
or to Date
via lubridate::as_date()
if the
variable only contains date information.
Usage
trakt_get(url, client_id = Sys.getenv("trakt_client_id"), HEAD = FALSE)
Arguments
- url
character(1)
: The API endpoint. Either a full URL like"https://api.trakt.tv/shows/breaking-bad"
or just the endpoint likeshows/breaking-bad
.- client_id
character(1)
: API client ID. If no value is set, this defaults to the package's client ID. See trakt_credentials for further information.- HEAD
logical(1) [FALSE]
: IfTRUE
, only a HTTPHEAD
request is performed and its content returned. This is useful if you are only interested in status codes or other headers, and don't want to waste resources/bandwidth on the response body.
Value
The parsed (jsonlite::fromJSON()
) content of the API response.
An empty tibble() if the response is an empty
JSON
array.
Details
See the official API reference for a detailed overview of available methods. Most methods of potential interest for data collection have dedicated functions in this package.
Note
No OAuth2 methods are supported yet, meaning you don't have access to
POST
methods or user information of non-public profiles.
See also
Other API-basics:
trakt_credentials()
,
trakt_get_token()
Examples
# A simple request to a direct URL
trakt_get("https://api.trakt.tv/shows/breaking-bad")
#> $title
#> [1] "Breaking Bad"
#>
#> $year
#> [1] 2008
#>
#> $ids
#> $ids$trakt
#> [1] 1388
#>
#> $ids$slug
#> [1] "breaking-bad"
#>
#> $ids$tvdb
#> [1] 81189
#>
#> $ids$imdb
#> [1] "tt0903747"
#>
#> $ids$tmdb
#> [1] 1396
#>
#> $ids$tvrage
#> NULL
#>
#>
# A HEAD-only request
# useful for validating a URL exists or the API is accessible
trakt_get("https://api.trakt.tv/users/jemus42", HEAD = TRUE)
#> $status
#> [1] 200
#>
#> $version
#> [1] "HTTP/2"
#>
#> $headers
#> $date
#> [1] "Tue, 01 Oct 2024 18:33:41 GMT"
#>
#> $`content-type`
#> [1] "application/json; charset=utf-8"
#>
#> $`x-frame-options`
#> [1] "SAMEORIGIN"
#>
#> $`x-xss-protection`
#> [1] "0"
#>
#> $`x-content-type-options`
#> [1] "nosniff"
#>
#> $`x-download-options`
#> [1] "noopen"
#>
#> $`x-permitted-cross-domain-policies`
#> [1] "none"
#>
#> $`referrer-policy`
#> [1] "strict-origin-when-cross-origin"
#>
#> $`x-private-user`
#> [1] "false"
#>
#> $vary
#> [1] "Accept-Encoding"
#>
#> $`content-encoding`
#> [1] "gzip"
#>
#> $etag
#> [1] "W/\"7ebe0ce147f05210a4f1e8618010830b\""
#>
#> $`cache-control`
#> [1] "max-age=0, private, must-revalidate"
#>
#> $`x-request-id`
#> [1] "c8200091-ba78-40be-a399-3bbadaf5b5bc"
#>
#> $`x-runtime`
#> [1] "0.008200"
#>
#> $`cf-cache-status`
#> [1] "DYNAMIC"
#>
#> $server
#> [1] "cloudflare"
#>
#> $`cf-ray`
#> [1] "8cbe8adf1b5d0164-ORD"
#>
#> attr(,"class")
#> [1] "insensitive" "list"
#>
# Optionally be lazy about URL specification by dropping the hostname:
trakt_get("shows/game-of-thrones")
#> $title
#> [1] "Game of Thrones"
#>
#> $year
#> [1] 2011
#>
#> $ids
#> $ids$trakt
#> [1] 1390
#>
#> $ids$slug
#> [1] "game-of-thrones"
#>
#> $ids$tvdb
#> [1] 121361
#>
#> $ids$imdb
#> [1] "tt0944947"
#>
#> $ids$tmdb
#> [1] 1399
#>
#> $ids$tvrage
#> NULL
#>
#>