diff --git a/NAMESPACE b/NAMESPACE index 62343a4..d06f87b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -9,6 +9,7 @@ export(current_session) export(folder_create) export(identifier_exists) export(is.sbitem) +export(is_logged_in) export(item_append_files) export(item_create) export(item_exists) @@ -28,6 +29,7 @@ export(item_upload_create) export(query_item_identifier) export(query_item_in_folder) export(query_items) +export(sb_ping) export(sbtools_DELETE) export(sbtools_GET) export(sbtools_POST) @@ -36,6 +38,8 @@ export(session_age) export(session_age_reset) export(session_check_reauth) export(session_expired) +export(session_info) +export(session_logout) export(session_validate) export(set_endpoint) export(set_expiration) diff --git a/R/is_logged_in.R b/R/is_logged_in.R new file mode 100644 index 0000000..13b6098 --- /dev/null +++ b/R/is_logged_in.R @@ -0,0 +1,12 @@ +#' Check whether you're logged into a ScienceBase session +#' +#' @export +#' @param ... Additional parameters are passed on to \code{\link[httr]{GET}} +#' @param session SB session object from \code{\link{authenticate_sb}} +#' @return Logical, \code{TRUE} or \code{FALSE} +#' @examples \dontrun{ +#' is_logged_in() +#' } +is_logged_in <- function(..., session = current_session()) { + session_info(..., session = session)$isLoggedIn +} diff --git a/R/sb_ping.R b/R/sb_ping.R new file mode 100644 index 0000000..da538f5 --- /dev/null +++ b/R/sb_ping.R @@ -0,0 +1,12 @@ +#' Ping ScienceBase to see if it's available +#' +#' @export +#' @param ... Additional parameters are passed on to \code{\link[httr]{GET}} +#' @return list, with \code{"OK"} if ScienceBase is up +#' @examples \dontrun{ +#' sb_ping() +#' } +sb_ping <- function(...) { + x <- GET(paste0(pkg.env$url_item, 'ping'), ...) + jsonlite::fromJSON(content(x, "text")) +} diff --git a/R/session_info.R b/R/session_info.R new file mode 100644 index 0000000..3925ab2 --- /dev/null +++ b/R/session_info.R @@ -0,0 +1,15 @@ +#' Get session info +#' +#' @export +#' @param ... Additional parameters are passed on to \code{\link[httr]{GET}} +#' @param session SB session object from \code{\link{authenticate_sb}} +#' @return list, if not logged in states that, but if logged in, user details +#' @examples \dontrun{ +#' session_info() +#' } +session_info <- function(..., session = current_session()) { + x <- GET(paste0(pkg.env$url_base, "jossoHelper/sessionInfo"), + handle = session, ...) + stop_for_status(x) + jsonlite::fromJSON(content(x, "text")) +} diff --git a/R/session_logout.R b/R/session_logout.R new file mode 100644 index 0000000..4ccb0cb --- /dev/null +++ b/R/session_logout.R @@ -0,0 +1,20 @@ +#' Logout of a ScienceBase session +#' +#' @export +#' @param ... Additional parameters are passed on to \code{\link[httr]{GET}} +#' @param session SB session object from \code{\link{authenticate_sb}} +#' @return invisible, returns nothing if logged out, or errors with message +#' @examples \dontrun{ +#' session_logout() +#' } +session_logout <- function(..., session = current_session()) { + if (!is_logged_in(session = session)) { + stop("You're not logged in. See ?authenticate_sb", call. = FALSE) + } + ret <- httr::POST(paste0(pkg.env$url_base, 'j_spring_security_logout'), handle = session, ...) + if (ret$status_code != 200) { + stop("Logout did not succeed", call. = FALSE) + } + pkg.env$session <- NULL + invisible() +} diff --git a/man/is_logged_in.Rd b/man/is_logged_in.Rd new file mode 100644 index 0000000..f683bd2 --- /dev/null +++ b/man/is_logged_in.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/is_logged_in.R +\name{is_logged_in} +\alias{is_logged_in} +\title{Check whether you're logged into a ScienceBase session} +\usage{ +is_logged_in(..., session = current_session()) +} +\arguments{ +\item{...}{Additional parameters are passed on to \code{\link[httr]{GET}}} + +\item{session}{SB session object from \code{\link{authenticate_sb}}} +} +\value{ +Logical, \code{TRUE} or \code{FALSE} +} +\description{ +Check whether you're logged into a ScienceBase session +} +\examples{ +\dontrun{ +is_logged_in() +} +} + diff --git a/man/sb_ping.Rd b/man/sb_ping.Rd new file mode 100644 index 0000000..6ac918d --- /dev/null +++ b/man/sb_ping.Rd @@ -0,0 +1,23 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/sb_ping.R +\name{sb_ping} +\alias{sb_ping} +\title{Ping ScienceBase to see if it's available} +\usage{ +sb_ping(...) +} +\arguments{ +\item{...}{Additional parameters are passed on to \code{\link[httr]{GET}}} +} +\value{ +list, with \code{"OK"} if ScienceBase is up +} +\description{ +Ping ScienceBase to see if it's available +} +\examples{ +\dontrun{ +sb_ping() +} +} + diff --git a/man/session_info.Rd b/man/session_info.Rd new file mode 100644 index 0000000..0ac2e8e --- /dev/null +++ b/man/session_info.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/session_info.R +\name{session_info} +\alias{session_info} +\title{Get session info} +\usage{ +session_info(..., session = current_session()) +} +\arguments{ +\item{...}{Additional parameters are passed on to \code{\link[httr]{GET}}} + +\item{session}{SB session object from \code{\link{authenticate_sb}}} +} +\value{ +list, if not logged in states that, but if logged in, user details +} +\description{ +Get session info +} +\examples{ +\dontrun{ +session_info() +} +} + diff --git a/man/session_logout.Rd b/man/session_logout.Rd new file mode 100644 index 0000000..2725a35 --- /dev/null +++ b/man/session_logout.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/session_logout.R +\name{session_logout} +\alias{session_logout} +\title{Logout of a ScienceBase session} +\usage{ +session_logout(..., session = current_session()) +} +\arguments{ +\item{...}{Additional parameters are passed on to \code{\link[httr]{GET}}} + +\item{session}{SB session object from \code{\link{authenticate_sb}}} +} +\value{ +invisible, returns nothing if logged out, or errors with message +} +\description{ +Logout of a ScienceBase session +} +\examples{ +\dontrun{ +session_logout() +} +} +