Class: OpenC3::JsonApiObject

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/io/json_api_object.rb

Overview

Used to forward all method calls to the remote server object. Before using this class ensure the remote service has been started in the server class:

json = JsonDrb.new
json.start_service('127.0.0.1', 7777, self)

Now the JsonApiObject can be used to call server methods directly:

server = JsonApiObject('http://openc3-cosmos-cmd-tlm-api:2901', 1.0)
server.cmd(*args)

Direct Known Subclasses

JsonDRbObject

Constant Summary collapse

USER_AGENT =
'OpenC3 / v5 (ruby/openc3/lib/io/json_api_object)'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url:, timeout: 1.0, authentication: nil) ⇒ JsonApiObject

Returns a new instance of JsonApiObject.

Parameters:



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/openc3/io/json_api_object.rb', line 60

def initialize(url:, timeout: 1.0, authentication: nil)
  @http = nil
  @mutex = Mutex.new
  @request_data = ""
  @response_data = ""
  @url = url
  @log = [nil, nil, nil]
  @authentication = authentication.nil? ? generate_auth() : authentication
  @timeout = timeout
  @shutdown = false
  # JsonDRb.debug = true # Enable for debugging
end

Instance Attribute Details

#request_dataObject (readonly)

Returns the value of attribute request_data.



52
53
54
# File 'lib/openc3/io/json_api_object.rb', line 52

def request_data
  @request_data
end

#response_dataObject (readonly)

Returns the value of attribute response_data.



53
54
55
# File 'lib/openc3/io/json_api_object.rb', line 53

def response_data
  @response_data
end

Instance Method Details

#disconnectObject

Disconnects from http server



104
105
106
107
# File 'lib/openc3/io/json_api_object.rb', line 104

def disconnect
  @http.close if @http
  @http = nil
end

#generate_authObject

generate the auth object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/openc3/io/json_api_object.rb', line 74

def generate_auth
  if ENV['OPENC3_API_TOKEN'].nil? and ENV['OPENC3_API_USER'].nil?
    if ENV['OPENC3_API_PASSWORD']
      return OpenC3Authentication.new()
    else
      return nil
    end
  else
    return OpenC3KeycloakAuthentication.new(ENV['OPENC3_KEYCLOAK_URL'])
  end
end

#request(*method_params, **keyword_params) ⇒ Object

Forwards all method calls to the remote service.

Parameters:

  • method_params (Array)

    Array of parameters to pass to the method

  • keyword_params (Hash<Symbol, Variable>)

    Hash of keyword parameters

Returns:

  • The result of the method call.

Raises:



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/openc3/io/json_api_object.rb', line 91

def request(*method_params, **keyword_params)
  raise JsonApiError, "Shutdown" if @shutdown
  method = method_params[0]
  endpoint = method_params[1]
  @mutex.synchronize do
    kwargs = _generate_kwargs(keyword_params)
    @log = [nil, nil, nil]
    connect() if !@http
    return _send_request(method: method, endpoint: endpoint, kwargs: kwargs)
  end
end

#shutdownObject

Permanently disconnects from the http server



110
111
112
113
# File 'lib/openc3/io/json_api_object.rb', line 110

def shutdown
  @shutdown = true
  disconnect()
end