R/blocks_api.R

# GraphSense API
#
# GraphSense API provides programmatic access to various ledgers' addresses, entities, blocks, transactions and tags for automated and highly efficient forensics tasks.
#
# The version of the OpenAPI document: 1.0.1
# Contact: contact@ikna.io
# Generated by: https://openapi-generator.tech

#' @docType class
#' @title Blocks operations
#' @description graphsense.Blocks
#' @format An \code{R6Class} generator object
#' @field apiClient Handles the client-server communication.
#'
#' @section Methods:
#' \describe{
#' \strong{ GetBlock } \emph{ Get a block by its height }
#' 
#'
#' \itemize{
#' \item \emph{ @param } currency character
#' \item \emph{ @param } height integer
#' \item \emph{ @returnType } \link{Block} \cr
#'
#'
#' \item status code : 200 | OK
#'
#' \item return type : Block 
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' \strong{ ListBlockTxs } \emph{ Get block transactions }
#' 
#'
#' \itemize{
#' \item \emph{ @param } currency character
#' \item \emph{ @param } height integer
#' \item \emph{ @returnType } list( \link{tx} ) \cr
#'
#'
#' \item status code : 200 | OK
#'
#' \item return type : array[Tx] 
#' \item response headers :
#'
#' \tabular{ll}{
#' }
#' }
#'
#' }
#'
#'
#' @examples
#' \dontrun{
#' ####################  GetBlock  ####################
#'
#' library(graphsense)
#' var.currency <- 'btc' # character | The cryptocurrency code (e.g., btc)
#' var.height <- 1 # integer | The block height
#'
#' #Get a block by its height
#' api.instance <- BlocksApi$new()
#'
#' #Configure API key authorization: api_key
#' api.instance$apiClient$apiKeys['Authorization'] <- 'WRITE_YOUR_API_KEY_HERE';
#'
#' result <- api.instance$GetBlock(var.currency, var.height)
#'
#'
#' ####################  ListBlockTxs  ####################
#'
#' library(graphsense)
#' var.currency <- 'btc' # character | The cryptocurrency code (e.g., btc)
#' var.height <- 1 # integer | The block height
#'
#' #Get block transactions
#' api.instance <- BlocksApi$new()
#'
#' #Configure API key authorization: api_key
#' api.instance$apiClient$apiKeys['Authorization'] <- 'WRITE_YOUR_API_KEY_HERE';
#'
#' result <- api.instance$ListBlockTxs(var.currency, var.height)
#'
#'
#' }
#' @importFrom R6 R6Class
#' @importFrom base64enc base64encode
#' @export
BlocksApi <- R6::R6Class(
  'BlocksApi',
  public = list(
    apiClient = NULL,
    initialize = function(apiClient){
      if (!missing(apiClient)) {
        self$apiClient <- apiClient
      }
      else {
        self$apiClient <- ApiClient$new()
      }
    },
    GetBlock = function(currency, height, ...){
      apiResponse <- self$GetBlockWithHttpInfo(currency, height, ...)
      resp <- apiResponse$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        apiResponse$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        apiResponse
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        apiResponse
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        apiResponse
      }
    },

    GetBlockWithHttpInfo = function(currency, height, ...){
      args <- list(...)
      queryParams <- list()
      headerParams <- c()

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

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

      body <- NULL
      urlPath <- "/{currency}/blocks/{height}"
      if (!missing(`currency`)) {
        urlPath <- gsub(paste0("\\{", "currency", "\\}"), URLencode(as.character(`currency`), reserved = TRUE), urlPath)
      }

      if (!missing(`height`)) {
        urlPath <- gsub(paste0("\\{", "height", "\\}"), URLencode(as.character(`height`), reserved = TRUE), urlPath)
      }

      # API key authentication
      if ("Authorization" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["Authorization"]) > 0) {
        headerParams['Authorization'] <- paste(unlist(self$apiClient$apiKeys["Authorization"]), 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) {
        deserializedRespObj <- tryCatch(
          self$apiClient$deserialize(resp, "Block", loadNamespace("graphsense")),
          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)
      }
    },
    ListBlockTxs = function(currency, height, ...){
      apiResponse <- self$ListBlockTxsWithHttpInfo(currency, height, ...)
      resp <- apiResponse$response
      if (httr::status_code(resp) >= 200 && httr::status_code(resp) <= 299) {
        apiResponse$content
      } else if (httr::status_code(resp) >= 300 && httr::status_code(resp) <= 399) {
        apiResponse
      } else if (httr::status_code(resp) >= 400 && httr::status_code(resp) <= 499) {
        apiResponse
      } else if (httr::status_code(resp) >= 500 && httr::status_code(resp) <= 599) {
        apiResponse
      }
    },

    ListBlockTxsWithHttpInfo = function(currency, height, ...){
      args <- list(...)
      queryParams <- list()
      headerParams <- c()

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

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

      body <- NULL
      urlPath <- "/{currency}/blocks/{height}/txs"
      if (!missing(`currency`)) {
        urlPath <- gsub(paste0("\\{", "currency", "\\}"), URLencode(as.character(`currency`), reserved = TRUE), urlPath)
      }

      if (!missing(`height`)) {
        urlPath <- gsub(paste0("\\{", "height", "\\}"), URLencode(as.character(`height`), reserved = TRUE), urlPath)
      }

      # API key authentication
      if ("Authorization" %in% names(self$apiClient$apiKeys) && nchar(self$apiClient$apiKeys["Authorization"]) > 0) {
        headerParams['Authorization'] <- paste(unlist(self$apiClient$apiKeys["Authorization"]), 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) {
        deserializedRespObj <- tryCatch(
          self$apiClient$deserialize(resp, "array[Tx]", loadNamespace("graphsense")),
          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)
      }
    }
  )
)
graphsense/rgraphsense documentation built on Sept. 2, 2022, 1:45 p.m.