octaspace 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +93 -30
- data/LICENSE +21 -0
- data/README.md +104 -289
- data/lib/octaspace/client.rb +40 -23
- data/lib/octaspace/configuration.rb +73 -14
- data/lib/octaspace/errors.rb +70 -39
- data/lib/octaspace/json_null.rb +56 -0
- data/lib/octaspace/middleware/url_rotator.rb +3 -3
- data/lib/octaspace/model.rb +115 -0
- data/lib/octaspace/money.rb +69 -0
- data/lib/octaspace/railtie.rb +1 -2
- data/lib/octaspace/resources/accounts.rb +8 -20
- data/lib/octaspace/resources/base.rb +49 -2
- data/lib/octaspace/resources/compute.rb +123 -0
- data/lib/octaspace/resources/hosting.rb +107 -0
- data/lib/octaspace/resources/network.rb +24 -5
- data/lib/octaspace/resources/render.rb +46 -0
- data/lib/octaspace/resources/sessions.rb +125 -10
- data/lib/octaspace/resources/stats.rb +57 -0
- data/lib/octaspace/resources/vpn.rb +48 -0
- data/lib/octaspace/response.rb +62 -12
- data/lib/octaspace/transport/base.rb +30 -30
- data/lib/octaspace/transport/faraday_transport.rb +157 -91
- data/lib/octaspace/transport/persistent_transport.rb +23 -49
- data/lib/octaspace/types/account.rb +17 -4
- data/lib/octaspace/types/hosting.rb +116 -0
- data/lib/octaspace/types/machines.rb +158 -0
- data/lib/octaspace/types/network.rb +87 -0
- data/lib/octaspace/types/session.rb +173 -4
- data/lib/octaspace/types/stats.rb +143 -0
- data/lib/octaspace/version.rb +1 -1
- data/lib/octaspace.rb +21 -19
- metadata +30 -35
- data/MIT-LICENSE +0 -22
- data/lib/octaspace/payload_helpers.rb +0 -53
- data/lib/octaspace/resources/apps.rb +0 -19
- data/lib/octaspace/resources/idle_jobs.rb +0 -32
- data/lib/octaspace/resources/nodes.rb +0 -63
- data/lib/octaspace/resources/services/machine_rental.rb +0 -78
- data/lib/octaspace/resources/services/render.rb +0 -30
- data/lib/octaspace/resources/services/session_proxy.rb +0 -54
- data/lib/octaspace/resources/services/vpn.rb +0 -30
- data/lib/octaspace/resources/services.rb +0 -39
- data/lib/octaspace/types/balance.rb +0 -11
- data/lib/octaspace/types/node.rb +0 -12
data/lib/octaspace/client.rb
CHANGED
|
@@ -8,7 +8,7 @@ module OctaSpace
|
|
|
8
8
|
#
|
|
9
9
|
# @example Standard mode (default)
|
|
10
10
|
# client = OctaSpace::Client.new(api_key: ENV["OCTA_API_KEY"])
|
|
11
|
-
# client.
|
|
11
|
+
# client.compute.list
|
|
12
12
|
# client.accounts.balance
|
|
13
13
|
#
|
|
14
14
|
# @example Keep-alive mode (persistent connections + pool)
|
|
@@ -18,14 +18,14 @@ module OctaSpace
|
|
|
18
18
|
# pool_size: ENV.fetch("RAILS_MAX_THREADS", 5).to_i
|
|
19
19
|
# )
|
|
20
20
|
#
|
|
21
|
-
# @example
|
|
21
|
+
# @example Several API hosts with failover (the second one is your own, e.g. a proxy)
|
|
22
22
|
# client = OctaSpace::Client.new(
|
|
23
23
|
# api_key: ENV["OCTA_API_KEY"],
|
|
24
|
-
# base_urls: [
|
|
24
|
+
# base_urls: [OctaSpace::DEFAULT_BASE_URL, "https://octaspace-proxy.your-company.example"]
|
|
25
25
|
# )
|
|
26
26
|
#
|
|
27
|
-
# @example Without API key (
|
|
28
|
-
# client = OctaSpace::Client.new
|
|
27
|
+
# @example Without an API key (network and GPU statistics, listings, apps)
|
|
28
|
+
# client = OctaSpace::Client.new(api_key: "")
|
|
29
29
|
# client.network.info
|
|
30
30
|
#
|
|
31
31
|
# @example With hooks
|
|
@@ -35,22 +35,40 @@ module OctaSpace
|
|
|
35
35
|
# on_response: ->(resp) { puts "← #{resp.status}" }
|
|
36
36
|
# )
|
|
37
37
|
class Client
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
# @return [Resources::Accounts] balance
|
|
39
|
+
attr_reader :accounts
|
|
40
|
+
# @return [Resources::Compute] machines for rent: list, apps, start, start_batch, restart
|
|
41
|
+
attr_reader :compute
|
|
42
|
+
# @return [Resources::Hosting] nodes of the account: state, prices, maintenance
|
|
43
|
+
attr_reader :hosting
|
|
44
|
+
# @return [Resources::Network] platform statistics; no API key needed
|
|
45
|
+
attr_reader :network
|
|
46
|
+
# @return [Resources::Render] render nodes: list, start, start_batch
|
|
47
|
+
attr_reader :render
|
|
48
|
+
# @return [Resources::Sessions] running sessions: list, get, logs, stop, wait_ready, wait_stopped
|
|
49
|
+
attr_reader :sessions
|
|
50
|
+
# @return [Resources::Stats] GPU market and mining statistics; no API key needed
|
|
51
|
+
attr_reader :stats
|
|
52
|
+
# @return [Resources::Vpn] VPN nodes: list, start
|
|
53
|
+
attr_reader :vpn
|
|
40
54
|
|
|
41
|
-
# @param api_key [String, nil] API key
|
|
55
|
+
# @param api_key [String, nil] API key of your OctaSpace account (+octa_ak_…+);
|
|
56
|
+
# defaults to the global configuration, which reads +OCTA_API_KEY+. Pass ""
|
|
57
|
+
# for a client without a key. See the README for the calls that work
|
|
58
|
+
# without one.
|
|
42
59
|
# @param opts [Hash] Per-instance configuration overrides.
|
|
43
60
|
# Any attribute from OctaSpace::Configuration can be passed here.
|
|
44
61
|
def initialize(api_key: nil, transport: nil, **opts)
|
|
45
62
|
@config = build_config(api_key, opts)
|
|
46
63
|
@transport = transport || build_transport
|
|
47
64
|
@accounts = Resources::Accounts.new(@transport)
|
|
48
|
-
@
|
|
49
|
-
@
|
|
50
|
-
@apps = Resources::Apps.new(@transport)
|
|
65
|
+
@compute = Resources::Compute.new(@transport)
|
|
66
|
+
@hosting = Resources::Hosting.new(@transport)
|
|
51
67
|
@network = Resources::Network.new(@transport)
|
|
52
|
-
@
|
|
53
|
-
@
|
|
68
|
+
@render = Resources::Render.new(@transport)
|
|
69
|
+
@sessions = Resources::Sessions.new(@transport)
|
|
70
|
+
@stats = Resources::Stats.new(@transport)
|
|
71
|
+
@vpn = Resources::Vpn.new(@transport)
|
|
54
72
|
end
|
|
55
73
|
|
|
56
74
|
# Shut down persistent connections (only relevant in keep_alive mode)
|
|
@@ -61,15 +79,14 @@ module OctaSpace
|
|
|
61
79
|
# Transport diagnostics (pool stats when in keep_alive mode)
|
|
62
80
|
# @return [Hash]
|
|
63
81
|
def transport_stats
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
end
|
|
82
|
+
@transport.respond_to?(:transport_stats) ? @transport.transport_stats : {mode: :custom}
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Rate limit state (RateLimit-* headers) of the most recent response that
|
|
86
|
+
# carried them, or nil before the first such response.
|
|
87
|
+
# @return [OctaSpace::RateLimit, nil]
|
|
88
|
+
def rate_limit
|
|
89
|
+
@transport.respond_to?(:rate_limit) ? @transport.rate_limit : nil
|
|
73
90
|
end
|
|
74
91
|
|
|
75
92
|
private
|
|
@@ -77,7 +94,7 @@ module OctaSpace
|
|
|
77
94
|
# Merge global config with per-instance overrides
|
|
78
95
|
def build_config(api_key, overrides)
|
|
79
96
|
cfg = OctaSpace.configuration.dup
|
|
80
|
-
cfg.api_key = api_key
|
|
97
|
+
cfg.api_key = api_key unless api_key.nil?
|
|
81
98
|
overrides.each do |key, value|
|
|
82
99
|
cfg.public_send(:"#{key}=", value) if cfg.respond_to?(:"#{key}=")
|
|
83
100
|
end
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module OctaSpace
|
|
4
|
+
# Default API base URL
|
|
5
|
+
DEFAULT_BASE_URL = "https://api.octa.space"
|
|
6
|
+
|
|
7
|
+
# Prefix of account API keys (created in the OctaSpace dashboard under
|
|
8
|
+
# Account → Credentials → API Keys)
|
|
9
|
+
API_KEY_PREFIX = "octa_ak_"
|
|
10
|
+
|
|
4
11
|
# Global and per-client configuration for the OctaSpace SDK
|
|
5
12
|
#
|
|
6
13
|
# @example Global configuration (Rails initializer)
|
|
@@ -13,15 +20,25 @@ module OctaSpace
|
|
|
13
20
|
class Configuration
|
|
14
21
|
# --- Authentication ---
|
|
15
22
|
|
|
16
|
-
#
|
|
17
|
-
|
|
23
|
+
# Account API key (+octa_ak_…+), sent as +Authorization: Bearer <key>+.
|
|
24
|
+
# Defaults to the +OCTA_API_KEY+ environment variable. Network and GPU
|
|
25
|
+
# statistics, the machine listings and the apps work without it. A value
|
|
26
|
+
# that already starts with "Bearer " is accepted.
|
|
27
|
+
# @return [String, nil]
|
|
28
|
+
attr_reader :api_key
|
|
18
29
|
|
|
19
30
|
# --- Connection ---
|
|
20
31
|
|
|
21
|
-
#
|
|
32
|
+
# Base URL of the API (one host). Defaults to the +OCTA_API_URL+
|
|
33
|
+
# environment variable (several hosts separated by commas set
|
|
34
|
+
# {#base_urls}), otherwise {DEFAULT_BASE_URL}.
|
|
35
|
+
# @return [String]
|
|
22
36
|
attr_accessor :base_url
|
|
23
37
|
|
|
24
|
-
#
|
|
38
|
+
# Ordered list of API hosts. GET requests fail over to the next URL
|
|
39
|
+
# on network errors; requests that change state are sent to one URL only.
|
|
40
|
+
# Takes precedence over {#base_url}.
|
|
41
|
+
# @return [Array<String>, nil]
|
|
25
42
|
attr_accessor :base_urls
|
|
26
43
|
|
|
27
44
|
# @return [Integer] Seconds to wait for connection to open
|
|
@@ -49,6 +66,9 @@ module OctaSpace
|
|
|
49
66
|
attr_accessor :idle_timeout
|
|
50
67
|
|
|
51
68
|
# --- Retry ---
|
|
69
|
+
# Only GET requests and the idempotent calls (session stop, node
|
|
70
|
+
# maintenance) are retried, on network errors and on 429/502/503/504.
|
|
71
|
+
# Starting or restarting a session and changing prices are never retried.
|
|
52
72
|
|
|
53
73
|
# @return [Integer] Maximum number of retries on transient failures
|
|
54
74
|
attr_accessor :max_retries
|
|
@@ -59,9 +79,19 @@ module OctaSpace
|
|
|
59
79
|
# @return [Float] Exponential backoff multiplier
|
|
60
80
|
attr_accessor :backoff_factor
|
|
61
81
|
|
|
82
|
+
# @return [Float] Longest backoff delay in seconds (without Retry-After)
|
|
83
|
+
attr_accessor :max_retry_interval
|
|
84
|
+
|
|
85
|
+
# A Retry-After longer than this is raised to the caller instead of waited
|
|
86
|
+
# for.
|
|
87
|
+
# @return [Integer] seconds
|
|
88
|
+
attr_accessor :max_retry_after
|
|
89
|
+
|
|
62
90
|
# --- Hooks ---
|
|
63
91
|
|
|
64
|
-
# @return [#call, nil] Callable invoked before each request; receives
|
|
92
|
+
# @return [#call, nil] Callable invoked before each request attempt; receives
|
|
93
|
+
# a Hash with :method, :url, :path, :params and :attempt (never the headers,
|
|
94
|
+
# so the API key does not leak into logs)
|
|
65
95
|
attr_accessor :on_request
|
|
66
96
|
|
|
67
97
|
# @return [#call, nil] Callable invoked after each response; receives OctaSpace::Response
|
|
@@ -69,7 +99,10 @@ module OctaSpace
|
|
|
69
99
|
|
|
70
100
|
# --- Logging ---
|
|
71
101
|
|
|
72
|
-
#
|
|
102
|
+
# Logger for one line per request (method, URL, status, duration).
|
|
103
|
+
# Headers and bodies are never logged: they carry the API key and,
|
|
104
|
+
# for sessions, URLs that grant access.
|
|
105
|
+
# @return [Logger, nil]
|
|
73
106
|
attr_accessor :logger
|
|
74
107
|
|
|
75
108
|
# @return [Symbol] Log level (:debug, :info, :warn, :error)
|
|
@@ -77,7 +110,7 @@ module OctaSpace
|
|
|
77
110
|
|
|
78
111
|
# --- SSL ---
|
|
79
112
|
|
|
80
|
-
# @return [Boolean] Verify
|
|
113
|
+
# @return [Boolean] Verify the server's TLS certificate
|
|
81
114
|
attr_accessor :ssl_verify
|
|
82
115
|
|
|
83
116
|
# --- Identity ---
|
|
@@ -86,7 +119,7 @@ module OctaSpace
|
|
|
86
119
|
attr_accessor :user_agent
|
|
87
120
|
|
|
88
121
|
DEFAULTS = {
|
|
89
|
-
base_url:
|
|
122
|
+
base_url: DEFAULT_BASE_URL,
|
|
90
123
|
open_timeout: 10,
|
|
91
124
|
read_timeout: 30,
|
|
92
125
|
write_timeout: 30,
|
|
@@ -97,6 +130,8 @@ module OctaSpace
|
|
|
97
130
|
max_retries: 2,
|
|
98
131
|
retry_interval: 0.5,
|
|
99
132
|
backoff_factor: 2.0,
|
|
133
|
+
max_retry_interval: 10.0,
|
|
134
|
+
max_retry_after: 60,
|
|
100
135
|
ssl_verify: true,
|
|
101
136
|
log_level: :info
|
|
102
137
|
}.freeze
|
|
@@ -104,21 +139,29 @@ module OctaSpace
|
|
|
104
139
|
def initialize
|
|
105
140
|
DEFAULTS.each { |k, v| public_send(:"#{k}=", v) }
|
|
106
141
|
@user_agent = "octaspace-ruby/#{OctaSpace::VERSION} Ruby/#{RUBY_VERSION}"
|
|
142
|
+
apply_env
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# Sets the API key; surrounding whitespace and a leading "Bearer " are
|
|
146
|
+
# removed, and an empty value means no key.
|
|
147
|
+
# @param value [String, nil]
|
|
148
|
+
def api_key=(value)
|
|
149
|
+
key = value.to_s.strip.sub(/\Abearer\s+/i, "")
|
|
150
|
+
@api_key = key.empty? ? nil : key
|
|
107
151
|
end
|
|
108
152
|
|
|
109
|
-
#
|
|
110
|
-
|
|
111
|
-
alias_method :persistent=, :keep_alive=
|
|
153
|
+
# @return [Boolean] whether an API key is configured
|
|
154
|
+
def api_key? = !api_key.nil?
|
|
112
155
|
|
|
113
156
|
# @return [Boolean]
|
|
114
157
|
def keep_alive? = !!keep_alive
|
|
115
158
|
|
|
116
|
-
# Returns effective list of API URLs.
|
|
159
|
+
# Returns effective list of API URLs, without trailing slashes.
|
|
117
160
|
# base_urls takes priority over base_url; always returns an Array.
|
|
118
161
|
# @return [Array<String>]
|
|
119
162
|
def urls
|
|
120
|
-
candidates =
|
|
121
|
-
candidates.empty? ?
|
|
163
|
+
candidates = clean_urls(base_urls)
|
|
164
|
+
candidates.empty? ? clean_urls(base_url) : candidates
|
|
122
165
|
end
|
|
123
166
|
|
|
124
167
|
# Deep-clone configuration for per-client overrides
|
|
@@ -132,5 +175,21 @@ module OctaSpace
|
|
|
132
175
|
end
|
|
133
176
|
copy
|
|
134
177
|
end
|
|
178
|
+
|
|
179
|
+
private
|
|
180
|
+
|
|
181
|
+
# Reads OCTA_API_KEY and OCTA_API_URL (one URL, or several separated by commas)
|
|
182
|
+
def apply_env
|
|
183
|
+
self.api_key = ENV["OCTA_API_KEY"]
|
|
184
|
+
urls = clean_urls(ENV["OCTA_API_URL"].to_s.split(","))
|
|
185
|
+
return if urls.empty?
|
|
186
|
+
|
|
187
|
+
self.base_url = urls.first
|
|
188
|
+
self.base_urls = urls if urls.size > 1
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def clean_urls(value)
|
|
192
|
+
Array(value).map { |url| url.to_s.strip.sub(%r{/+\z}, "") }.reject(&:empty?)
|
|
193
|
+
end
|
|
135
194
|
end
|
|
136
195
|
end
|
data/lib/octaspace/errors.rb
CHANGED
|
@@ -3,7 +3,14 @@
|
|
|
3
3
|
module OctaSpace
|
|
4
4
|
# Base error class for all OctaSpace SDK errors
|
|
5
5
|
class Error < StandardError
|
|
6
|
-
|
|
6
|
+
# @return [OctaSpace::Response, nil] the response that caused the error
|
|
7
|
+
attr_reader :response
|
|
8
|
+
|
|
9
|
+
# @return [Integer, nil] HTTP status
|
|
10
|
+
attr_reader :status
|
|
11
|
+
|
|
12
|
+
# @return [String, nil] X-Request-Id of the response
|
|
13
|
+
attr_reader :request_id
|
|
7
14
|
|
|
8
15
|
def initialize(message = nil, response: nil)
|
|
9
16
|
@response = response
|
|
@@ -21,68 +28,73 @@ module OctaSpace
|
|
|
21
28
|
class ConnectionError < NetworkError; end
|
|
22
29
|
class TimeoutError < NetworkError; end
|
|
23
30
|
|
|
24
|
-
#
|
|
25
|
-
class
|
|
26
|
-
|
|
27
|
-
# Domain-level rejection where transport succeeded but the provision request
|
|
28
|
-
# was rejected by the API payload contract.
|
|
29
|
-
class ProvisionRejectedError < Error
|
|
30
|
-
attr_reader :rejections
|
|
31
|
-
|
|
32
|
-
def initialize(message = nil, response: nil, rejections: [])
|
|
33
|
-
@rejections = Array(rejections)
|
|
34
|
-
super(message || build_message(@rejections), response: response)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
private
|
|
38
|
-
|
|
39
|
-
def build_message(rejections)
|
|
40
|
-
first_reason =
|
|
41
|
-
rejections.filter_map do |item|
|
|
42
|
-
next unless item.is_a?(Hash)
|
|
31
|
+
# Raised by sessions.wait_ready / wait_stopped when the timeout runs out
|
|
32
|
+
class WaitTimeoutError < TimeoutError; end
|
|
43
33
|
|
|
44
|
-
|
|
45
|
-
|
|
34
|
+
# An HTTP 4xx/5xx response from the API, or a 2xx response the SDK could not
|
|
35
|
+
# read. The API answers errors as +{"message": "..."}+; the message is
|
|
36
|
+
# used as the exception message.
|
|
37
|
+
class ApiError < Error
|
|
38
|
+
# @return [Integer, nil] seconds to wait before retrying (Retry-After header)
|
|
39
|
+
attr_reader :retry_after
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
# @return [OctaSpace::RateLimit, nil] rate limit state of this response
|
|
42
|
+
attr_reader :rate_limit
|
|
48
43
|
|
|
49
|
-
|
|
44
|
+
def initialize(message = nil, response: nil)
|
|
45
|
+
@retry_after = response&.retry_after
|
|
46
|
+
@rate_limit = response&.rate_limit
|
|
47
|
+
super
|
|
50
48
|
end
|
|
49
|
+
|
|
50
|
+
# @return [Object, nil] parsed response body
|
|
51
|
+
def body = response&.data
|
|
51
52
|
end
|
|
52
53
|
|
|
53
|
-
#
|
|
54
|
+
# 400 or 422: the request was rejected as invalid; the message says why
|
|
55
|
+
# (for example "tail must be 1..1000" or "Invalid node number").
|
|
56
|
+
class ValidationError < ApiError; end
|
|
57
|
+
|
|
58
|
+
# 401: the API key is missing, wrong or deleted. Keys start with +octa_ak_+.
|
|
59
|
+
# Also raised before any request when an endpoint needs a key and the
|
|
60
|
+
# client has none.
|
|
54
61
|
class AuthenticationError < ApiError; end
|
|
55
62
|
|
|
56
63
|
# 403 Forbidden
|
|
57
64
|
class PermissionError < ApiError; end
|
|
58
65
|
|
|
59
|
-
# 404
|
|
66
|
+
# 404: the resource does not exist. For a session this also means it has
|
|
67
|
+
# stopped: the API only reports running sessions.
|
|
60
68
|
class NotFoundError < ApiError; end
|
|
61
69
|
|
|
62
|
-
#
|
|
63
|
-
|
|
70
|
+
# Raised by sessions.wait_ready when the session disappears (404) while it
|
|
71
|
+
# is waited for: it stopped, for example because its SSH setup failed. A
|
|
72
|
+
# stopped session's logs are no longer available.
|
|
73
|
+
class SessionStoppedError < NotFoundError; end
|
|
64
74
|
|
|
65
|
-
#
|
|
66
|
-
|
|
67
|
-
|
|
75
|
+
# 409: the operation conflicts with the current state, e.g. restarting a
|
|
76
|
+
# session that is still starting or restarting.
|
|
77
|
+
class ConflictError < ApiError; end
|
|
68
78
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
super
|
|
72
|
-
end
|
|
73
|
-
end
|
|
79
|
+
# 429: too many requests. Wait {#retry_after} seconds before retrying.
|
|
80
|
+
class RateLimitError < ApiError; end
|
|
74
81
|
|
|
75
82
|
# 5xx Server Errors
|
|
76
83
|
class ServerError < ApiError; end
|
|
77
84
|
class BadGatewayError < ServerError; end # 502
|
|
78
|
-
|
|
85
|
+
|
|
86
|
+
# 503: the service is temporarily unavailable; retry later.
|
|
87
|
+
class ServiceUnavailableError < ServerError; end
|
|
88
|
+
|
|
79
89
|
class GatewayTimeoutError < ServerError; end # 504
|
|
80
90
|
|
|
81
91
|
# HTTP status code → exception class mapping
|
|
82
92
|
STATUS_ERRORS = {
|
|
93
|
+
400 => ValidationError,
|
|
83
94
|
401 => AuthenticationError,
|
|
84
95
|
403 => PermissionError,
|
|
85
96
|
404 => NotFoundError,
|
|
97
|
+
409 => ConflictError,
|
|
86
98
|
422 => ValidationError,
|
|
87
99
|
429 => RateLimitError,
|
|
88
100
|
502 => BadGatewayError,
|
|
@@ -90,13 +102,32 @@ module OctaSpace
|
|
|
90
102
|
504 => GatewayTimeoutError
|
|
91
103
|
}.freeze
|
|
92
104
|
|
|
105
|
+
# Longest error message kept from a response body (e.g. an HTML error page)
|
|
106
|
+
MAX_ERROR_MESSAGE_LENGTH = 512
|
|
107
|
+
|
|
93
108
|
# Build the appropriate error for a given Response
|
|
94
109
|
# @param response [OctaSpace::Response]
|
|
110
|
+
# @param api_key [String, nil] the key the request was sent with, for a hint on 401
|
|
95
111
|
# @return [OctaSpace::ApiError] subclass instance
|
|
96
|
-
def self.error_for(response)
|
|
112
|
+
def self.error_for(response, api_key: nil)
|
|
97
113
|
klass = STATUS_ERRORS.fetch(response.status) do
|
|
98
114
|
response.server_error? ? ServerError : ApiError
|
|
99
115
|
end
|
|
100
|
-
|
|
116
|
+
message = error_message(response)
|
|
117
|
+
if response.status == 401 && api_key && !api_key.start_with?(API_KEY_PREFIX)
|
|
118
|
+
message += " (API keys start with \"#{API_KEY_PREFIX}\"; create one in the OctaSpace dashboard under Account → Credentials → API Keys)"
|
|
119
|
+
end
|
|
120
|
+
klass.new(message, response: response)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @api private
|
|
124
|
+
def self.error_message(response)
|
|
125
|
+
data = response.data
|
|
126
|
+
if data.is_a?(Hash)
|
|
127
|
+
text = data.values_at("message", "error", "detail").find { |v| v.is_a?(String) && !v.empty? }
|
|
128
|
+
end
|
|
129
|
+
text ||= response.body.to_s.strip
|
|
130
|
+
text = "Request failed with status #{response.status}" if text.empty?
|
|
131
|
+
(text.length > MAX_ERROR_MESSAGE_LENGTH) ? "#{text[0, MAX_ERROR_MESSAGE_LENGTH]}…" : text
|
|
101
132
|
end
|
|
102
133
|
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OctaSpace
|
|
4
|
+
# Normalizes values of listings and statistics: the string "null" is read as
|
|
5
|
+
# nil, and a numeric string in an integer GPU field as a number.
|
|
6
|
+
#
|
|
7
|
+
# @api private
|
|
8
|
+
module JsonNull
|
|
9
|
+
module_function
|
|
10
|
+
|
|
11
|
+
# Replaces every "null" string value (not object keys) with nil, in place.
|
|
12
|
+
# Must not be applied where "null" can be real data, such as a session's
|
|
13
|
+
# environment variables.
|
|
14
|
+
# @return [Object] the same value
|
|
15
|
+
def null_strings!(value)
|
|
16
|
+
case value
|
|
17
|
+
when Array
|
|
18
|
+
value.map! { |item| (item == "null") ? nil : null_strings!(item) }
|
|
19
|
+
when Hash
|
|
20
|
+
value.each { |key, item| value[key] = (item == "null") ? nil : null_strings!(item) }
|
|
21
|
+
end
|
|
22
|
+
value
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @return [Integer, Float, nil] a numeric value as is; a numeric string as
|
|
26
|
+
# a number; anything else (blank, "null", text) as nil
|
|
27
|
+
def number(value)
|
|
28
|
+
case value
|
|
29
|
+
when Numeric, nil then value
|
|
30
|
+
when String
|
|
31
|
+
text = value.strip
|
|
32
|
+
if text.match?(/\A-?\d+\z/)
|
|
33
|
+
text.to_i
|
|
34
|
+
elsif text.match?(/\A-?\d+\.\d+\z/)
|
|
35
|
+
text.to_f
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Normalizes integer fields of each GPU object of a machine listing, in place.
|
|
41
|
+
# @param machines [Array<Hash>]
|
|
42
|
+
# @param fields [Array<String>]
|
|
43
|
+
def gpu_numbers!(machines, fields)
|
|
44
|
+
Array(machines).each do |machine|
|
|
45
|
+
next unless machine.is_a?(Hash)
|
|
46
|
+
|
|
47
|
+
Array(machine["gpus"]).each do |gpu|
|
|
48
|
+
next unless gpu.is_a?(Hash)
|
|
49
|
+
|
|
50
|
+
fields.each { |field| gpu[field] = number(gpu[field]) if gpu[field].is_a?(String) }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
machines
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -10,13 +10,13 @@ module OctaSpace
|
|
|
10
10
|
#
|
|
11
11
|
# @example
|
|
12
12
|
# rotator = OctaSpace::Middleware::UrlRotator.new([
|
|
13
|
-
#
|
|
14
|
-
# "https://
|
|
13
|
+
# OctaSpace::DEFAULT_BASE_URL,
|
|
14
|
+
# "https://octaspace-proxy.your-company.example" # your own second host
|
|
15
15
|
# ])
|
|
16
16
|
#
|
|
17
17
|
# url = rotator.next_url
|
|
18
18
|
# rotator.mark_failed(url)
|
|
19
|
-
# rotator.stats # => { total: 2, available: 1, failed: [
|
|
19
|
+
# rotator.stats # => { total: 2, available: 1, failed: [OctaSpace::DEFAULT_BASE_URL] }
|
|
20
20
|
class UrlRotator
|
|
21
21
|
# Seconds a failed URL is excluded from rotation
|
|
22
22
|
FAILURE_COOLDOWN = 30
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module OctaSpace
|
|
4
|
+
# Read-only view over a JSON object returned by the API.
|
|
5
|
+
#
|
|
6
|
+
# Declared attributes get reader methods; nested objects and arrays of
|
|
7
|
+
# objects are wrapped in their own model classes. Fields without a reader
|
|
8
|
+
# are kept and reachable with {#[]} and {#to_h}.
|
|
9
|
+
#
|
|
10
|
+
# @example
|
|
11
|
+
# machine = client.compute.list.first
|
|
12
|
+
# machine.node_number # => "7KQ4-M2XD-4"
|
|
13
|
+
# machine.gpus.first.model # => "NVIDIA GeForce RTX 4090"
|
|
14
|
+
# machine[:new_field] # => value of a field the SDK has no reader for
|
|
15
|
+
# machine.to_h # => the JSON object as parsed
|
|
16
|
+
class Model
|
|
17
|
+
class << self
|
|
18
|
+
# Declares attributes with reader methods.
|
|
19
|
+
# @param names [Array<Symbol>]
|
|
20
|
+
# @param model [Class, nil] wrap a nested object (or each element of an array) in this model
|
|
21
|
+
# @param many [Boolean] the value is an array of objects; nil reads as []
|
|
22
|
+
def attribute(*names, model: nil, many: false)
|
|
23
|
+
names.each do |name|
|
|
24
|
+
key = name.to_s
|
|
25
|
+
attribute_names << name
|
|
26
|
+
if model
|
|
27
|
+
define_method(name) { nested(key, model, many) }
|
|
28
|
+
else
|
|
29
|
+
define_method(name) { @data[key] }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Hides the values of these attributes in #inspect (URLs that grant access).
|
|
35
|
+
def redact(*names)
|
|
36
|
+
redacted_names.concat(names)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# @return [Array<Symbol>]
|
|
40
|
+
def redacted_names
|
|
41
|
+
@redacted_names ||= []
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# @return [Array<Symbol>] declared attribute names
|
|
45
|
+
def attribute_names
|
|
46
|
+
@attribute_names ||= []
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def inherited(subclass)
|
|
50
|
+
super
|
|
51
|
+
subclass.instance_variable_set(:@attribute_names, attribute_names.dup)
|
|
52
|
+
subclass.instance_variable_set(:@redacted_names, redacted_names.dup)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Wraps a JSON object; anything else (nil, a scalar) is returned as is.
|
|
56
|
+
def wrap(value)
|
|
57
|
+
value.is_a?(Hash) ? new(value) : value
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Wraps each JSON object of an array; nil becomes an empty array.
|
|
61
|
+
def wrap_all(values)
|
|
62
|
+
Array(values).map { |value| wrap(value) }
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# @param data [Hash] JSON object with string keys
|
|
67
|
+
def initialize(data = {})
|
|
68
|
+
@data = data.to_h.transform_keys(&:to_s)
|
|
69
|
+
@nested = {}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Value of any field, including ones without a reader
|
|
73
|
+
# @param key [String, Symbol]
|
|
74
|
+
def [](key) = @data[key.to_s]
|
|
75
|
+
|
|
76
|
+
# @param key [String, Symbol]
|
|
77
|
+
# @return [Boolean] whether the API sent this field (it may be null)
|
|
78
|
+
def key?(key) = @data.key?(key.to_s)
|
|
79
|
+
|
|
80
|
+
# @return [Hash] the JSON object as parsed (string keys, nested values unwrapped)
|
|
81
|
+
def to_h = @data
|
|
82
|
+
|
|
83
|
+
# Pattern matching support: +in {node_number:, base_price:}+
|
|
84
|
+
def deconstruct_keys(keys)
|
|
85
|
+
(keys || @data.keys.map(&:to_sym)).to_h { |key| [key, respond_to?(key) ? public_send(key) : @data[key.to_s]] }
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def ==(other)
|
|
89
|
+
other.instance_of?(self.class) && other.to_h == to_h
|
|
90
|
+
end
|
|
91
|
+
alias_method :eql?, :==
|
|
92
|
+
|
|
93
|
+
def hash = [self.class, @data].hash
|
|
94
|
+
|
|
95
|
+
def inspect
|
|
96
|
+
shown = self.class.attribute_names.filter_map do |name|
|
|
97
|
+
value = @data[name.to_s]
|
|
98
|
+
next if !@data.key?(name.to_s) || value.is_a?(Enumerable)
|
|
99
|
+
|
|
100
|
+
"#{name}=#{(value && self.class.redacted_names.include?(name)) ? "[REDACTED]" : value.inspect}"
|
|
101
|
+
end
|
|
102
|
+
"#<#{self.class.name} #{shown.join(" ")}>"
|
|
103
|
+
end
|
|
104
|
+
alias_method :to_s, :inspect
|
|
105
|
+
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
def nested(key, model, many)
|
|
109
|
+
return @nested[key] if @nested.key?(key)
|
|
110
|
+
|
|
111
|
+
value = @data[key]
|
|
112
|
+
@nested[key] = (many || value.is_a?(Array)) ? model.wrap_all(value) : model.wrap(value)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|