-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from pegeler/master
Changed the way API keys are handled ropensci/software-review#190
- Loading branch information
Showing
6 changed files
with
74 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,13 @@ Package: nomisr | |
Type: Package | ||
Title: Access Nomis UK Labour Market Data with R | ||
Version: 0.1.0 | ||
Authors@R: person("Evan Odell", email="[email protected]", | ||
role=c("aut", "cre"), | ||
comment = c(ORCID='0000-0003-1845-808X')) | ||
Authors@R: c( | ||
person( | ||
"Evan", "Odell", email = "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID='0000-0003-1845-808X')), | ||
person( | ||
"Paul", "Egeler", email = "[email protected]", role = c("rev", "ctb"), | ||
comment = "Reviewed package for rOpenSci: ropensci/onboarding#190")) | ||
Description: Access UK official statistics from the Nomis database through R. | ||
Nomis includes data from the Census, the Labour Force Survey, DWP benefit | ||
statistics and other economic and demographic data from the Office for | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,51 @@ | ||
|
||
#' Nomis API Key | ||
#' | ||
#' @description The Nomis API has an optional key. Using the key means that | ||
#' 100,000 rows can be returned per call, which can speed up larger data | ||
#' requests and reduce the chances of being rate limited or having requests | ||
#' timing out. | ||
#' @description Assign or reassign API key for Nomis. | ||
#' | ||
#' @description You can sign up for an API key | ||
#' \href{https://www.nomisweb.co.uk/myaccount/userjoin.asp}{here}. | ||
#' @details The Nomis API has an optional key. Using the key means that 100,000 | ||
#' rows can be returned per call, which can speed up larger data requests and | ||
#' reduce the chances of being rate limited or having requests timing out. | ||
#' | ||
#' @param force If TRUE, resets the API key and requires a | ||
#' new key to be provided. | ||
#' @details Be default, \code{nomisr} will look for the environment variable | ||
#' \code{NOMIS_API_KEY} when the package is loaded. If found, the API key will | ||
#' be stored in the session option \code{nomisr.API.key}. If you would like to | ||
#' reload the API key or would like to manually enter one in, this function | ||
#' may be used. | ||
#' | ||
#' @details You can sign up for an API key | ||
#' \href{https://www.nomisweb.co.uk/myaccount/userjoin.asp}{here}. | ||
#' | ||
#' @param check_env If TRUE, will check the environment variable | ||
#' \code{NOMIS_API_KEY} first before asking for user input. | ||
#' | ||
#' @export | ||
nomis_api_key <- function(force = FALSE) { | ||
nomis_api_key <- function(check_env = FALSE) { | ||
|
||
env <- Sys.getenv('NOMIS_API_KEY') | ||
if (!identical(env, "") && !force) return(env) | ||
|
||
if (!interactive()) { | ||
stop("Please set environment variable NOMIS_API_KEY to your Nomis API key", | ||
call. = FALSE) | ||
message("Use `Sys.setenv(NOMIS_API_KEY = <key>)`") | ||
if (check_env) { | ||
key <- Sys.getenv('NOMIS_API_KEY') | ||
if (key != "") { | ||
message("Updating NOMIS_API_KEY environment variable...") | ||
options("nomisr.API.key" = key) | ||
return(invisible()) | ||
} else { | ||
warning("Couldn't find environment variable 'NOMIS_API_KEY'") | ||
} | ||
} | ||
|
||
message("Couldn't find environment variable NOMIS_API_KEY") | ||
message("Please enter your API key and press enter:") | ||
key <- readline(": ") | ||
if (interactive()) { | ||
key <- readline("Please enter your API key and press enter: ") | ||
} else { | ||
cat("Please enter your API key and press enter: ") | ||
key <- readLines(con = "stdin", n = 1) | ||
} | ||
|
||
if (identical(key, "")) { | ||
Sys.unsetenv('NOMIS_API_KEY') | ||
stop("Nomis API key entry failed", call. = FALSE) | ||
} | ||
|
||
message("Updating NOMIS_API_KEY") | ||
Sys.setenv(NOMIS_API_KEY = key) | ||
|
||
key | ||
|
||
message("Updating NOMIS_API_KEY environment variable...") | ||
options("nomisr.API.key" = key) | ||
invisible() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.