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://rexec.sh", "your-token")
sandboxes = client.sandboxes.list
# Legacy: client.containers is the same service

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)



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

Returns the value of attribute base_url.



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

def base_url
  @base_url
end

#filesObject (readonly)

Returns the value of attribute files.



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

def files
  @files
end

#sandboxesObject (readonly)

Returns the value of attribute sandboxes.



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

def sandboxes
  @sandboxes
end

#terminalObject (readonly)

Returns the value of attribute terminal.



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

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.



85
86
87
# File 'lib/rexec/client.rb', line 85

def token
  @token
end

Instance Method Details

#containersSandboxService

Deprecated alias for #sandboxes.

Returns:



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.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.



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