Skip to contents

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 like shows/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]: If TRUE, only a HTTP HEAD 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, 16 Apr 2024 14:56:37 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"
#> 
#> $etag
#> [1] "W/\"7ef65a74d81a298cd530c56df83baca9\""
#> 
#> $`cache-control`
#> [1] "max-age=0, private, must-revalidate"
#> 
#> $`x-request-id`
#> [1] "d4f4669b-e47d-46df-9dc9-93641524d512"
#> 
#> $`x-runtime`
#> [1] "0.009817"
#> 
#> $`cf-cache-status`
#> [1] "DYNAMIC"
#> 
#> $server
#> [1] "cloudflare"
#> 
#> $`cf-ray`
#> [1] "875505ec2bd2638d-ORD"
#> 
#> $`content-encoding`
#> [1] "br"
#> 
#> $`alt-svc`
#> [1] "h3=\":443\"; ma=86400"
#> 
#> 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
#> 
#>