R/synapse_helpers.R

Defines functions logged_in attempt_login attempt_instantiate

Documented in attempt_login logged_in

# Try to get Synapse client object
attempt_instantiate <- function() {
    if (reticulate::py_module_available("synapseclient")) {
        return(synapseclient$Synapse())
    } else {
        return(NULL)
    }
}

#' @title Attempt to log into Synapse
#'
#' @description Attempt to log into Synapse. Will first try using authentication
#' credentials written to a .synapseConfig file. If that fails, will try using
#' any credentials passed to the function. Will return `NULL` if not all
#' attempts failed.
#'
#' @export
#' @param syn Synapse client object
#' @param ... Synapse credentials, such as `authToken` or `email` with a
#' `password` or `apiKey`.
attempt_login <- function(syn, ...) {
    is_logged_in <- FALSE
    ## If failed to login, try using credentials provided
    if (!is_logged_in) {
        tryCatch(
            {
                syn$login(...)
            },
            error = function(e) {
                stop("There was a problem logging in.")
            }
        )
    }
}

#' @title Check if logged in as user
#'
#' @description Check if logged into Synapse as a non-anonymous user.
#'
#' @param syn Synapse client object.
#' @return FALSE if not logged in at all or if logged in anonymously, else TRUE.
logged_in <- function(syn) {
    stopifnot(inherits(syn, "synapseclient.client.Synapse"))
    if (is.null(syn) || is.null(syn$username) || (syn$username == "anonymous")) {
        return(FALSE)
    } else {
        return(TRUE)
    }
}
Sage-Bionetworks/mhealthannotator documentation built on Jan. 28, 2022, 6:08 a.m.