knitr::opts_chunk$set(echo = TRUE,
                      comment = "      ##",
                      error = TRUE,
                      collapse = TRUE)
# foo1 will be a function
library(knitr)
knit_hooks$set(foo1 = function(before, options, envir) {
  if (before) {

    '\\tiny _I appear before a chunk!_\n\n'
  } else {
    '\\normalsize  \n\n_I am after a chunk..._'
  }
})

API and Restful APIs

API is way to open a close software system in specific ways and to specific

users.

Restful Examples


RESTFUL APIs



DEFINITION

(from Wikipedia)https://en.wikipedia.org/wiki/Representational_state_transfer#Applied_to_web_services

Web service APIs that adhere to the REST architectural constraints are called RESTful APIs.[12] HTTP-based RESTful APIs are defined with the following aspects:[13]


Really need to know?

No R libraries (wrapper) for Github API

Goal: Use R to programmatically retrieve data for analysis, securely.



What technologies are APIs built upon?



HTTP Diagram



API documenation

Endpoints:
https://docs.github.com/en/rest/overview/endpoints-available-for-github-apps https://developers.google.com/youtube/v3/docs/playlists/list


CURL intro (using httpbin.org) do in browser, then in curl compare: curl -is httpbin.org vs httpbin.org/get

browser: show curl

curl -sv http://httpbin.org 

CURL intro (using Github) (Live)



We know some of the plumbing: HTTP, API documentation, CURL ...

Finally, Do this in R, httr2:

https://httr2.r-lib.org

show httpbin.org show github.com

EXAMPLE: Basic GET in R



json

```{zsh, foo1="whatever"} curl -s https://api.github.com/users/jimrothstein | head

#   json, -i includes headers
```{zsh}
curl -si https://api.github.com/users/jimrothstein | head

GITHUB Personal Access Token (PAT)


Github PAT token

Why a token?

settings | developer settings (bottom left)

more info: get token: https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api

https://docs.github.com/en/rest ```{zsh, echo=T} export token=$(Rscript -e "cat(Sys.getenv('GITHUB_PAT'))")

echo $token

curl -si -u jimrothstein:$token https://api.github.com/users/jimrothstein | head

# store the token ~/.Renviron
#   token, works
```{zsh, echo=T}
# Authorization?
export token=$(Rscript -e "cat(Sys.getenv('GITHUB_PAT'))")
echo $token
curl -Hs "Authorization: token $token" https://api.github.com/users/codertocat -I

There is another side to all this: the Creater of APIs.

R has a tool plumber.
Not for today.

One issue not to overlook: 3rd party Security


So far, the user has been you - the Developer. What if you want others to

use app? They are going to give their username/password to your app.


EXAMPLE Use Github uses token (PAT) in R.

New problem: each API server is different!

(Quote: httr2:: docs)

EXAMPLES: Many (follow httr2:: vignette) + Google, Youtube ...



#knitr::knit_exit()
{
file <- "002_API_R_talk.Rmd"
file  <- normalizePath(file)
file
}
rmarkdown::render(file, output_dir = "~/Downloads/print_and_delete")


jimrothstein/yt_api documentation built on Nov. 5, 2022, 8:05 p.m.