Module: Legion::API::Routes::Extensions

Defined in:
lib/legion/api/extensions.rb

Class Method Summary collapse

Class Method Details

.extension_entriesObject



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/legion/api/extensions.rb', line 145

def extension_entries
  handles = if Legion::Extensions.respond_to?(:extension_handles)
              Legion::Extensions.extension_handles
            else
              []
            end
  return handles.map { |handle| serialize_handle(handle) } unless handles.empty?

  Legion::Extensions::Catalog.all.filter_map do |name, entry|
    serialize_catalog_entry(name, entry)
  end
end

.extension_entry(name) ⇒ Object



158
159
160
161
162
163
# File 'lib/legion/api/extensions.rb', line 158

def extension_entry(name)
  handle = Legion::Extensions.extension_handle(name) if Legion::Extensions.respond_to?(:extension_handle)
  return serialize_handle(handle) if handle

  serialize_catalog_entry(name, Legion::Extensions::Catalog.entry(name))
end

.register_available_route(app) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/legion/api/extensions.rb', line 15

def self.register_available_route(app)
  app.get '/api/extension_catalog/available' do
    entries = Legion::Extensions::Catalog::Available.all
    entries = entries.select { |e| e[:category] == params[:category] } if params[:category]
    json_response(entries)
  end
end

.register_extension_routes(app) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/api/extensions.rb', line 23

def self.register_extension_routes(app)
  app.get '/api/extension_catalog' do
    entries = Routes::Extensions.extension_entries
    entries = entries.select { |e| e[:state] == params[:state] } if params[:state]
    json_response(entries)
  end

  app.get '/api/extension_catalog/:name' do
    name = params[:name]
    entry = Routes::Extensions.extension_entry(name)
    halt_not_found("extension '#{name}' not found") unless entry

    ext_mod = find_extension_module(name)

    runners = ext_mod ? runner_summaries(ext_mod) : []

    json_response(entry.merge(runners: runners).compact)
  end
end

.register_function_routes(app) ⇒ Object



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
# File 'lib/legion/api/extensions.rb', line 75

def self.register_function_routes(app)
  app.get '/api/extension_catalog/:name/runners/:runner_name/functions' do
    name = params[:name]
    halt_not_found("extension '#{name}' not found") unless Legion::Extensions::Catalog.entry(name)

    ext_mod = find_extension_module(name)
    halt_not_found("extension '#{name}' not loaded") unless ext_mod

    info = find_runner_info(ext_mod, params[:runner_name])
    halt_not_found("runner '#{params[:runner_name]}' not found") unless info

    functions = info[:runner_module].instance_methods(false).map do |m|
      args = info.dig(:class_methods, m, :args)
      { name: m.to_s, args: args }
    end
    json_response(functions)
  end

  app.get '/api/extension_catalog/:name/runners/:runner_name/functions/:function_name' do
    name = params[:name]
    halt_not_found("extension '#{name}' not found") unless Legion::Extensions::Catalog.entry(name)

    ext_mod = find_extension_module(name)
    halt_not_found("extension '#{name}' not loaded") unless ext_mod

    info = find_runner_info(ext_mod, params[:runner_name])
    halt_not_found("runner '#{params[:runner_name]}' not found") unless info

    func_sym = params[:function_name].to_sym
    halt_not_found("function '#{params[:function_name]}' not found") unless info[:runner_module].method_defined?(func_sym, false)

    args = info.dig(:class_methods, func_sym, :args)
    json_response({ name: params[:function_name], runner: params[:runner_name], args: args })
  end
end

.register_invoke_route(app) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/legion/api/extensions.rb', line 111

def self.register_invoke_route(app)
  app.post '/api/extension_catalog/:name/runners/:runner_name/functions/:function_name/invoke' do
    name = params[:name]
    halt_not_found("extension '#{name}' not found") unless Legion::Extensions::Catalog.entry(name)

    ext_mod = find_extension_module(name)
    halt_not_found("extension '#{name}' not loaded") unless ext_mod

    info = find_runner_info(ext_mod, params[:runner_name])
    halt_not_found("runner '#{params[:runner_name]}' not found") unless info

    func_sym = params[:function_name].to_sym
    halt_not_found("function '#{params[:function_name]}' not found") unless info[:runner_module].method_defined?(func_sym, false)

    body = parse_request_body

    result = Legion::Ingress.run(
      payload:       body,
      runner_class:  info[:runner_class],
      function:      func_sym,
      source:        'api',
      check_subtask: body.fetch(:check_subtask, true),
      generate_task: body.fetch(:generate_task, true)
    )
    json_response(result, status_code: 201)
  rescue NameError => e
    json_error('invalid_runner', e.message, status_code: 422)
  rescue StandardError => e
    Legion::Logging.error "API POST /api/extension_catalog invoke: #{e.class} - #{e.message}" if defined?(Legion::Logging)
    json_error('execution_error', e.message, status_code: 500)
  end
end

.register_runner_routes(app) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/legion/api/extensions.rb', line 43

def self.register_runner_routes(app)
  app.get '/api/extension_catalog/:name/runners' do
    name = params[:name]
    halt_not_found("extension '#{name}' not found") unless Legion::Extensions::Catalog.entry(name)

    ext_mod = find_extension_module(name)
    halt_not_found("extension '#{name}' not loaded") unless ext_mod

    json_response(runner_summaries(ext_mod))
  end

  app.get '/api/extension_catalog/:name/runners/:runner_name' do
    name = params[:name]
    halt_not_found("extension '#{name}' not found") unless Legion::Extensions::Catalog.entry(name)

    ext_mod = find_extension_module(name)
    halt_not_found("extension '#{name}' not loaded") unless ext_mod

    info = find_runner_info(ext_mod, params[:runner_name])
    halt_not_found("runner '#{params[:runner_name]}' not found") unless info

    runner_mod = info[:runner_module]
    functions = runner_mod.instance_methods(false).map(&:to_s)

    json_response({
                    name:         info[:runner_name],
                    runner_class: info[:runner_class],
                    functions:    functions
                  })
  end
end

.registered(app) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/legion/api/extensions.rb', line 7

def self.registered(app)
  register_available_route(app)
  register_extension_routes(app)
  register_runner_routes(app)
  register_function_routes(app)
  register_invoke_route(app)
end

.serialize_catalog_entry(name, entry) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/legion/api/extensions.rb', line 183

def serialize_catalog_entry(name, entry)
  return nil unless entry

  { name: name, state: entry[:state].to_s,
    registered_at: entry[:registered_at]&.iso8601,
    started_at: entry[:started_at]&.iso8601 }
end

.serialize_handle(handle) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/legion/api/extensions.rb', line 165

def serialize_handle(handle)
  {
    name:                     handle.lex_name,
    state:                    handle.state.to_s,
    active_version:           handle.active_version&.to_s,
    latest_installed_version: handle.latest_installed_version&.to_s,
    reload_state:             handle.reload_state.to_s,
    pending_reload:           handle.pending_reload?,
    hot_reloadable:           handle.hot_reloadable,
    loaded_at:                handle.loaded_at&.iso8601,
    last_error:               handle.last_error,
    routes:                   handle.routes,
    tools:                    handle.tools,
    absorbers:                handle.absorbers,
    owned_runners:            handle.runners
  }.compact
end