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.
-
#containers ⇒ Object
readonly
Returns the value of attribute containers.
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#terminal ⇒ Object
readonly
Returns the value of attribute terminal.
-
#token ⇒ Object
readonly
private
Get the API token.
Instance Method Summary collapse
-
#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.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/rexec/client.rb', line 21 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 @containers = ContainerService.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.
14 15 16 |
# File 'lib/rexec/client.rb', line 14 def base_url @base_url end |
#containers ⇒ Object (readonly)
Returns the value of attribute containers.
14 15 16 |
# File 'lib/rexec/client.rb', line 14 def containers @containers end |
#files ⇒ Object (readonly)
Returns the value of attribute files.
14 15 16 |
# File 'lib/rexec/client.rb', line 14 def files @files end |
#terminal ⇒ Object (readonly)
Returns the value of attribute terminal.
14 15 16 |
# File 'lib/rexec/client.rb', line 14 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.
78 79 80 |
# File 'lib/rexec/client.rb', line 78 def token @token end |
Instance Method Details
#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.
42 43 44 45 46 47 48 |
# File 'lib/rexec/client.rb', line 42 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.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rexec/client.rb', line 52 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.
70 71 72 73 74 |
# File 'lib/rexec/client.rb', line 70 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 |