Class: OpenC3::JsonApi

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

Instance Method Summary collapse

Constructor Details

#initialize(microservice_name:, prefix:, schema: 'http', hostname: nil, port:, timeout: 5.0, url: nil, scope: $openc3_scope) ⇒ JsonApi

Create a JsonApiObject connection to the API server



25
26
27
28
29
30
31
32
# File 'lib/openc3/io/json_api.rb', line 25

def initialize(microservice_name:, prefix:, schema: 'http', hostname: nil, port:, timeout: 5.0, url: nil, scope: $openc3_scope)
  url = _generate_url(microservice_name: microservice_name, prefix: prefix, schema: schema, hostname: hostname, port: port, scope: scope) unless url
  @json_api = JsonApiObject.new(
    url: url,
    timeout: timeout,
    authentication: _generate_auth()
  )
end

Instance Method Details

#_generate_authObject

generate the auth object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/openc3/io/json_api.rb', line 54

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

#_generate_url(microservice_name:, prefix:, schema: 'http', hostname: nil, port:, scope: $openc3_scope) ⇒ Object

pull openc3-cosmos-script-runner-api url from environment variables



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/openc3/io/json_api.rb', line 41

def _generate_url(microservice_name:, prefix:, schema: 'http', hostname: nil, port:, scope: $openc3_scope)
  prefix = '/' + prefix unless prefix[0] == '/'
  if ENV['KUBERNETES_SERVICE_HOST']
    hostname = "#{scope}__USER__#{microservice_name}" unless hostname
    hostname = hostname.downcase.gsub("__", "-").gsub("_", "-")
    return "#{schema}://#{hostname}-service:#{port.to_i}#{prefix}"
  else
    hostname = 'openc3-operator' unless hostname
    return "#{schema}://#{hostname}:#{port.to_i}#{prefix}"
  end
end

#_request(*method_params, **kw_params) ⇒ Object



66
67
68
69
70
# File 'lib/openc3/io/json_api.rb', line 66

def _request(*method_params, **kw_params)
  kw_params[:scope] = $openc3_scope unless kw_params[:scope]
  kw_params[:json] = true unless kw_params[:json]
  @json_api.request(*method_params, **kw_params)
end

#shutdownObject



34
35
36
# File 'lib/openc3/io/json_api.rb', line 34

def shutdown
  @json_api.shutdown
end