Skip to content

Commit

Permalink
Merge pull request #5 from pegeler/master
Browse files Browse the repository at this point in the history
Changed the way API keys are handled ropensci/software-review#190
  • Loading branch information
evanodell authored Apr 17, 2018
2 parents 079783b + a133309 commit a3fb066
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 41 deletions.
10 changes: 7 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 35 additions & 25 deletions R/api-key.R
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()

}
6 changes: 3 additions & 3 deletions R/data_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#' Retrieve Nomis datasets
#'
#' @description Retrieves specific datasets from nomis, based on their ID. To
#' @description Retrieves specific datasets from Nomis, based on their ID. To
#' find dataset IDs, use \code{\link{nomis_data_info}}. Datasets are retrived
#' in csv format and parsed with the \code{read_csv} function from the
#' \code{readr} package into a tibble, with all columns parsed as character
Expand Down Expand Up @@ -204,9 +204,9 @@ nomis_get_data <- function(id, time = NULL, date = NULL, geography = NULL,
""
)

if(nchar(Sys.getenv('NOMIS_API_KEY')) > 0) {
if(!is.null(getOption("nomisr.API.key"))) {

api_query <- paste0("&uid=", nomis_api_key())
api_query <- paste0("&uid=", getOption("nomisr.API.key"))
max_length <- 100000

} else {
Expand Down
11 changes: 11 additions & 0 deletions R/nomisr-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@
#' @importFrom dplyr bind_rows
#' @importFrom utils menu
NULL

# Checking for API key on package load
.onLoad <- function(libname, pkgname) {

if (is.null(getOption("nomisr.API.key"))) {
key <- Sys.getenv('NOMIS_API_KEY')
if (key != "") options("nomisr.API.key" = key)
}

invisible()
}
26 changes: 17 additions & 9 deletions man/nomis_api_key.Rd

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

2 changes: 1 addition & 1 deletion man/nomis_get_data.Rd

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

0 comments on commit a3fb066

Please sign in to comment.