Class: Sprites::Client
- Inherits:
-
Object
- Object
- Sprites::Client
- Defined in:
- lib/sprites/client.rb
Overview
HTTP client for the Sprites API.
Instance Attribute Summary collapse
-
#token ⇒ String
readonly
The API token.
Instance Method Summary collapse
-
#auth_headers ⇒ Array<Array<String>>
Authorization headers for WebSocket connections.
-
#checkpoints ⇒ Resources::Checkpoints
Access checkpoint operations.
- #delete(path) ⇒ Object
-
#exec ⇒ Resources::Exec
Access command execution operations.
- #get(path) ⇒ Object
-
#initialize(token: nil, base_url: nil) ⇒ Client
constructor
Create a new client.
-
#policies ⇒ Resources::Policies
Access network policy operations.
- #post(path, body) ⇒ Object
- #post_stream(path, body, &block) ⇒ Object
- #put(path, body) ⇒ Object
-
#sprites ⇒ Resources::Sprites
Access sprite operations.
-
#websocket_url ⇒ String
WebSocket URL derived from base URL.
Constructor Details
#initialize(token: nil, base_url: nil) ⇒ Client
Create a new client.
21 22 23 24 |
# File 'lib/sprites/client.rb', line 21 def initialize(token: nil, base_url: nil) @token = token || Sprites.configuration.token @base_url = base_url || Sprites.configuration.base_url end |
Instance Attribute Details
#token ⇒ String (readonly)
Returns the API token.
15 16 17 |
# File 'lib/sprites/client.rb', line 15 def token @token end |
Instance Method Details
#auth_headers ⇒ Array<Array<String>>
Returns authorization headers for WebSocket connections.
60 61 62 |
# File 'lib/sprites/client.rb', line 60 def auth_headers [["authorization", "Bearer #{@token}"]] end |
#checkpoints ⇒ Resources::Checkpoints
Access checkpoint operations.
36 37 38 |
# File 'lib/sprites/client.rb', line 36 def checkpoints Resources::Checkpoints.new(self) end |
#delete(path) ⇒ Object
70 |
# File 'lib/sprites/client.rb', line 70 def delete(path) = handle_response(connection.delete(path)) |
#exec ⇒ Resources::Exec
Access command execution operations.
50 51 52 |
# File 'lib/sprites/client.rb', line 50 def exec Resources::Exec.new(self) end |
#get(path) ⇒ Object
64 |
# File 'lib/sprites/client.rb', line 64 def get(path) = handle_response(connection.get(path)) |
#policies ⇒ Resources::Policies
Access network policy operations.
43 44 45 |
# File 'lib/sprites/client.rb', line 43 def policies Resources::Policies.new(self) end |
#post(path, body) ⇒ Object
66 |
# File 'lib/sprites/client.rb', line 66 def post(path, body) = handle_response(connection.post(path, body.to_json, "Content-Type" => "application/json")) |
#post_stream(path, body, &block) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/sprites/client.rb', line 72 def post_stream(path, body, &block) response = connection.post(path, body.to_json, "Content-Type" => "application/json") raise_error(response.body) unless response.success? events = parse_ndjson(response.body) if (error_event = events.find { |e| e[:type] == "error" }) raise Error, error_event[:error] end block_given? ? events.each(&block) : events end |
#put(path, body) ⇒ Object
68 |
# File 'lib/sprites/client.rb', line 68 def put(path, body) = handle_response(connection.put(path, body.to_json, "Content-Type" => "application/json")) |
#sprites ⇒ Resources::Sprites
Access sprite operations.
29 30 31 |
# File 'lib/sprites/client.rb', line 29 def sprites Resources::Sprites.new(self) end |
#websocket_url ⇒ String
Returns WebSocket URL derived from base URL.
55 56 57 |
# File 'lib/sprites/client.rb', line 55 def websocket_url @base_url.sub(/^http(s?)/) { "ws#{$1}" } end |