Class: Hive::Jsonrpc

Inherits:
Api
  • Object
show all
Defined in:
lib/hive/jsonrpc.rb

Overview

Jsonrpc allows you to inspect the available methods offered by a node. If a node runs a plugin you want, then all of the API methods it can exposes will automatically be available. This API is used internally to determine which APIs and methods are available on the node you specify.

In theory, if a new plugin is created and enabled by the node, it will be available by this library without needing an update to its code.

Constant Summary collapse

API_METHODS =
%i(get_signature get_methods)
UNLISTED_APIS =

APIs that the RPC server will (or likely will) respond to that is maintained outside the view of jsonrpc.

%i(bridge database_api)

Constants inherited from Api

Api::DEFAULT_RPC_CLIENT_CLASS

Instance Attribute Summary

Attributes inherited from Api

#chain, #methods, #rpc_client

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Api

api_class_name, api_name, api_name=, default_rpc_client_class, #inspect, jsonrpc, jsonrpc=, register

Constructor Details

#initialize(options = {}) ⇒ Jsonrpc

Returns a new instance of Jsonrpc.


25
26
27
28
29
# File 'lib/hive/jsonrpc.rb', line 25

def initialize(options = {})
  @api_name = self.class.api_name = :jsonrpc
  @methods = API_METHODS
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hive::Api

Class Method Details

.api_methodsObject


16
17
18
# File 'lib/hive/jsonrpc.rb', line 16

def self.api_methods
  @api_methods ||= {}
end

.reset_api_methodsObject

Might help diagnose a cluster that has asymmetric plugin definitions.


21
22
23
# File 'lib/hive/jsonrpc.rb', line 21

def self.reset_api_methods
  @api_methods = nil
end

Instance Method Details

#get_all_signatures(&block) ⇒ Object


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hive/jsonrpc.rb', line 62

def get_all_signatures(&block)
  request_object = []
  method_names = []
  method_map = {}
  signatures = {}
  offset = 0
  
  get_api_methods do |api, methods|
    request_object += methods.map do |method|
      method_name = "#{api}.#{method}"
      method_names << method_name
      current_rpc_id = @rpc_client.rpc_id
      offset += 1
      method_map[current_rpc_id] = [api, method]
      
      {
        jsonrpc: '2.0',
        id: current_rpc_id,
        method: 'jsonrpc.get_signature',
        params: {method: method_name}
      }
    end
  end
  
  chunks = if request_object.size > Hive::RPC::HttpClient::JSON_RPC_BATCH_SIZE_MAXIMUM
    request_object.each_slice(Hive::RPC::HttpClient::JSON_RPC_BATCH_SIZE_MAXIMUM)
  else
    request_object
  end
  
  for request_object in chunks do
    @rpc_client.rpc_batch_execute(api_name: self.class.api_name, request_object: request_object) do |result, error, id|
      api, method = method_map[id]
      api = api.to_sym
      method = method.to_sym
      
      signatures[api] ||= {}
      signatures[api][method] = result
    end
    
    if !!block
      signatures.each do |api, methods|
        yield api, methods
      end
    end
  end
  
  return signatures unless !!block
end

#get_api_methods(&block) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/hive/jsonrpc.rb', line 31

def get_api_methods(&block)
  api_methods = self.class.api_methods[@rpc_client.uri.to_s]
  
  if api_methods.nil?
    get_methods do |result, error, rpc_id|
      raise NotAppBaseError, "#{@rpc_client.uri} does not appear to run AppBase" unless defined? result.map
      
      methods = result.map do |method|
        method.split('.').map(&:to_sym)
      end
      
      api_methods = Hashie::Mash.new
      
      methods.each do |api, method|
        api_methods[api] ||= []
        api_methods[api] << method
      end
      
      self.class.api_methods[@rpc_client.uri.to_s] = api_methods
    end
  end
  
  if !!block
    api_methods.each do |api, methods|
      yield api, methods
    end
  else
    return api_methods
  end
end