R/chinese_api.R

# NamSor API v2
#
# NamSor API v2 : enpoints to process personal names (gender, cultural origin or ethnicity) in all alphabets or languages. Use GET methods for small tests, but prefer POST methods for higher throughput (batch processing of up to 1000 names at a time). Need something you can't find here? We have many more features coming soon. Let us know, we'll do our best to add it! 
#
# OpenAPI spec version: 2.0.2-beta
# Contact: contact@namsor.com
# Generated by: https://openapi-generator.tech

#' @title Chinese operations
#' @description namsor.Chinese
#'
#' @field path Stores url path of the request.
#' @field apiClient Handles the client-server communication.
#'
#' @importFrom R6 R6Class
#'
#' @section Methods:
#' \describe{
#'
#' ChineseNameCandidates Identify Chinese name candidates, based on the romanized name.
#'
#'
#' ChineseNameCandidatesBatch Identify Chinese name candidates, based on the romanized name (firstName = chineseGivenName; lastName=chineseSurname).
#'
#'
#' ChineseNameCandidatesGenderBatch Identify Chinese name candidates, based on the romanized name (firstName = chineseGivenName; lastName=chineseSurname).
#'
#'
#' ChineseNameGenderCandidates Identify Chinese name candidates, based on the romanized name - having a known gender ('male' or 'female')
#'
#' }
#'
#' @importFrom jsonlite base64_enc
#' @export
ChineseApi <- R6::R6Class(
  'ChineseApi',
  public = list(
    apiClient = NULL,
    initialize = function(apiClient){
      if (!missing(apiClient)) {
        self$apiClient <- apiClient
      }
      else {
        self$apiClient <- ApiClient$new()
      }
    },
    ChineseNameCandidates = function(chinese.surname.latin, chinese.given.name.latin, ...){
      args <- list(...)
      queryParams <- list()
      headerParams <- c()

      if (missing(`chinese.surname.latin`)) {
        stop("Missing required parameter `chinese.surname.latin`.")
      }

      if (missing(`chinese.given.name.latin`)) {
        stop("Missing required parameter `chinese.given.name.latin`.")
      }

      urlPath <- "/api2/json/chineseNameCandidates/{chineseSurnameLatin}/{chineseGivenNameLatin}"
      if (!missing(`chinese.surname.latin`)) {
        urlPath <- gsub(paste0("\\{", "chineseSurnameLatin", "\\}"), `chinese.surname.latin`, urlPath)
      }

      if (!missing(`chinese.given.name.latin`)) {
        urlPath <- gsub(paste0("\\{", "chineseGivenNameLatin", "\\}"), `chinese.given.name.latin`, urlPath)
      }

      # API key authentication
      if ("X-API-KEY" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["X-API-KEY"]) > 0) {
        headerParams['X-API-KEY'] <- paste(unlist(self$apiClient$apiKeys["X-API-KEY"]), collapse='')
      }

      resp <- self$apiClient$CallApi(url = paste0(self$apiClient$basePath, urlPath),
                                 method = "GET",
                                 queryParams = queryParams,
                                 headerParams = headerParams,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        RomanizedNameOut$new()$fromJSONString(httr::content(resp, "text", encoding = "UTF-8"))
      } 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)
      }

    },
    ChineseNameCandidatesBatch = function(batch.first.last.name.in=NULL, ...){
      args <- list(...)
      queryParams <- list()
      headerParams <- c()

      if (!missing(`batch.first.last.name.in`)) {
        body <- `batch.first.last.name.in`$toJSONString()
      } else {
        body <- NULL
      }

      urlPath <- "/api2/json/chineseNameCandidatesBatch"
      # API key authentication
      if ("X-API-KEY" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["X-API-KEY"]) > 0) {
        headerParams['X-API-KEY'] <- paste(unlist(self$apiClient$apiKeys["X-API-KEY"]), collapse='')
      }

      resp <- self$apiClient$CallApi(url = paste0(self$apiClient$basePath, urlPath),
                                 method = "POST",
                                 queryParams = queryParams,
                                 headerParams = headerParams,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        BatchNameMatchCandidatesOut$new()$fromJSONString(httr::content(resp, "text", encoding = "UTF-8"))
      } 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)
      }

    },
    ChineseNameCandidatesGenderBatch = function(batch.first.last.name.in=NULL, ...){
      args <- list(...)
      queryParams <- list()
      headerParams <- c()

      if (!missing(`batch.first.last.name.in`)) {
        body <- `batch.first.last.name.in`$toJSONString()
      } else {
        body <- NULL
      }

      urlPath <- "/api2/json/chineseNameCandidatesGenderBatch"
      # API key authentication
      if ("X-API-KEY" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["X-API-KEY"]) > 0) {
        headerParams['X-API-KEY'] <- paste(unlist(self$apiClient$apiKeys["X-API-KEY"]), collapse='')
      }

      resp <- self$apiClient$CallApi(url = paste0(self$apiClient$basePath, urlPath),
                                 method = "POST",
                                 queryParams = queryParams,
                                 headerParams = headerParams,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        BatchNameMatchCandidatesOut$new()$fromJSONString(httr::content(resp, "text", encoding = "UTF-8"))
      } 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)
      }

    },
    ChineseNameGenderCandidates = function(chinese.surname.latin, chinese.given.name.latin, known.gender, ...){
      args <- list(...)
      queryParams <- list()
      headerParams <- c()

      if (missing(`chinese.surname.latin`)) {
        stop("Missing required parameter `chinese.surname.latin`.")
      }

      if (missing(`chinese.given.name.latin`)) {
        stop("Missing required parameter `chinese.given.name.latin`.")
      }

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

      urlPath <- "/api2/json/chineseNameGenderCandidates/{chineseSurnameLatin}/{chineseGivenNameLatin}/{knownGender}"
      if (!missing(`chinese.surname.latin`)) {
        urlPath <- gsub(paste0("\\{", "chineseSurnameLatin", "\\}"), `chinese.surname.latin`, urlPath)
      }

      if (!missing(`chinese.given.name.latin`)) {
        urlPath <- gsub(paste0("\\{", "chineseGivenNameLatin", "\\}"), `chinese.given.name.latin`, urlPath)
      }

      if (!missing(`known.gender`)) {
        urlPath <- gsub(paste0("\\{", "knownGender", "\\}"), `known.gender`, urlPath)
      }

      # API key authentication
      if ("X-API-KEY" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["X-API-KEY"]) > 0) {
        headerParams['X-API-KEY'] <- paste(unlist(self$apiClient$apiKeys["X-API-KEY"]), collapse='')
      }

      resp <- self$apiClient$CallApi(url = paste0(self$apiClient$basePath, urlPath),
                                 method = "GET",
                                 queryParams = queryParams,
                                 headerParams = headerParams,
                                 body = body,
                                 ...)

      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        RomanizedNameOut$new()$fromJSONString(httr::content(resp, "text", encoding = "UTF-8"))
      } 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/namsor-r-client documentation built on May 26, 2019, 7:01 p.m.