---
title: "Low Level NHL API"
author: "Jozef Hajnala"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Low Level NHL API}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

# Low Level NHL API

If you would like to interact with the API directly without any helpers, the `nhl_get_data()` function is made for this purpose. It takes as input one or more urls (a character vector) and returns a list of the same length, each element containing the raw results returned from the API for the corresponding URL.

For example:

```{r}
library(nhlapi)
urls <- c(
  "https://statsapi.web.nhl.com/api/v1/teams/1",
  "https://statsapi.web.nhl.com/api/v1/people/8477474"
)

nhl_get_data(urls)
```

Since it is helpful for work with R to get data in a flat data frame format, the function will by default attempt to flatten nested data frame columns and return flat data frames where possible. If you really want raw data returned, you can set the flatten argument to `FALSE`:

```{r}
nhl_get_data(urls, flatten = FALSE)
```

In case there are errors encountered when retrieving the data, the process does not fail, but returns a list with class `nhl_get_data_error`:

```{r}
nhl_get_data(c("https://statsapi.web.nhl.com/api/v1/wrongurl", urls))
```

The URLs themselves are preserved via the `url` attribute:

```{r}
lapply(nhl_get_data(urls), attr, which = "url")
```
