R/RestService.r

# Netherlands Biodiversity API
#
# Access to the digitised Natural History collection at the Naturalis Biodiversity Center
#
# OpenAPI spec version: v2
# Contact: support@naturalis.nl
# Generated by: https://github.com/swagger-api/swagger-codegen.git

#'
#' @docType class
#'
#' @format R6 class
#'
#' @usage
#' # RestService$new()
#'
#' @format
#' R6 class
#'
#' @title RestService Class
#'
#' @description
#' For more information on the NBA object model, please refer to the
#' official NBA documentation at
#' \href{https://docs.biodiversitydata.nl}{https://docs.biodiversitydata.nl} and
#' the NBA model and endpoints reference at
#' \href{https://docs.biodiversitydata.nl/endpoints-reference}{https://docs.biodiversitydata.nl/endpoints-reference}.
#'
#' @details Model class for RestService objects.
#'
#'
#' @field endPoint  character
#'
#' @field method  character
#'
#' @field consumes  character
#'
#' @field produces  character
#'
#' @field url  character
#'
#'
#'
#' @section Methods:
#' \describe{
#'
#' \item{\code{$new()}}{
#'
#'   Constructor RestService object.
#'
#' }
#' \item{\code{$fromList(RestServiceList)}}{
#'
#'   Create RestService object from list.
#'
#' }
#'
#' \item{\code{$toList()}}{
#'
#'   Get list representation of RestService.
#'
#' }
#' \item{\code{fromJSONString(RestServiceJson)}}{
#'
#'   Create RestService object from JSON.
#'
#' }
#' \item{\code{toJSONString(pretty=TRUE)}}{
#'
#'   Get JSON representation of RestService.
#'
#' }
#' }
#' @importFrom R6 R6Class
#' @importFrom jsonlite fromJSON toJSON
#' @export
RestService <- R6::R6Class(
  "RestService",
  public = list(
    `endPoint` = NULL,
    `method` = NULL,
    `consumes` = NULL,
    `produces` = NULL,
    `url` = NULL,
    initialize = function(
                              `endPoint`,
                              `method`,
                              `consumes`,
                              `produces`,
                              `url`) {
      if (!missing(`endPoint`)) {
        stopifnot(
          is.character(`endPoint`),
          length(`endPoint`) == 1
        )
        self[["endPoint"]] <- `endPoint`
      }
      if (!missing(`method`)) {
        stopifnot(
          is.character(`method`),
          length(`method`) == 1
        )
        self[["method"]] <- `method`
      }
      if (!missing(`consumes`)) {
        stopifnot(
          is.character(`consumes`),
          length(`consumes`) == 1
        )
        self[["consumes"]] <- `consumes`
      }
      if (!missing(`produces`)) {
        stopifnot(
          is.character(`produces`),
          length(`produces`) == 1
        )
        self[["produces"]] <- `produces`
      }
      if (!missing(`url`)) {
        stopifnot(
          is.character(`url`),
          length(`url`) == 1
        )
        self[["url"]] <- `url`
      }
    },
    toList = function() {
      RestServiceList <- list()
      if (!is.null(self[["endPoint"]])) {
        RestServiceList[["endPoint"]] <-
          self[["endPoint"]]
      }
      if (!is.null(self[["method"]])) {
        RestServiceList[["method"]] <-
          self[["method"]]
      }
      if (!is.null(self[["consumes"]])) {
        RestServiceList[["consumes"]] <-
          self[["consumes"]]
      }
      if (!is.null(self[["produces"]])) {
        RestServiceList[["produces"]] <-
          self[["produces"]]
      }
      if (!is.null(self[["url"]])) {
        RestServiceList[["url"]] <-
          self[["url"]]
      }
      ## omit empty nested lists in returned list
      RestServiceList[vapply(
        RestServiceList,
        length,
        FUN.VALUE = integer(1)
      ) > 0]
    },
    fromList = function(RestServiceList,
                            typeMapping = NULL) {
      self[["endPoint"]] <-
        RestServiceList[["endPoint"]]
      self[["method"]] <-
        RestServiceList[["method"]]
      self[["consumes"]] <-
        RestServiceList[["consumes"]]
      self[["produces"]] <-
        RestServiceList[["produces"]]
      self[["url"]] <-
        RestServiceList[["url"]]
      invisible(self)
    },
    toJSONString = function(pretty = TRUE) {
      jsonlite::toJSON(
        self$toList(),
        simplifyVector = TRUE,
        auto_unbox = TRUE,
        pretty = pretty
      )
    },
    fromJSONString = function(RestServiceJson,
                                  typeMapping = NULL) {
      RestServiceList <- jsonlite::fromJSON(
        RestServiceJson,
        simplifyVector = FALSE
      )
      self <- self$fromList(RestServiceList)
      invisible(self)
    },
    print = function(...) {
      ## print class name
      cat("<RestService>\n")
      ## print all members with values
      cat("Fields:\n")
      if (typeof(self$endPoint) == "environment") {
        cat("\tendPoint:\tobject of class", paste0("<", class(self$endPoint)[1], ">"), "\n")
      }
      else if (typeof(self$endPoint) == "list") {
        cat("\tendPoint:\tlist of length", length(self$endPoint), "\n")
      }
      else {
        cat("\tendPoint:\t", self$endPoint, "\n")
      }
      if (typeof(self$method) == "environment") {
        cat("\tmethod:\tobject of class", paste0("<", class(self$method)[1], ">"), "\n")
      }
      else if (typeof(self$method) == "list") {
        cat("\tmethod:\tlist of length", length(self$method), "\n")
      }
      else {
        cat("\tmethod:\t", self$method, "\n")
      }
      if (typeof(self$consumes) == "environment") {
        cat("\tconsumes:\tobject of class", paste0("<", class(self$consumes)[1], ">"), "\n")
      }
      else if (typeof(self$consumes) == "list") {
        cat("\tconsumes:\tlist of length", length(self$consumes), "\n")
      }
      else {
        cat("\tconsumes:\t", self$consumes, "\n")
      }
      if (typeof(self$produces) == "environment") {
        cat("\tproduces:\tobject of class", paste0("<", class(self$produces)[1], ">"), "\n")
      }
      else if (typeof(self$produces) == "list") {
        cat("\tproduces:\tlist of length", length(self$produces), "\n")
      }
      else {
        cat("\tproduces:\t", self$produces, "\n")
      }
      if (typeof(self$url) == "environment") {
        cat("\turl:\tobject of class", paste0("<", class(self$url)[1], ">"), "\n")
      }
      else if (typeof(self$url) == "list") {
        cat("\turl:\tlist of length", length(self$url), "\n")
      }
      else {
        cat("\turl:\t", self$url, "\n")
      }
      ## print all methods
      cat("Methods:\n")
      cat("\tfromJSONString\n")
      cat("\ttoJSONString\n")
      cat("\tfromList\n")
      cat("\ttoList\n")
      cat("\tprint\n")
      invisible(self)
    }
  )
)
naturalis/nbaR documentation built on Nov. 12, 2023, 4:47 p.m.