R/pet_api.R

# OpenAPI Petstore
#
# This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
#
# The version of the OpenAPI document: 1.0.0
# Generated by: https://openapi-generator.tech

#' @docType class
#' @title Pet operations
#' @description petstore.Pet
#' @format An \code{R6Class} generator object
#' @field api_client Handles the client-server communication.
#'
#' @section Methods:
#' \describe{
#' \strong{ AddPet } \emph{ Add a new pet to the store }
#' 
#'
#' \itemize{
#' \item \emph{ @param } body \link{Pet}
#'
#'
#' \item status code : 405 | Invalid input
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ DeletePet } \emph{ Deletes a pet }
#' 
#'
#' \itemize{
#' \item \emph{ @param } pet.id integer
#' \item \emph{ @param } api.key character
#'
#'
#' \item status code : 400 | Invalid pet value
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ FindPetsByStatus } \emph{ Finds Pets by status }
#' Multiple status values can be provided with comma separated strings
#'
#' \itemize{
#' \item \emph{ @param } status Enum < [available, pending, sold] >
#' \item \emph{ @returnType } list( \link{Pet} ) \cr
#'
#'
#' \item status code : 200 | successful operation
#'
#' \item return type : array[Pet] 
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' \item status code : 400 | Invalid status value
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ FindPetsByTags } \emph{ Finds Pets by tags }
#' Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
#'
#' \itemize{
#' \item \emph{ @param } tags list( character )
#' \item \emph{ @returnType } list( \link{Pet} ) \cr
#'
#'
#' \item status code : 200 | successful operation
#'
#' \item return type : array[Pet] 
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' \item status code : 400 | Invalid tag value
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ GetPetById } \emph{ Find pet by ID }
#' Returns a single pet
#'
#' \itemize{
#' \item \emph{ @param } pet.id integer
#' \item \emph{ @returnType } \link{Pet} \cr
#'
#'
#' \item status code : 200 | successful operation
#'
#' \item return type : Pet 
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' \item status code : 400 | Invalid ID supplied
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' \item status code : 404 | Pet not found
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ UpdatePet } \emph{ Update an existing pet }
#' 
#'
#' \itemize{
#' \item \emph{ @param } body \link{Pet}
#'
#'
#' \item status code : 400 | Invalid ID supplied
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' \item status code : 404 | Pet not found
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' \item status code : 405 | Validation exception
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ UpdatePetWithForm } \emph{ Updates a pet in the store with form data }
#' 
#'
#' \itemize{
#' \item \emph{ @param } pet.id integer
#' \item \emph{ @param } name character
#' \item \emph{ @param } status character
#'
#'
#' \item status code : 405 | Invalid input
#'
#'
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ UploadFile } \emph{ uploads an image }
#' 
#'
#' \itemize{
#' \item \emph{ @param } pet.id integer
#' \item \emph{ @param } additional.metadata character
#' \item \emph{ @param } file data.frame
#' \item \emph{ @returnType } \link{ModelApiResponse} \cr
#'
#'
#' \item status code : 200 | successful operation
#'
#' \item return type : ModelApiResponse 
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' }
#'
#'
#' @examples
#' \dontrun{
#' ####################  AddPet  ####################
#'
#' library(petstore)
#' var.body <- Pet$new() # Pet | Pet object that needs to be added to the store
#'
#' #Add a new pet to the store
#' api.instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api.instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
#'
#' result <- api.instance$AddPet(var.body)
#'
#'
#' ####################  DeletePet  ####################
#'
#' library(petstore)
#' var.pet.id <- 56 # integer | Pet id to delete
#' var.api.key <- 'api.key_example' # character | 
#'
#' #Deletes a pet
#' api.instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api.instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
#'
#' result <- api.instance$DeletePet(var.pet.id, api.key=var.api.key)
#'
#'
#' ####################  FindPetsByStatus  ####################
#'
#' library(petstore)
#' var.status <- ['status_example'] # array[character] | Status values that need to be considered for filter
#'
#' #Finds Pets by status
#' api.instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api.instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
#'
#' result <- api.instance$FindPetsByStatus(var.status)
#'
#'
#' ####################  FindPetsByTags  ####################
#'
#' library(petstore)
#' var.tags <- ['tags_example'] # array[character] | Tags to filter by
#'
#' #Finds Pets by tags
#' api.instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api.instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
#'
#' result <- api.instance$FindPetsByTags(var.tags)
#'
#'
#' ####################  GetPetById  ####################
#'
#' library(petstore)
#' var.pet.id <- 56 # integer | ID of pet to return
#'
#' #Find pet by ID
#' api.instance <- PetApi$new()
#'
#' #Configure API key authorization: api_key
#' api.instance$api_client$api_keys['api_key'] <- 'TODO_YOUR_API_KEY';
#'
#' result <- api.instance$GetPetById(var.pet.id)
#'
#'
#' ####################  UpdatePet  ####################
#'
#' library(petstore)
#' var.body <- Pet$new() # Pet | Pet object that needs to be added to the store
#'
#' #Update an existing pet
#' api.instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api.instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
#'
#' result <- api.instance$UpdatePet(var.body)
#'
#'
#' ####################  UpdatePetWithForm  ####################
#'
#' library(petstore)
#' var.pet.id <- 56 # integer | ID of pet that needs to be updated
#' var.name <- 'name_example' # character | Updated name of the pet
#' var.status <- 'status_example' # character | Updated status of the pet
#'
#' #Updates a pet in the store with form data
#' api.instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api.instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
#'
#' result <- api.instance$UpdatePetWithForm(var.pet.id, name=var.name, status=var.status)
#'
#'
#' ####################  UploadFile  ####################
#'
#' library(petstore)
#' var.pet.id <- 56 # integer | ID of pet to update
#' var.additional.metadata <- 'additional.metadata_example' # character | Additional data to pass to server
#' var.file <- File.new('/path/to/file') # data.frame | file to upload
#'
#' #uploads an image
#' api.instance <- PetApi$new()
#'
#' # Configure OAuth2 access token for authorization: petstore_auth
#' api.instance$api_client$access_token <- 'TODO_YOUR_ACCESS_TOKEN';
#'
#' result <- api.instance$UploadFile(var.pet.id, additional.metadata=var.additional.metadata, file=var.file)
#'
#'
#' }
#' @importFrom R6 R6Class
#' @importFrom base64enc base64encode
#' @export
PetApi <- R6::R6Class(
  'PetApi',
  public = list(
    api_client = NULL,
    initialize = function(api_client) {
      if (!missing(api_client)) {
        self$api_client <- api_client
      }
      else {
        self$api_client <- ApiClient$new()
      }
    },
    AddPet = function(body, ...) {
      api_response <- self$AddPetWithHttpInfo(body, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    AddPetWithHttpInfo = function(body, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`body`)) {
        stop("Missing required parameter `body`.")
      }

      if (!missing(`body`)) {
        body <- `body`$toJSONString()
      } else {
        body <- NULL
      }

      url_path <- "/pet"
      # OAuth token
      header_params['Authorization'] <- paste("Bearer", self$api_client$access_token, sep=" ")

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "POST",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        ApiResponse$new(NULL, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    },
    DeletePet = function(pet.id, api.key=NULL, ...) {
      api_response <- self$DeletePetWithHttpInfo(pet.id, api.key, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    DeletePetWithHttpInfo = function(pet.id, api.key=NULL, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`pet.id`)) {
        stop("Missing required parameter `pet.id`.")
      }

      body <- NULL
      url_path <- "/pet/{petId}"
      if (!missing(`pet.id`)) {
        url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet.id`), reserved = TRUE), url_path)
      }

      # OAuth token
      header_params['Authorization'] <- paste("Bearer", self$api_client$access_token, sep=" ")

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "DELETE",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        ApiResponse$new(NULL, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    },
    FindPetsByStatus = function(status, data_file=NULL, ...) {
      api_response <- self$FindPetsByStatusWithHttpInfo(status, data_file=data_file, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    FindPetsByStatusWithHttpInfo = function(status, data_file=NULL, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`status`)) {
        stop("Missing required parameter `status`.")
      }

      body <- NULL
      url_path <- "/pet/findByStatus"
      # OAuth token
      header_params['Authorization'] <- paste("Bearer", self$api_client$access_token, sep=" ")

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "GET",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        # save response in a file
        if (!is.null(data_file)) {
            write(httr::content(resp, "text", encoding = "UTF-8", simplifyVector = FALSE), data_file)
        }

        deserializedRespObj <- tryCatch(
          self$api_client$deserialize(resp, "array[Pet]", loadNamespace("petstore")),
          error = function(e) {
             stop("Failed to deserialize response")
          }
        )
        ApiResponse$new(deserializedRespObj, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    },
    FindPetsByTags = function(tags, data_file=NULL, ...) {
      api_response <- self$FindPetsByTagsWithHttpInfo(tags, data_file=data_file, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    FindPetsByTagsWithHttpInfo = function(tags, data_file=NULL, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`tags`)) {
        stop("Missing required parameter `tags`.")
      }

      body <- NULL
      url_path <- "/pet/findByTags"
      # OAuth token
      header_params['Authorization'] <- paste("Bearer", self$api_client$access_token, sep=" ")

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "GET",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        # save response in a file
        if (!is.null(data_file)) {
            write(httr::content(resp, "text", encoding = "UTF-8", simplifyVector = FALSE), data_file)
        }

        deserializedRespObj <- tryCatch(
          self$api_client$deserialize(resp, "array[Pet]", loadNamespace("petstore")),
          error = function(e) {
             stop("Failed to deserialize response")
          }
        )
        ApiResponse$new(deserializedRespObj, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    },
    GetPetById = function(pet.id, data_file=NULL, ...) {
      api_response <- self$GetPetByIdWithHttpInfo(pet.id, data_file=data_file, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    GetPetByIdWithHttpInfo = function(pet.id, data_file=NULL, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`pet.id`)) {
        stop("Missing required parameter `pet.id`.")
      }

      body <- NULL
      url_path <- "/pet/{petId}"
      if (!missing(`pet.id`)) {
        url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet.id`), reserved = TRUE), url_path)
      }

      # API key authentication
      if ("api_key" %in% names(self$api_client$api_keys) && nchar(self$api_client$api_keys["api_key"]) > 0) {
        header_params['api_key'] <- paste(unlist(self$api_client$api_keys["api_key"]), collapse='')
      }

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "GET",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        # save response in a file
        if (!is.null(data_file)) {
            write(httr::content(resp, "text", encoding = "UTF-8", simplifyVector = FALSE), data_file)
        }

        deserializedRespObj <- tryCatch(
          self$api_client$deserialize(resp, "Pet", loadNamespace("petstore")),
          error = function(e) {
             stop("Failed to deserialize response")
          }
        )
        ApiResponse$new(deserializedRespObj, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    },
    UpdatePet = function(body, ...) {
      api_response <- self$UpdatePetWithHttpInfo(body, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    UpdatePetWithHttpInfo = function(body, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`body`)) {
        stop("Missing required parameter `body`.")
      }

      if (!missing(`body`)) {
        body <- `body`$toJSONString()
      } else {
        body <- NULL
      }

      url_path <- "/pet"
      # OAuth token
      header_params['Authorization'] <- paste("Bearer", self$api_client$access_token, sep=" ")

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "PUT",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        ApiResponse$new(NULL, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    },
    UpdatePetWithForm = function(pet.id, name=NULL, status=NULL, ...) {
      api_response <- self$UpdatePetWithFormWithHttpInfo(pet.id, name, status, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    UpdatePetWithFormWithHttpInfo = function(pet.id, name=NULL, status=NULL, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`pet.id`)) {
        stop("Missing required parameter `pet.id`.")
      }

      body <- list(
        "name" = name,
        "status" = status
      )

      url_path <- "/pet/{petId}"
      if (!missing(`pet.id`)) {
        url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet.id`), reserved = TRUE), url_path)
      }

      # OAuth token
      header_params['Authorization'] <- paste("Bearer", self$api_client$access_token, sep=" ")

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "POST",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        ApiResponse$new(NULL, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    },
    UploadFile = function(pet.id, additional.metadata=NULL, file=NULL, data_file=NULL, ...) {
      api_response <- self$UploadFileWithHttpInfo(pet.id, additional.metadata, file, data_file=data_file, ...)
      resp <- api_response$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        api_response$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        api_response
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        api_response
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        api_response
      }
    },

    UploadFileWithHttpInfo = function(pet.id, additional.metadata=NULL, file=NULL, data_file=NULL, ...) {
      args <- list(...)
      query_params <- list()
      header_params <- c()

      if (missing(`pet.id`)) {
        stop("Missing required parameter `pet.id`.")
      }

      body <- list(
        "additionalMetadata" = additional.metadata,
        "file" = httr::upload_file(file)
      )

      url_path <- "/pet/{petId}/uploadImage"
      if (!missing(`pet.id`)) {
        url_path <- gsub(paste0("\\{", "petId", "\\}"), URLencode(as.character(`pet.id`), reserved = TRUE), url_path)
      }

      # OAuth token
      header_params['Authorization'] <- paste("Bearer", self$api_client$access_token, sep=" ")

      resp <- self$api_client$CallApi(url = paste0(self$api_client$base_path, url_path),
                                 method = "POST",
                                 query_params = query_params,
                                 header_params = header_params,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        # save response in a file
        if (!is.null(data_file)) {
            write(httr::content(resp, "text", encoding = "UTF-8", simplifyVector = FALSE), data_file)
        }

        deserializedRespObj <- tryCatch(
          self$api_client$deserialize(resp, "ModelApiResponse", loadNamespace("petstore")),
          error = function(e) {
             stop("Failed to deserialize response")
          }
        )
        ApiResponse$new(deserializedRespObj, resp)
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        ApiResponse$new(paste("Server returned ", httr::status_code(resp), " response status code."), resp)
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        ApiResponse$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        ApiResponse$new("API server error", resp)
      }
    }
  )
)
wing328/petstore-r-client documentation built on June 2, 2022, 10:24 a.m.