withruntime 0.28.0 → 1.0.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.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/lib/withruntime/generated/apps.rb +192 -0
  3. data/lib/withruntime/generated/audit.rb +78 -0
  4. data/lib/withruntime/generated/browsers.rb +192 -0
  5. data/lib/withruntime/generated/builds.rb +192 -0
  6. data/lib/withruntime/generated/ci.rb +192 -0
  7. data/lib/withruntime/generated/containers.rb +192 -0
  8. data/lib/withruntime/generated/desktops.rb +192 -0
  9. data/lib/withruntime/generated/domains.rb +152 -0
  10. data/lib/withruntime/generated/egress_ips.rb +81 -0
  11. data/lib/withruntime/generated/events.rb +60 -0
  12. data/lib/withruntime/generated/feedback.rb +96 -0
  13. data/lib/withruntime/generated/functions.rb +192 -0
  14. data/lib/withruntime/generated/identities.rb +68 -0
  15. data/lib/withruntime/generated/images.rb +391 -0
  16. data/lib/withruntime/generated/job_runs.rb +110 -0
  17. data/lib/withruntime/generated/jobs.rb +333 -0
  18. data/lib/withruntime/generated/keys.rb +261 -0
  19. data/lib/withruntime/generated/kit.rb +111 -0
  20. data/lib/withruntime/generated/me.rb +69 -0
  21. data/lib/withruntime/generated/networks.rb +72 -0
  22. data/lib/withruntime/generated/notebooks.rb +192 -0
  23. data/lib/withruntime/generated/otel.rb +94 -0
  24. data/lib/withruntime/generated/ports.rb +118 -0
  25. data/lib/withruntime/generated/previews.rb +124 -0
  26. data/lib/withruntime/generated/projects.rb +108 -0
  27. data/lib/withruntime/generated/roles.rb +68 -0
  28. data/lib/withruntime/generated/routes.rb +11 -0
  29. data/lib/withruntime/generated/runtime.rb +188 -0
  30. data/lib/withruntime/generated/sandboxes.rb +1835 -0
  31. data/lib/withruntime/generated/secrets.rb +294 -0
  32. data/lib/withruntime/generated/sessions.rb +143 -0
  33. data/lib/withruntime/generated/sites.rb +192 -0
  34. data/lib/withruntime/generated/snapshots.rb +32 -0
  35. data/lib/withruntime/generated/tunnels.rb +165 -0
  36. data/lib/withruntime/generated/usage.rb +287 -0
  37. data/lib/withruntime/generated/volumes.rb +61 -0
  38. data/lib/withruntime/generated/webhooks.rb +91 -0
  39. data/lib/withruntime/generated/workspaces.rb +192 -0
  40. data/lib/withruntime/signing.rb +162 -0
  41. data/lib/withruntime/version.rb +2 -2
  42. data/lib/withruntime.rb +5 -43
  43. metadata +45 -35
  44. data/lib/withruntime/agents.rb +0 -877
  45. data/lib/withruntime/api_defaults.rb +0 -17
  46. data/lib/withruntime/apps.rb +0 -377
  47. data/lib/withruntime/browsers.rb +0 -104
  48. data/lib/withruntime/builds.rb +0 -218
  49. data/lib/withruntime/ci.rb +0 -164
  50. data/lib/withruntime/client.rb +0 -140
  51. data/lib/withruntime/containers.rb +0 -39
  52. data/lib/withruntime/credentials.rb +0 -111
  53. data/lib/withruntime/fields.rb +0 -40
  54. data/lib/withruntime/file_watch.rb +0 -73
  55. data/lib/withruntime/images.rb +0 -258
  56. data/lib/withruntime/network_products.rb +0 -49
  57. data/lib/withruntime/parity_products.rb +0 -51
  58. data/lib/withruntime/products.rb +0 -560
  59. data/lib/withruntime/record.rb +0 -99
  60. data/lib/withruntime/reservations.rb +0 -76
  61. data/lib/withruntime/sandbox.rb +0 -775
  62. data/lib/withruntime/tar.rb +0 -292
  63. data/lib/withruntime/transport.rb +0 -575
  64. data/lib/withruntime/tunnel.rb +0 -275
  65. data/lib/withruntime/wallet.rb +0 -68
  66. data/lib/withruntime/websocket.rb +0 -384
