Class: Rexec::Client
- Inherits:
-
Object
- Object
- Rexec::Client
- Defined in:
- lib/rexec/client.rb
Overview
Main client for interacting with Rexec API.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#sandboxes ⇒ Object
readonly
Returns the value of attribute sandboxes.
-
#terminal ⇒ Object
readonly
Returns the value of attribute terminal.
-
#token ⇒ Object
readonly
private
Get the API token.
Instance Method Summary collapse
-
#containers ⇒ SandboxService
Deprecated alias for #sandboxes.
-
#initialize(base_url, token, timeout: 30) ⇒ Client
constructor
Initialize a new Rexec client.
-
#request(method, path, body: nil, params: nil) ⇒ Object
private
Make an API request.
-
#request_bytes(method, path) ⇒ Object
private
Make a raw request and return bytes.
-
#ws_url(path) ⇒ Object
private
Get WebSocket URL.
Constructor Details
#initialize(base_url, token, timeout: 30) ⇒ Client
Initialize a new Rexec client.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rexec/client.rb', line 28 def initialize(base_url, token, timeout: 30) @base_url = base_url.chomp("/") @token = token @timeout = timeout @http = Faraday.new(url: @base_url) do |f| f.request :json f.response :json, content_type: /\bjson$/ f.adapter Faraday.default_adapter f..timeout = timeout f.headers["Authorization"] = "Bearer #{token}" f.headers["Accept"] = "application/json" end @sandboxes = SandboxService.new(self) @files = FileService.new(self) @terminal = TerminalService.new(self) end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
15 16 17 |
# File 'lib/rexec/client.rb', line 15 def base_url @base_url end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
15 16 17 |
# File 'lib/rexec/client.rb', line 15 def files @files end |
#sandboxes ⇒ Object (readonly)
Returns the value of attribute sandboxes.
15 16 17 |
# File 'lib/rexec/client.rb', line 15 def sandboxes @sandboxes end |
#terminal ⇒ Object (readonly)
Returns the value of attribute terminal.
15 16 17 |
# File 'lib/rexec/client.rb', line 15 def terminal @terminal end |
#token ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get the API token.
85 86 87 |
# File 'lib/rexec/client.rb', line 85 def token @token end |
Instance Method Details
#containers ⇒ SandboxService
Deprecated alias for #sandboxes.
19 20 21 |
# File 'lib/rexec/client.rb', line 19 def containers sandboxes end |
#request(method, path, body: nil, params: nil) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Make an API request.
49 50 51 52 53 54 55 |
# File 'lib/rexec/client.rb', line 49 def request(method, path, body: nil, params: nil) response = @http.run_request(method, path, body, nil) do |req| req.params = params if params end handle_response(response) end |
#request_bytes(method, path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Make a raw request and return bytes.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rexec/client.rb', line 59 def request_bytes(method, path) raw_http = Faraday.new(url: @base_url) do |f| f.adapter Faraday.default_adapter f..timeout = @timeout f.headers["Authorization"] = "Bearer #{@token}" end response = raw_http.run_request(method, path, nil, nil) if response.status >= 400 raise APIError.new(response.status, "Request failed") end response.body end |
#ws_url(path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get WebSocket URL.
77 78 79 80 81 |
# File 'lib/rexec/client.rb', line 77 def ws_url(path) uri = URI.parse(@base_url) ws_scheme = uri.scheme == "https" ? "wss" : "ws" "#{ws_scheme}://#{uri.host}:#{uri.port || (uri.scheme == 'https' ? 443 : 80)}#{path}" end |