R/ApiClient.r

Defines functions connectToOpenSILEX checkAuthorization

Documented in checkAuthorization connectToOpenSILEX

# OpenSilex API
#
# No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
#
# OpenAPI spec version: 1.0.0-rc+2
# 
# Generated by: https://github.com/swagger-api/swagger-codegen.git


#' ApiClient Class
#'
#' Generic API client for Swagger client library builds.
#' Swagger generic API client. This client handles the client-
#' server communication, and is invariant across implementations. Specifics of
#' the methods and models for each application are generated from the Swagger
#' templates.
#'
#' NOTE: This class is auto generated by the swagger code generator program.
#' Ref: https://github.com/swagger-api/swagger-codegen
#' Do not edit the class manually.
#'
#' @export
ApiClient  <- R6::R6Class(
  'ApiClient',
  public = list(
    basePath =  sub("/$", "",get("BASE_PATH",opensilexWSClientR:::configWS)),
    configuration = NULL,
    userAgent = NULL,
    defaultHeaders = NULL,
    initialize = function(basePath, configuration, defaultHeaders){
        if (!missing(basePath)) {
            self$basePath <- basePath
        }

        if (!missing(configuration)) {
            self$configuration <- configuration
        }

        if (!missing(defaultHeaders)) {
            self$defaultHeaders <- defaultHeaders
        }

        self$`userAgent` <- 'Swagger-Codegen/1.0.0/r'
    },
    callApi = function(url, method, queryParams, headerParams, body, ...){
       
        checkAuthorization()
        access_token = paste('Bearer',get("TOKEN_VALUE",opensilexWSClientR:::configWS))
        headerParams[['Authorization']] = access_token
        headers <- httr::add_headers(headerParams)

        if (method == "GET") {
            httr::GET(url,query = queryParams, headers, ...)
        }
        else if (method == "POST") {
            httr::POST(url, query = queryParams, headers, body = body, httr::content_type_json(), ...)
        }
        else if (method == "PUT") {
            httr::PUT(url, query = queryParams, headers, body = body, ...)
        }
        else if (method == "PATCH") {
            httr::PATCH(url, query = queryParams, headers, body = body, ...)
        }
        else if (method == "HEAD") {
            httr::HEAD(url, query = queryParams, headers, ...)
        }
        else if (method == "DELETE") {
            httr::DELETE(url,query = queryParams, headers, ...)
        }
        else {
            stop("http method must be `GET`, `HEAD`, `OPTIONS`, `POST`, `PATCH`, `PUT` or `DELETE`.")
        }
    }
  )
)

##' @title checkAuthorization
##' @description Test WS connection at before each call
##' @keywords internal
checkAuthorization <-function(){
    if(!get("USER_VALID",opensilexWSClientR:::configWS)) {
      stop("You must first connect to an OpenSILEX Instance using connectToOpenSILEXWS() function")
    }

    if(!is.null(get("IDENTIFIER",opensilexWSClientR:::configWS))
     && !is.null(get("PASSWORD",opensilexWSClientR:::configWS)) 
     && !is.null(get("BASE_PATH",opensilexWSClientR:::configWS))){
      connectToOpenSILEXWS(get("IDENTIFIER",opensilexWSClientR:::configWS),get("PASSWORD",opensilexWSClientR:::configWS),get("BASE_PATH",opensilexWSClientR:::configWS))
    }
}

##' @title connectToOpenSILEX
##' @param url character, if apiID is private add the url of the chosen API, containing the IP,
##'            the full url with the protocol. e.g. 'http://www.opensilex.org/openSilexAPI/rest'
##' @param identifier login of the user to create the token
##' @param password password of the user to create the token
##' @param reconnection to force the client reconnection
##' @import opensilexWSClientR
##' @description load name space and connexion parameters of the webservice.
##' Execute only once at the beginning of the requests.
##' In the case of a WebService change of address or a renaming of services, please edit this list.
##' and execute the function.
##' Demonstration instances :
##' \describe{
##' connectToOpenSILEX(identifier="guest@opensilex.org",password="guest",
##' url = "http://www.opensilex.org/openSilexAPI/rest")
##' }
##' @export
connectToOpenSILEX<-function(identifier = NULL, password = NULL, url = NULL, reconnection = TRUE){
  # configWS is an environment with specific variables to opensilex web service
  # if apiID is private, we use the url given by the user
  if (is.null(identifier)) {
    stop("Please, give an identifier")
  }
  if (is.null(password)) {
    stop("Please, give an user password")
  }
  if(is.null(url)){
    stop("Please, give an OpenSILEX WS full URL")
  }
  opensilexWSClientR::connectToOpenSILEXWS(identifier = identifier,
                                           password = password,
                                           url = url)

}
OpenSILEX/opensilexClientToolsR documentation built on Jan. 28, 2022, 5:03 a.m.