Class: Rexec::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rexec/client.rb

Overview

Main client for interacting with Rexec API.

Examples:

client = Rexec::Client.new("https://your-instance.com", "your-token")
containers = client.containers.list

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url, token, timeout: 30) ⇒ Client

Initialize a new Rexec client.

Parameters:

  • base_url (String)

    Base URL of your Rexec instance

  • token (String)

    API token for authentication

  • timeout (Integer) (defaults to: 30)

    Request timeout in seconds (default: 30)



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.options.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_urlObject (readonly)

Returns the value of attribute base_url.



14
15
16
# File 'lib/rexec/client.rb', line 14

def base_url
  @base_url
end

#containersObject (readonly)

Returns the value of attribute containers.



14
15
16
# File 'lib/rexec/client.rb', line 14

def containers
  @containers
end

#filesObject (readonly)

Returns the value of attribute files.



14
15
16
# File 'lib/rexec/client.rb', line 14

def files
  @files
end

#terminalObject (readonly)

Returns the value of attribute terminal.



14
15
16
# File 'lib/rexec/client.rb', line 14

def terminal
  @terminal
end

#tokenObject (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.options.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