@@ -0,0 +1,188 @@
1
+ # Generated by spec-gen from engines/front/crates/spec. Do not edit:
2
+ # change the spec and run `cargo run -p runtime-front-spec-gen -- write`.
3
+
4
+ require "json"
5
+ require "net/http"
6
+ require "securerandom"
7
+ require "uri"
8
+ require_relative "../signing"
9
+
10
+ module Withruntime
11
+ module Generated
12
+ # The API version this client is written for, sent as Runtime-Version.
13
+ API_VERSION = "2026-10-27"
14
+
15
+ # The answers sent again after a wait (the spec's calls::RETRIES): status,
16
+ # code (nil: any), and whether only when the answer names its wait.
17
+ RETRIES = [[429, "rate_limited", true], [503, "unavailable", false], [503, "capacity_unavailable", false]].freeze
18
+
19
+ # The most times a request is sent again.
20
+ MAX_RETRIES = 3
21
+
22
+ # The longest wait, in ms, a retry sleeps on: a refusal that names a
23
+ # longer one is raised at once, with its retry_after_ms.
24
+ LONGEST_WAIT_MS = 60000
25
+
26
+ # How long a call waits for its answer, in ms, when it runs for no time
27
+ # its caller names; one that does waits that plus WAIT_MARGIN_MS.
28
+ CALL_TIMEOUT_MS = 60000
29
+ WAIT_MARGIN_MS = 10000
30
+
31
+ # The reason a run's answer was cut while the run goes on: followed on
32
+ # under the same key.
33
+ RUN_GOES_ON = "run_goes_on"
34
+
35
+ # An error the API returned: its body's code, message and request ID,
36
+ # the answer's status, and whether it is sent again (the spec's retries).
37
+ class ApiError < StandardError
38
+ attr_reader :code, :status, :request_id, :retryable, :retry_after_ms
39
+
40
+ def initialize(status:, code:, message:, request_id:, retry_after_ms:)
41
+ super(message.to_s)
42
+ @code = code
43
+ @status = status
44
+ @request_id = request_id
45
+ @retry_after_ms = retry_after_ms
46
+ @retryable = RETRIES.any? { |s, c, w| s == status && (c.nil? || c == code) && (!retry_after_ms.nil? || !w) }
47
+ end
48
+ end
49
+
50
+ # What every list returns: a page of its items, and where the next page
51
+ # starts (the next call's cursor) when there is one.
52
+ Page = Struct.new(:items, :next, keyword_init: true)
53
+
54
+ def self.page(h)
55
+ Page.new(items: (h["items"] || []).map { |i| yield i }, next: h["next"])
56
+ end
57
+
58
+ # HTTP, retries and errors, shared by every product's client.
59
+ class Transport
60
+ # +api_key+: a bearer key, or a key pair's rtsk_ secret, which signs each
61
+ # request for +project+ (default RUNTIME_PROJECT) and is never sent.
62
+ def initialize(api_key: nil, base_url: nil, max_retries: MAX_RETRIES, project: nil)
63
+ @api_key = api_key || ENV["RUNTIME_API_KEY"]
64
+ @project = project || ENV["RUNTIME_PROJECT"]
65
+ @base_url = (base_url || ENV["RUNTIME_API_URL"] || "https://api.withruntime.com").chomp("/")
66
+ @max_retries = max_retries
67
+ end
68
+
69
+ # Sent again after a lost connection or a timeout only when +idempotent+.
70
+ # A verb that +runs+ is waited on as long as it may (+runs_seconds+) and a
71
+ # margin, or to its end when that is nil or 0: its answer begins at once with
72
+ # a space a read, so each read of it waits CALL_TIMEOUT_MS at most.
73
+ def request(method, path, query: nil, body: nil, idempotency_key: nil, data: nil, raw: false, idempotent: false, runs: false, runs_seconds: nil)
74
+ timeout = runs && runs_seconds.to_f.positive? ? runs_seconds + WAIT_MARGIN_MS / 1000.0 : CALL_TIMEOUT_MS / 1000.0
75
+ uri = URI(@base_url + path)
76
+ q = (query || {}).reject { |_, v| v.nil? }
77
+ uri.query = URI.encode_www_form(q) unless q.empty?
78
+ headers = { "accept" => raw ? "application/octet-stream" : "application/json", "runtime-version" => API_VERSION }
79
+ # A malformed key pair's secret, or one with no project, is refused here.
80
+ ::WithRuntime::Signing.key_for(@api_key, @project) if @api_key
81
+ headers["idempotency-key"] = idempotency_key || SecureRandom.uuid unless method == "GET"
82
+ if !data.nil?
83
+ headers["content-type"] = "application/octet-stream"
84
+ elsif !body.nil?
85
+ data = JSON.generate(body)
86
+ headers["content-type"] = "application/json"
87
+ end
88
+ attempt = 0
89
+ loop do
90
+ backoff = [2.0, 0.1 * (2**attempt)].min
91
+ begin
92
+ # Each attempt authorized anew: a key pair signs it, as the server
93
+ # takes a nonce once.
94
+ auth = @api_key ? ::WithRuntime::Signing.authorize(@api_key, @project, method, uri.request_uri, data, idempotency_key: headers["idempotency-key"]) : {}
95
+ res = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
96
+ open_timeout: timeout, read_timeout: timeout, write_timeout: timeout) do |http|
97
+ http.send_request(method, uri.request_uri, data, headers.merge(auth))
98
+ end
99
+ rescue SystemCallError, IOError, Timeout::Error
100
+ raise if !idempotent || attempt >= @max_retries
101
+ sleep(backoff)
102
+ attempt += 1
103
+ next
104
+ end
105
+ code = res.code.to_i
106
+ return res.body.to_s.b if code < 300 && raw
107
+ answer = code < 300 && !(res.body.nil? || res.body.empty?) ? JSON.parse(res.body) : nil
108
+ # A run's answer begins at once, so an error after that is in its body.
109
+ said = runs && answer.is_a?(Hash) && answer["error"].is_a?(Hash) ? answer["error"] : nil
110
+ return answer if code < 300 && said.nil?
111
+ err = said || begin
112
+ JSON.parse(res.body)["error"]
113
+ rescue StandardError
114
+ nil
115
+ end
116
+ err = {} unless err.is_a?(Hash)
117
+ after = res["retry-after"].to_s
118
+ error = ApiError.new(
119
+ status: said ? (said["status"] || 503).to_i : code,
120
+ code: err["code"] || "http_#{code}",
121
+ message: err["message"] || res.body.to_s,
122
+ request_id: err["requestId"] || res["request-id"].to_s,
123
+ retry_after_ms: after.match?(/\A\d+\z/) ? after.to_i * 1000 : nil
124
+ )
125
+ # A run whose answer a release cut goes on: followed on under the
126
+ # same key, its retries counted afresh, so releases mid-run spend none.
127
+ attempt = -1 if said && said["details"].is_a?(Hash) && said["details"]["reason"] == RUN_GOES_ON
128
+ # A longer wait than LONGEST_WAIT_MS (a daily cap) is the caller's
129
+ # to schedule: raised at once.
130
+ wait = error.retry_after_ms ? error.retry_after_ms / 1000.0 : backoff
131
+ if error.retryable && attempt < @max_retries && wait * 1000 <= LONGEST_WAIT_MS
132
+ sleep(wait)
133
+ attempt += 1
134
+ next
135
+ end
136
+ raise error
137
+ end
138
+ end
139
+ end
140
+
141
+ # Every route an OpenAPI document of this API names, by "product.verb":
142
+ # how a verb newer than this SDK is found in the API's served one.
143
+ def self.routes_of(doc)
144
+ out = {}
145
+ (doc && doc["paths"] || {}).each do |path, methods|
146
+ methods.each do |method, op|
147
+ next unless op.is_a?(Hash) && op["operationId"].is_a?(String)
148
+ params = op["parameters"] || []
149
+ content = (op["requestBody"] || {})["content"] || {}
150
+ ok = ((op["responses"] || {})["200"] || {})["content"] || {}
151
+ runs = op["x-runs"]
152
+ out[op["operationId"]] = {
153
+ "method" => method.upcase, "path" => path,
154
+ "params" => params.select { |p| p["in"] == "path" }.map { |p| p["name"] },
155
+ "query" => params.select { |p| p["in"] == "query" }.map { |p| p["name"] },
156
+ "body" => content.key?("application/json") ? "json" : (content.key?("application/octet-stream") ? "bytes" : "none"),
157
+ "raw" => ok.key?("application/octet-stream"), "idempotent" => op["x-idempotent"] == true,
158
+ "runs" => runs ? runs["field"] : nil
159
+ }
160
+ end
161
+ end
162
+ out
163
+ end
164
+
165
+ # A verb's call from its arguments by name: its path's parts, its
166
+ # query's fields, and the rest its body ("data", bytes or base64, for a
167
+ # verb whose body is bytes).
168
+ def self.call_route(transport, route, args, idempotency_key: nil)
169
+ rest = args.transform_keys(&:to_s).compact
170
+ path = route["path"].dup
171
+ route["params"].each do |p|
172
+ raise ArgumentError, "#{p} is required." unless rest.key?(p)
173
+ path = path.sub("{#{p}}", URI.encode_www_form_component(rest.delete(p).to_s))
174
+ end
175
+ query = route["query"].each_with_object({}) { |q, h| h[q] = rest.delete(q) if rest.key?(q) }
176
+ data = nil
177
+ if route["body"] == "bytes"
178
+ d = rest.delete("data")
179
+ data = d.is_a?(String) && d.encoding != Encoding::BINARY ? d.unpack1("m") : d
180
+ end
181
+ runs = route["runs"]
182
+ # A verb that runs waits as long as its field says, or to its end when it is left out.
183
+ runs_seconds = runs && !rest[runs].nil? ? Float(rest[runs]) : nil
184
+ transport.request(route["method"], path, query: query.empty? ? nil : query, body: route["body"] == "json" ? rest : nil,
185
+ data: data, raw: route["raw"], idempotency_key: idempotency_key, idempotent: route["idempotent"], runs: !runs.nil?, runs_seconds: runs_seconds)
186
+ end
187
+ end
188
+ end