Author: Tan Ho
Date: 2023-02-11

# MFL: Get Endpoint

#### Tan Ho

#### 2023-02-11

## Creating custom MFL API calls

    library(ffscrapr)

The MFL API is extensive. If there is something you’d like to access beyond the current scope of ffscrapr, you can use the lower-level “`mfl_getendpoint`” function to create a GET request and access the data, while still using the authentication and rate-limiting features I’ve already created.

Here is an example of how you can call one of the endpoints - in this case, let’s try searching for SFBX leagues through the leagueSearch endpoint.

We’ll start by opening up this page, https://api.myfantasyleague.com/2020/api_info?STATE=test&CCAT=export&TYPE=leagueSearch, which is the “test” page for this particular endpoint. From here, we can see that the only parameter required is “SEARCH”.

We need a connection object to pass into the mfl_getendpoint function, although in this example we don’t need to give it much since the endpoint doesn’t need a leagueID or username/password or APIKEY.

    conn <- mfl_connect(season = 2020)

    conn
    #> <MFL connection 2020_>
    #> List of 5
    #>  $ platform   : chr "MFL"
    #>  $ season     : num 2020
    #>  $ league_id  : chr(0)
    #>  $ APIKEY     : NULL
    #>  $ auth_cookie: NULL
    #>  - attr(*, "class")= chr "mfl_conn"

The parameters of the mfl_getendpoint function are `conn`, `endpoint`, and any other optional parameters required by the API.

The function will automatically insert the league_id, API key, and/or authentication cookies from the connection object, and will request JSON for you - so you do not need to add any of these parameters.

It is safest to assume that everything is case-sensitive: the endpoint must match the case displayed by MFL (“leagueSearch”) and the SEARCH argument name must be provided in upper-case.

    sfb_search <- mfl_getendpoint(conn,endpoint = "leagueSearch", SEARCH = "sfbx conference")

    str(sfb_search, max.level = 1)
    #> List of 3
    #>  $ content :List of 3
    #>  $ query   : chr "https://api.myfantasyleague.com/2020/export?TYPE=leagueSearch&SEARCH=sfbx%20conference&JSON=1"
    #>  $ response:List of 9
    #>   ..- attr(*, "class")= chr "response"
    #>  - attr(*, "class")= chr "mfl_api"

The function returns a list with the `query` that was sent, the `response` that was received, and the `content` that was parsed - this helps you debug the result of the function later, by inspecting the query that was sent and the response that was received.

I like to extract the `content` with `purrr::pluck` and then convert it into a tibble and unnest the content from there, but you can use base R subsetting or `magrittr::extract2` for the same purpose.

    search_results <- sfb_search %>%
      purrr::pluck("content","leagues","league") %>%
      tibble::tibble() %>%
      tidyr::unnest_wider(1)

    head(search_results)
    #> # A tibble: 6 × 3
    #>   homeURL                                          name                id
    #>   <chr>                                            <chr>               <chr>
    #> 1 http://www73.myfantasyleague.com/2020/home/13411 #SFBX Conference 12 13411
    #> 2 http://www73.myfantasyleague.com/2020/home/16428 #SFBX Conference 6  16428
    #> 3 http://www73.myfantasyleague.com/2020/home/17910 #SFBX Conference 11 17910
    #> 4 http://www73.myfantasyleague.com/2020/home/26453 #SFBX Conference 8  26453
    #> 5 http://www73.myfantasyleague.com/2020/home/27495 #SFBX Conference 2  27495
    #> 6 http://www73.myfantasyleague.com/2020/home/31492 #SFBX Conference 14 31492

## Another Example: Trade Bait

Here’s another example, this time with the trade bait endpoint: https://api.myfantasyleague.com/2020/api_info?STATE=test&CCAT=export&TYPE=tradeBait

    fog <- mfl_connect(season = 2019, league_id = 12608)

    fog_tradebait <- mfl_getendpoint(fog, "tradeBait", INCLUDE_DRAFT_PICKS = 1) %>%
      purrr::pluck("content","tradeBaits","tradeBait") %>%
      tibble::tibble() %>%
      tidyr::unnest_wider(1) %>%
      tidyr::separate_rows("willGiveUp",sep = ",") %>%
      dplyr::left_join(
        ff_franchises(fog) %>% dplyr::select("franchise_id","franchise_name"),
        by = c("franchise_id")
      ) %>%
      dplyr::left_join(
        mfl_players(fog) %>% dplyr::select("player_id","player_name","pos","age","team"),
        by = c("willGiveUp" = "player_id")
      )

    head(fog_tradebait)
    #> # A tibble: 6 × 9
    #>   timestamp  franchise_id willGiveUp inExcha…¹ franc…² playe…³ pos     age team
    #>   <chr>      <chr>        <chr>      <chr>     <chr>   <chr>   <chr> <dbl> <chr>
    #> 1 1574520555 0001         7394       "anyone … @JohnB… Rivers… QB     41.2 LAC
    #> 2 1574520555 0001         13128      "anyone … @JohnB… Cook, … RB     27.5 MIN
    #> 3 1580535809 0003         13319      ""        The Ac… Jones,… RB     28.2 GBP
    #> 4 1580535809 0003         13139      ""        The Ac… Willia… RB     27.9 GBP
    #> 5 1580535809 0003         11675      ""        The Ac… Adams,… WR     30.1 GBP
    #> 6 1580572165 0004         12171      ""        Kevin … Johnso… RB     31.2 ARI
    #> # … with abbreviated variable names ¹​inExchangeFor, ²​franchise_name,
    #> #   ³​player_name