All Downloads are FREE. Search and download functionalities are using the official Maven repository.

r.api.mustache Maven / Gradle / Ivy

There is a newer version: 3.0.0-rc1
Show newest version
{{>partial_header}}
{{#operations}}
#' @title {{baseName}} operations
#' @description {{importPath}}
#'
#' @field path Stores url path of the request.
#' @field apiClient Handles the client-server communication.
#' @field userAgent Set the user agent of the request.
#'
#' @importFrom R6 R6Class
#'
#' @section Methods:
#' \describe{
{{#operation}}
#'
#' {{operationId}} {{summary}}
#'
{{/operation}}
#' }
#'
#' @export
{{classname}} <- R6::R6Class(
  '{{classname}}',
  public = list(
    userAgent = "{{#httpUserAgent}}{{{.}}}{{/httpUserAgent}}{{^httpUserAgent}}Swagger-Codegen/{{{packageVersion}}}/r{{/httpUserAgent}}",
    apiClient = NULL,
    initialize = function(apiClient){
      if (!missing(apiClient)) {
        self$apiClient <- apiClient
      }
      else {
        self$apiClient <- ApiClient$new()
      }
    },
    {{#operation}}
    {{operationId}} = function({{#allParams}}{{paramName}}, {{/allParams}}...){
      args <- list(...)
      queryParams <- list()
      headerParams <- character()

      {{#hasHeaderParams}}
      {{#headerParams}}
      if (!missing(`{{paramName}}`)) {
        headerParams['{{baseName}}'] <- `{{paramName}}`
      }

      {{/headerParams}}
      {{/hasHeaderParams}}
      {{#hasQueryParams}}
      {{#queryParams}}
      if (!missing(`{{paramName}}`)) {
        queryParams['{{baseName}}'] <- {{paramName}}
      }

      {{/queryParams}}
      {{/hasQueryParams}}
      {{#hasFormParams}}
      body <- list(
        {{#formParams}}
          {{^isFile}}
          "{{baseName}}" = {{paramName}}{{#hasMore}},{{/hasMore}}
          {{/isFile}}
          {{#isFile}}
          "{{baseName}}" = httr::upload_file({{paramName}}){{#hasMore}},{{/hasMore}}
          {{/isFile}}
        {{/formParams}}
      )

      {{/hasFormParams}}
      {{#hasBodyParam}}
      {{#bodyParams}}
      if (!missing(`{{paramName}}`)) {
        body <- `{{paramName}}`$toJSONString()
      } else {
        body <- NULL
      }

      {{/bodyParams}}
      {{/hasBodyParam}}
      urlPath <- "{{path}}"
      {{#hasPathParams}}
      {{#pathParams}}
      if (!missing(`{{paramName}}`)) {
        urlPath <- gsub(paste0("\\{", "{{baseName}}", "\\}"), `{{paramName}}`, urlPath)
      }

      {{/pathParams}}
      {{/hasPathParams}}
      resp <- self$apiClient$callApi(url = paste0(self$apiClient$basePath, urlPath),
                                 method = "{{httpMethod}}",
                                 queryParams = queryParams,
                                 headerParams = headerParams,
                                 body = body,
                                 ...)
      
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        {{#returnType}}
        returnObject <- {{returnType}}$new()
        result <- returnObject$fromJSON(httr::content(resp, "text", encoding = "UTF-8"))
        Response$new(returnObject, resp)
        {{/returnType}}
        {{^returnType}}
        # void response, no need to return anything
        {{/returnType}}
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        Response$new("API client error", resp)
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        Response$new("API server error", resp)
      }

    }{{#hasMore}},{{/hasMore}}
    {{/operation}}
  )
)
{{/operations}}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy