Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/apikey #28

Merged
merged 4 commits into from
May 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export(ping)
export(resource_search)
export(resource_show)
export(revision_list)
export(set_api_key)
export(set_ckanr_url)
export(tag_list)
export(tag_search)
Expand Down
34 changes: 34 additions & 0 deletions R/api_key.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#' Set API KEY of CKAN
#'
#' Some CKAN API functions require authentication.
#' The \code{set_api_key} is a helper function to set the api key and
#' enable authentication.
#'
#' @details
#'
#' The \code{ckanr} will check the value of global options \code{X-CKAN-API-Key}
#' before communicating with CKAN server. If the value is not \code{NULL}, the
#' \code{ckanr} will use the value as api key to authenticate with CKAN service.
#'
#' @param api_key (character). The api key of the user.
#' @examples
#' \dontrun{
#' set_api_key("xxx")
#' # list the private resources authorized to the above api key
#' organization_show("some_name", include_datasets = TRUE)[["packages"]]
#' }
#' @export
set_api_key <- function(api_key) {
options("X-CKAN-API-Key" = api_key)
}

#' Enable Authentication with API-KEY
#'
#' Some CKAN API functions require authentication.
#' The \code{set_api_key} is a helper function to set the API-KEY and
#' the \code{api_key} is a helper function to enable authentication.
#' @importFrom httr add_headers
api_key <- function() {
value <- getOption("X-CKAN-API-Key", NULL)
if (is.null(value)) NULL else add_headers("X-CKAN-API-Key" = value)
}
17 changes: 14 additions & 3 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,21 @@ jsd <- function(x){
}

ckan_POST <- function(url, method, body=NULL, ...){
if (is.null(body) || length(body) == 0) {
res <- POST(file.path(url, ck(), method), ctj(), ...)
api_key_header <- api_key()
if (is.null(api_key_header)) {
# no authentication
if (is.null(body) || length(body) == 0) {
res <- POST(file.path(url, ck(), method), ctj(), ...)
} else {
res <- POST(file.path(url, ck(), method), body = body, ...)
}
} else {
res <- POST(file.path(url, ck(), method), body = body, ...)
# authentication
if (is.null(body) || length(body) == 0) {
res <- POST(file.path(url, ck(), method), ctj(), api_key_header, ...)
} else {
res <- POST(file.path(url, ck(), method), body = body, api_key_header, ...)
}
}
err_handler(res)
content(res, "text")
Expand Down
14 changes: 14 additions & 0 deletions man/api_key.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/api_key.R
\name{api_key}
\alias{api_key}
\title{Enable Authentication with API-KEY}
\usage{
api_key()
}
\description{
Some CKAN API functions require authentication.
The \code{set_api_key} is a helper function to set the API-KEY and
the \code{api_key} is a helper function to enable authentication.
}

29 changes: 29 additions & 0 deletions man/set_api_key.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/api_key.R
\name{set_api_key}
\alias{set_api_key}
\title{Set API KEY of CKAN}
\usage{
set_api_key(api_key)
}
\arguments{
\item{api_key}{(character). The api key of the user.}
}
\description{
Some CKAN API functions require authentication.
The \code{set_api_key} is a helper function to set the api key and
enable authentication.
}
\details{
The \code{ckanr} will check the value of global options \code{X-CKAN-API-Key}
before communicating with CKAN server. If the value is not \code{NULL}, the
\code{ckanr} will use the value as api key to authenticate with CKAN service.
}
\examples{
\dontrun{
set_api_key("xxx")
# list the private resources authorized to the above api key
organization_show("some_name", include_datasets = TRUE)[["packages"]]
}
}

13 changes: 13 additions & 0 deletions tests/testthat/test-user_list.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
context("user_list")

test_that("user_list gives back expected class types", {
a <- user_list()
expect_is(a, "list")
})

test_that("user_list works giving back json output", {
b <- user_list(as = "json")
expect_is(b, "character")
b_df <- jsonlite::fromJSON(b)
expect_is(b_df, "list")
})