---
title: "Implemented API methods"
author: "Lukas Burk"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Implemented API methods}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
editor_options: 
  chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  echo = FALSE
)

library(tRakt)
library(kableExtra)
library(purrr)
library(dplyr)
library(stringr)
```

```{r api-list}
api <- system.file("api-methods.yml", package = "tRakt") %>%
  yaml::read_yaml() 

api <- api %>%
  map_dfr(~ {
    map_dfr(.x, as_tibble, .id = "Method")
  }, .id = "Section")

pkg_funs <- getNamespaceExports("tRakt") %>%
  str_subset("%>%|trakt_get|build_trakt_url|trakt_credentials", negate = TRUE)

unmentioned <- pkg_funs[!(pkg_funs %in% str_remove_all(api$implementation, pattern = "\\(\\)"))]

format_api_table <- function(xdf) {
  xdf %>%
    mutate(
      endpoint = cell_spec(endpoint, link = url),
      # Section = stringr::str_to_title(Section),
      # Section = cell_spec(Section, bold = TRUE),
      Method = str_to_title(Method),
      Method = str_replace_all(Method, "^Id\\s", "ID "),
      #implementation = if_else(is.na(implementation), "N/A", implementation),
      implementation = if_else(
        implementation == "none",
        "",
        paste0("<code>", implementation, "</code>")
      ),
      authentication = if_else(
        !is.na(authentication),
        cell_spec("Required", bold = TRUE, color = "white", background = "red"),
        cell_spec("Optional", bold = FALSE, color = "white", background = "green")
      ),
    ) %>%
    select(
      -url,
      Endpoint = endpoint,
      Auth = authentication,
      Implementation = implementation
    )
}

print_api_table <- function(xdf) {
  xdf %>%
    kable(escape = FALSE) %>%
    kable_styling(
      position = "center",
      bootstrap_options = c("striped", "hover"),
      full_width = TRUE
    )
}
```

This is a reference list of [API methods available](https://trakt.docs.apiary.io/), their endpoint URLs (with user-specified parameters indiciated by a `:` prefix) and a link to their respective implementation in this package, if applicable.  
Authenticated methods (there's only a few) are indicated as such.

Note that not all available methods are listed here, especially those geared towards interactive apps – I highly doubt that people will want to post (or update) comments through this package.

# Search

```{r api-search}
api %>%
  filter(Section == "search") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

# Movies

```{r api-movies}
api %>%
  filter(Section == "movies") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

# Shows

```{r api-shows}
api %>%
  filter(Section == "shows") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

## Seasons

```{r api-shows-seasons}
api %>%
  filter(Section == "seasons") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

## Episodes

```{r api-shows-episodes}
api %>%
  filter(Section == "episodes") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

# People

```{r api-people}
api %>%
  filter(Section == "people") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

# Users

```{r api-users}
api %>%
  filter(Section == "users") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

# Comments

```{r api-comments}
api %>%
  filter(Section == "comments") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

# Lists

```{r api-lists}
api %>%
  filter(Section == "lists") %>%
  select(-Section) %>%
  format_api_table() %>%
  print_api_table()
```

# Misc

These endpoints are used to check filter arguments. The output is cached in tidied up
format as package datasets.

```{r api-misc}
api %>%
  filter(Section %in% c(
    "certifications", "countries",
    "genres", "languages", "networks"
  )) %>%
  format_api_table() %>%
  select(-Auth) %>%
  print_api_table()
```
