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: ENV['OPENC3_API_URL'], timeout: 1.0, authentication: nil) ⇒ JsonApiObject

Returns a new instance of JsonApiObject.

Parameters:

  • url (String) (defaults to: ENV['OPENC3_API_URL'])

    The url of openc3-cosmos-cmd-tlm-api openc3-cosmos-cmd-tlm-api:2901

  • timeout (Float) (defaults to: 1.0)

    The time to wait before disconnecting 1.0

  • authentication (OpenC3Authentication) (defaults to: nil)

    The authentication object if nill initialize will generate



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

def initialize(url: ENV['OPENC3_API_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
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



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

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

#generate_authObject

generate the auth object



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

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:



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

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



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

def shutdown
  @shutdown = true
  disconnect()
end