R/social_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 Social operations
#' @description namsor.Social
#'
#' @field path Stores url path of the request.
#' @field apiClient Handles the client-server communication.
#'
#' @importFrom R6 R6Class
#'
#' @section Methods:
#' \describe{
#'
#' PhonePrefix [USES 11 UNITS] Infer the likely country and phone prefix, given a personal name and formatted / unformatted phone number.
#'
#'
#' PhonePrefixBatch [USES 11 UNITS] Infer the likely country and phone prefix, of up to 1000 personal names, detecting automatically the local context given a name and formatted / unformatted phone number.
#'
#' }
#'
#' @importFrom jsonlite base64_enc
#' @export
SocialApi <- R6::R6Class(
  'SocialApi',
  public = list(
    apiClient = NULL,
    initialize = function(apiClient){
      if (!missing(apiClient)) {
        self$apiClient <- apiClient
      }
      else {
        self$apiClient <- ApiClient$new()
      }
    },
    PhonePrefix = function(first.name, last.name, phone.number, ...){
      args <- list(...)
      queryParams <- list()
      headerParams <- c()

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

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

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

      urlPath <- "/api2/json/phoneCode/{firstName}/{lastName}/{phoneNumber}"
      if (!missing(`first.name`)) {
        urlPath <- gsub(paste0("\\{", "firstName", "\\}"), `first.name`, urlPath)
      }

      if (!missing(`last.name`)) {
        urlPath <- gsub(paste0("\\{", "lastName", "\\}"), `last.name`, urlPath)
      }

      if (!missing(`phone.number`)) {
        urlPath <- gsub(paste0("\\{", "phoneNumber", "\\}"), `phone.number`, 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) {
        FirstLastNamePhoneCodedOut$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)
      }

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

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

      urlPath <- "/api2/json/phoneCodeBatch"
      # 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) {
        BatchFirstLastNamePhoneCodedOut$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.