Skip to content

Commit

Permalink
🎨 handle tokens better
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Cazelles committed Oct 27, 2020
1 parent d973895 commit 8f99a9d
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 50 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
^README.Rmd$
^README_files/
^\.github/workflows/R-CMD-check\.yaml$
^.github/
4 changes: 2 additions & 2 deletions .github/workflows/monitor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ jobs:
shell: Rscript {0}
- name: Use rowlbot
run: |
rowlbot::rowlbot("owl")
rowlbot::rowlbot("cool")
rowlbot::rowlbot("owl", "${{ secrets.OWLBOT_TOKEN }}")
rowlbot::rowlbot("cool", "${{ secrets.OWLBOT_TOKEN }}")
shell: Rscript {0}


Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: rowlbot
Title: R client for the Owlbot Dictionary API
Version: 1.0.0
Version: 1.0.0.9000
Date: 2020-10-27
Authors@R:
person(given = "Kevin",
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

S3method(plot,rob_def)
S3method(print,rob_def)
export(forget_token)
export(rob_get_secret)
export(rob_set_token)
export(rowlbot)
importFrom(cli,cat_line)
importFrom(cli,cat_rule)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# rowlbot 1.0.0.9000

* `rob_get_secret()`, `rob_set_token()`, `forget_token()` are now exposed.
* `rowlbot()``gains two arguments: `token` and `...`.
* `rob_getsecret()` is now `rob_get_secret()` for names consistency.


# rowlbot 1.0.0

* Initial release (see README).
9 changes: 6 additions & 3 deletions R/rowlbot.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
#'
#' @param word a word to be defined.
#' @param x an object of class `rob_def`.
#' @param ... ignored.
#' @param token authentification token. If `NULL` (default) then is [rob_get_secret()]
#' called which looks for `OWLBOT_TOKEN` in environment variables.
#' @param ... Extra argument passed to [httr::GET()].
#'
#' @export
#' @examples
#' \dontrun{
#' rowlbot("owl")
#' }
rowlbot <- function(word) {
out <- rob_res(rob_url(word))
rowlbot <- function(word, token = NULL, ...) {
if (is.null(token)) token <- rob_get_secret()
out <- rob_res(rob_url(word), token = token, ...)
class(out) <- "rob_def"
out
}
Expand Down
36 changes: 36 additions & 0 deletions R/token.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#' Get, set and forget authentification token.
#'
#' @param token authentification token (obtained at <https://owlbot.info/>).
#'
#' @export
rob_get_secret <- function() {
val <- Sys.getenv("OWLBOT_TOKEN")
if (identical(val, "")) {
message("
`OWLBOT_TOKEN` env var has not been set yet.
A token is required to use the OWLBOT API, see
https://owlbot.info/
")
rob_set_token()
val <- rob_get_secret()
}
val
}

#' @describeIn rob_get_secret set token interactively.
#' @export
rob_set_token <- function(token = NULL) {
if (is.null(token))
token <- readline("Enter your token without quotes: ")
if (identical(token, "")) {
stop("No token has been provided.")
} else {
Sys.setenv(OWLBOT_TOKEN = token)
cat("Authentication token stored for the session.\n")
}
invisible(NULL)
}

#' @describeIn rob_get_secret forget token.
#' @export
forget_token <- function() Sys.unsetenv("OWLBOT_TOKEN")
35 changes: 2 additions & 33 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,14 @@ rob_url <- function(...) paste0(rob_baseurl(), ...)
rob_get <- function(q_url, token, ...) {
token <- paste0("Token ", token)
names(token) <- "Authorization"
print(
httr::GET(q_url, httr::add_headers(token), ...)
)
httr::GET(q_url, httr::add_headers(token), ...)
}

rob_res <- function(q_url, token = rob_getsecret(), ...) {
rob_res <- function(q_url, token, ...) {
con <- rob_get(q_url, token, ...)
# check status
httr::stop_for_status(con)
# parsed
print(httr::content(con, "raw", ...))
httr::content(con, "parsed", ...)
httr::content(con, "parsed")
}

rob_getsecret <- function() {
val <- Sys.getenv("OWLBOT_TOKEN")
if (identical(val, "")) {
message("
`OWLBOT_TOKEN` env var has not been set yet.
A token is required to use the OWLBOT API, see
https://owlbot.info/
")
rob_set_token()
val <- rob_getsecret()
}
val
}

rob_set_token <- function(token = NULL) {
if (is.null(token))
token <- readline("Enter your token without quotes: ")
if (identical(token, "")) {
stop("No token has been provided.")
} else {
Sys.setenv(OWLBOT_TOKEN = token)
cat("Authentication token stored for the session.\n")
}
invisible(NULL)
}

forget_token <- function() Sys.unsetenv("OWLBOT_TOKEN")
27 changes: 27 additions & 0 deletions man/rob_get_secret.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions man/rowlbot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions tests/testthat/owl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ http_interactions:
reason: OK
message: 'Success: (200) OK'
headers:
date: Sun, 25 Oct 2020 23:04:07 GMT
date: Tue, 27 Oct 2020 19:08:12 GMT
content-type: application/json
set-cookie: __cfduid=d4618ead107327827936cb8fe6c3edf761603667047; expires=Tue,
24-Nov-20 23:04:07 GMT; path=/; domain=.owlbot.info; HttpOnly; SameSite=Lax
set-cookie: __cfduid=dc2f669d83bdfc2bc59eec9d0f6adc1f61603825691; expires=Thu,
26-Nov-20 19:08:11 GMT; path=/; domain=.owlbot.info; HttpOnly; SameSite=Lax
allow: GET, HEAD, OPTIONS
x-frame-options: SAMEORIGIN
vary: Accept, Origin
cf-cache-status: DYNAMIC
cf-request-id: 06039a4afb00003fdb2fbb4000000001
cf-request-id: 060d0f051a0000b2592a057000000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=QYvwzGSF38T2wQANJwc4WTMg8jguJXCOecpiuYmv4%2BBlJ9A%2BG3v7FPYwBTg8vv5pCqCQPFxL%2FQPoAmeL7BUwKaK%2B3zAzVgZBjyApxQ%3D%3D"}],"group":"cf-nel","max_age":604800}'
report-to: '{"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=qv1iE6nKxlUPxedcSQZD7PjXnagLaZqY3XSafmM1V74UYiqBocCAp0pBLWYxivwNzCu9%2BsV7vstNYo89BznWpF8jOQ7HaEqIPbV%2BKDqgRRjAl8%2BKfajyXw%3D%3D"}],"group":"cf-nel","max_age":604800}'
nel: '{"report_to":"cf-nel","max_age":604800}'
server: cloudflare
cf-ray: 5e7f9324c9c03fdb-YUL
cf-ray: 5e8eb44e8e90b259-YYC
content-encoding: br
body:
encoding: UTF-8
file: no
string: "{\"definitions\":[{\"type\":\"noun\",\"definition\":\"a nocturnal bird
of prey with large eyes, a facial disc, a hooked beak, and typically a loud
hooting call.\",\"example\":\"I love reaching out into that absolute silence,
when you can hear the owl or the wind.\",\"image_url\":\"https://media.owlbot.info/dictionary/images/owl.jpg.400x400_q85_box-403,83,960,640_crop_detail.jpg\",\"emoji\":\"\U0001F989\"}],\"word\":\"owl\",\"pronunciation\":\"oul\"}"
recorded_at: 2020-10-25 23:04:07 GMT
when you can hear the owl or the wind.\",\"image_url\":\"https://media.owlbot.info/dictionary/images/owl.jpg.400x400_q85_box-403,83,960,640_crop_detail.jpg\",\"emoji\":\"\U0001F989\"}],\"pronunciation\":\"oul\",\"word\":\"owl\"}"
recorded_at: 2020-10-27 19:08:12 GMT
recorded_with: vcr/0.5.4, webmockr/0.7.0

0 comments on commit 8f99a9d

Please sign in to comment.