Class: Restate::Client
- Inherits:
-
Object
- Object
- Restate::Client
- Defined in:
- lib/restate/client.rb,
sig/restate.rbs
Overview
HTTP client for invoking Restate services and managing the Restate runtime from outside the Restate runtime.
Instance Method Summary collapse
-
#cancel_invocation(invocation_id) ⇒ void
Cancel a running invocation.
-
#execute_query(sql) ⇒ Array<Hash>
Execute a SQL query against Restate's introspection API (DataFusion).
-
#initialize(ingress_url: 'http://localhost:8080', admin_url: 'http://localhost:9070', ingress_headers: {}, admin_headers: {}) ⇒ Client
constructor
A new instance of Client.
-
#kill_invocation(invocation_id) ⇒ void
Kill a running invocation (immediate termination, no cleanup).
-
#object(service, key) ⇒ ClientServiceProxy
Returns a proxy for calling a keyed virtual object.
- #parse_response(response) ⇒ Object
-
#post_admin(path, body) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#post_ingress(path, body) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#reject_awakeable(awakeable_id, message, code: 500) ⇒ void
Reject an awakeable from outside the Restate runtime.
-
#resolve_awakeable(awakeable_id, payload) ⇒ void
Resolve an awakeable from outside the Restate runtime.
- #resolve_name(service) ⇒ String
-
#service(service) ⇒ ClientServiceProxy
Returns a proxy for calling a stateless service.
-
#workflow(service, key) ⇒ ClientServiceProxy
Returns a proxy for calling a workflow.
Constructor Details
#initialize(ingress_url: 'http://localhost:8080', admin_url: 'http://localhost:9070', ingress_headers: {}, admin_headers: {}) ⇒ Client
Returns a new instance of Client.
34 35 36 37 38 39 40 |
# File 'lib/restate/client.rb', line 34 def initialize(ingress_url: 'http://localhost:8080', admin_url: 'http://localhost:9070', ingress_headers: {}, admin_headers: {}) @ingress_url = ingress_url.chomp('/') @admin_url = admin_url.chomp('/') @ingress_headers = ingress_headers @admin_headers = admin_headers end |
Instance Method Details
#cancel_invocation(invocation_id) ⇒ void
This method returns an undefined value.
Cancel a running invocation.
75 76 77 |
# File 'lib/restate/client.rb', line 75 def cancel_invocation(invocation_id) post_admin("/restate/invocations/#{invocation_id}/cancel", nil) end |
#execute_query(sql) ⇒ Array<Hash>
Execute a SQL query against Restate's introspection API (DataFusion). The admin API exposes system tables (sys_invocation, sys_journal, state, etc.) that can be queried with standard SQL.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/restate/client.rb', line 95 def execute_query(sql) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength uri = URI("#{@admin_url}/query") request = Net::HTTP::Post.new(uri) request['Content-Type'] = 'application/json' request['Accept'] = 'application/json' @admin_headers.each { |k, v| request[k] = v } request.body = JSON.generate({ query: sql }) response = Net::HTTP.start(uri.hostname, uri.port, # steep:ignore ArgumentTypeMismatch use_ssl: uri.scheme == 'https', open_timeout: 5, read_timeout: 30) { |http| http.request(request) } Kernel.raise "Restate query error: #{response.code} #{response.body}" unless response.is_a?(Net::HTTPSuccess) body = response.body body && !body.empty? ? (JSON.parse(body)['rows'] || []) : [] end |
#kill_invocation(invocation_id) ⇒ void
This method returns an undefined value.
Kill a running invocation (immediate termination, no cleanup).
80 81 82 |
# File 'lib/restate/client.rb', line 80 def kill_invocation(invocation_id) post_admin("/restate/invocations/#{invocation_id}/kill", nil) end |
#object(service, key) ⇒ ClientServiceProxy
Returns a proxy for calling a keyed virtual object.
50 51 52 |
# File 'lib/restate/client.rb', line 50 def object(service, key) ClientServiceProxy.new(@ingress_url, resolve_name(service), key, @ingress_headers) end |
#parse_response(response) ⇒ Object
147 148 149 150 |
# File 'lib/restate/client.rb', line 147 def parse_response(response) body = response.body body && !body.empty? ? JSON.parse(body) : nil end |
#post_admin(path, body) ⇒ Object
rubocop:disable Metrics/AbcSize
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/restate/client.rb', line 134 def post_admin(path, body) # rubocop:disable Metrics/AbcSize uri = URI("#{@admin_url}#{path}") request = Net::HTTP::Post.new(uri) request['Content-Type'] = 'application/json' @admin_headers.each { |k, v| request[k] = v } request.body = JSON.generate(body) if body response = Net::HTTP.start(uri.hostname, uri.port, # steep:ignore ArgumentTypeMismatch use_ssl: uri.scheme == 'https', read_timeout: 30) { |http| http.request(request) } Kernel.raise "Restate admin error: #{response.code} #{response.body}" unless response.is_a?(Net::HTTPSuccess) parse_response(response) end |
#post_ingress(path, body) ⇒ Object
rubocop:disable Metrics/AbcSize
121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/restate/client.rb', line 121 def post_ingress(path, body) # rubocop:disable Metrics/AbcSize uri = URI("#{@ingress_url}#{path}") request = Net::HTTP::Post.new(uri) request['Content-Type'] = 'application/json' @ingress_headers.each { |k, v| request[k] = v } request.body = JSON.generate(body) if body response = Net::HTTP.start(uri.hostname, uri.port, # steep:ignore ArgumentTypeMismatch use_ssl: uri.scheme == 'https', read_timeout: 30) { |http| http.request(request) } Kernel.raise "Restate ingress error: #{response.code} #{response.body}" unless response.is_a?(Net::HTTPSuccess) parse_response(response) end |
#reject_awakeable(awakeable_id, message, code: 500) ⇒ void
This method returns an undefined value.
Reject an awakeable from outside the Restate runtime.
67 68 69 70 |
# File 'lib/restate/client.rb', line 67 def reject_awakeable(awakeable_id, , code: 500) post_ingress("/restate/awakeables/#{awakeable_id}/reject", { 'message' => , 'code' => code }) end |
#resolve_awakeable(awakeable_id, payload) ⇒ void
This method returns an undefined value.
Resolve an awakeable from outside the Restate runtime.
62 63 64 |
# File 'lib/restate/client.rb', line 62 def resolve_awakeable(awakeable_id, payload) post_ingress("/restate/awakeables/#{awakeable_id}/resolve", payload) end |
#resolve_name(service) ⇒ String
113 114 115 116 117 118 119 |
# File 'lib/restate/client.rb', line 113 def resolve_name(service) if service.is_a?(Class) && service.respond_to?(:service_name) service.service_name # steep:ignore NoMethod else service.to_s end end |
#service(service) ⇒ ClientServiceProxy
Returns a proxy for calling a stateless service.
45 46 47 |
# File 'lib/restate/client.rb', line 45 def service(service) ClientServiceProxy.new(@ingress_url, resolve_name(service), nil, @ingress_headers) end |
#workflow(service, key) ⇒ ClientServiceProxy
Returns a proxy for calling a workflow.
55 56 57 |
# File 'lib/restate/client.rb', line 55 def workflow(service, key) ClientServiceProxy.new(@ingress_url, resolve_name(service), key, @ingress_headers) end |