superslice 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +192 -0
- data/lib/superslice/client.rb +261 -0
- data/lib/superslice/errors.rb +52 -0
- data/lib/superslice/version.rb +3 -0
- data/lib/superslice/webhooks.rb +36 -0
- data/lib/superslice.rb +4 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d99c6031fcf11cce156a24395cb3c667bd2f1953e8e612bd7370887efbdbcfcb
|
|
4
|
+
data.tar.gz: b7e2391c2609b48f629443ab4c7cc13f376d4dc3af26991558d321e65ba85dd3
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 66a39a214f95283ec6addc2c15dd4e272d9a4135572151e810aaea666367e18ac7a1bf27c7a73062974ef84f7fed56864bc32d91376bcaa8eddbad75db9d5042
|
|
7
|
+
data.tar.gz: 1a7a6edfe0961426477ddb57a370284c06538fc11f064dff4b567d1921d903e958747ffb8e6b88830fc192015369cf8e3320fdd3c87cbc022187293c12705cb7
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Superslice
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://superslice.co"><img src=".github/banner.png" alt="Superslice for Ruby" width="100%"></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://github.com/superslice-co/superslice-ruby/actions/workflows/ci.yml"><img src="https://github.com/superslice-co/superslice-ruby/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
|
|
7
|
+
<img src="https://img.shields.io/badge/Ruby-3.0%2B-121314" alt="Ruby 3.0+">
|
|
8
|
+
<img src="https://img.shields.io/badge/licence-MIT-97d9c2" alt="MIT licence">
|
|
9
|
+
</p>
|
|
10
|
+
|
|
11
|
+
<p align="center">
|
|
12
|
+
<a href="https://docs.superslice.co">Docs</a> ·
|
|
13
|
+
<a href="https://docs.superslice.co/api/post-slices">API reference</a> ·
|
|
14
|
+
<a href="https://app.superslice.co/keys">Get an API key</a> ·
|
|
15
|
+
<a href="https://status.superslice.co">Status</a>
|
|
16
|
+
</p>
|
|
17
|
+
|
|
18
|
+
# Superslice for Ruby
|
|
19
|
+
|
|
20
|
+
Superslice is a slicing API for 3D print quotes: send a model and get print time, filament use and a price from the real Bambu Studio, PrusaSlicer, Creality Print or ElegooSlicer engine.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
The gem is not yet published to RubyGems. Until it is, install from GitHub. In your Gemfile:
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
gem "superslice", git: "https://github.com/superslice-co/superslice-ruby.git"
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Once published: `gem install superslice`. Needs Ruby 3.0 or newer. No runtime dependencies.
|
|
31
|
+
|
|
32
|
+
## Quickstart
|
|
33
|
+
|
|
34
|
+
```ruby
|
|
35
|
+
require "superslice"
|
|
36
|
+
|
|
37
|
+
client = Superslice::Client.new(api_key: ENV["SUPERSLICE_API_KEY"])
|
|
38
|
+
|
|
39
|
+
slice = client.slices.create(
|
|
40
|
+
{ slicer: "bambu-studio",
|
|
41
|
+
printer: "Bambu Lab A1 0.4 nozzle",
|
|
42
|
+
process: "0.20mm Standard @BBL A1",
|
|
43
|
+
filament: "Bambu PLA Basic @BBL A1",
|
|
44
|
+
plate_type: "Textured PEI Plate",
|
|
45
|
+
metadata: { order_id: "1042" } },
|
|
46
|
+
file: "cube.stl"
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
slice = client.slices.wait(slice[:id])
|
|
50
|
+
|
|
51
|
+
if slice[:status] == "succeeded"
|
|
52
|
+
puts slice[:result][:print_time_s]
|
|
53
|
+
puts slice[:result][:filament_g]
|
|
54
|
+
puts slice[:price][:total_minor] if slice[:price]
|
|
55
|
+
else
|
|
56
|
+
puts slice[:error][:code]
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Every response is a plain Hash with symbol keys, exactly as the API returns it (see the [API reference](https://docs.superslice.co/api/post-slices) for the shapes). Fields the library does not know about are kept, so new API fields never break parsing. Money is an integer in `price[:total_minor]`.
|
|
61
|
+
|
|
62
|
+
To slice a model you already uploaded, or one at a URL, send JSON instead of a file:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
client.slices.create({ slicer: "prusa-slicer", printer: "...", process: "...", filament: "...",
|
|
66
|
+
model: { id: "mdl_..." } })
|
|
67
|
+
client.slices.create({ slicer: "prusa-slicer", printer: "...", process: "...", filament: "...",
|
|
68
|
+
model: { source_url: "https://example.com/part.stl" } })
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
`file:` takes a path, a `Pathname` or an IO. For an IO without a path, add `filename: "part.stl"`.
|
|
72
|
+
|
|
73
|
+
Other calls: `slices.get(id)`, `slices.list(limit:, cursor:)` (returns `{ data:, next_cursor: }`), `slices.list_all` (an Enumerator over every slice), `slices.wait(id, interval: 2, timeout: 600)`, `models.get(id)`, `models.delete(id)`.
|
|
74
|
+
|
|
75
|
+
## Catalogue lookup
|
|
76
|
+
|
|
77
|
+
Preset names come from the catalogue. Releases contain `@` and `+`; the library encodes them for you.
|
|
78
|
+
|
|
79
|
+
```ruby
|
|
80
|
+
release = client.engines.list[:data].find { |e| e[:slicer] == "bambu-studio" && e[:status] == "active" }[:id]
|
|
81
|
+
printers = client.engines.printers(release)[:data]
|
|
82
|
+
name = printers.first[:name]
|
|
83
|
+
client.engines.processes(release, name)
|
|
84
|
+
client.engines.filaments(release, name)
|
|
85
|
+
client.engines.plate_types(release, name)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Presets, pricing and slicer settings:
|
|
89
|
+
|
|
90
|
+
```ruby
|
|
91
|
+
client.presets.create("shop-pla.json", "bambu-studio")
|
|
92
|
+
client.presets.list
|
|
93
|
+
client.presets.update("pst_...", name: "Shop PLA")
|
|
94
|
+
client.presets.delete("pst_...")
|
|
95
|
+
|
|
96
|
+
client.pricing.get
|
|
97
|
+
client.pricing.set({ enabled: true, currency: "USD", minimum: "2.00", lines: [] })
|
|
98
|
+
client.pricing.delete
|
|
99
|
+
|
|
100
|
+
client.slicers.list
|
|
101
|
+
client.slicers.update("bambu-studio", mode: "pick", engine_releases: ["bambu-studio@02.09.02.62+02.09.00.89"])
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Idempotency
|
|
105
|
+
|
|
106
|
+
Pass an idempotency key so a repeated request returns the same slice instead of slicing twice:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
client.slices.create(params, file: "cube.stl", idempotency_key: "order-1042")
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Errors
|
|
113
|
+
|
|
114
|
+
Every error is a `Superslice::SupersliceError` with `status`, `code`, `title`, `errors`, `retry_after` and the raw `body`.
|
|
115
|
+
|
|
116
|
+
| Class | When |
|
|
117
|
+
|---|---|
|
|
118
|
+
| `AuthenticationError` | 401 |
|
|
119
|
+
| `PlanLimitError` | 402 |
|
|
120
|
+
| `PermissionError` | 403 |
|
|
121
|
+
| `NotFoundError` | 404 |
|
|
122
|
+
| `ConflictError` | 409 |
|
|
123
|
+
| `InvalidRequestError` | 400, 413 |
|
|
124
|
+
| `RateLimitError` | 429 |
|
|
125
|
+
| `ServerError` | 5xx |
|
|
126
|
+
| `ConnectionError`, `TimeoutError` | transport problems, or `slices.wait` running out of time |
|
|
127
|
+
| `SignatureError` | webhook verification failed |
|
|
128
|
+
|
|
129
|
+
```ruby
|
|
130
|
+
begin
|
|
131
|
+
client.slices.get("job_missing")
|
|
132
|
+
rescue Superslice::NotFoundError => e
|
|
133
|
+
puts e.code # "not_found"
|
|
134
|
+
rescue Superslice::SupersliceError => e
|
|
135
|
+
puts "#{e.status} #{e.code}: #{e.title}"
|
|
136
|
+
end
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
A slice that fails after it was accepted is not an exception. `slices.wait` returns it with `status: "failed"` and `error: { code:, message: }`.
|
|
140
|
+
|
|
141
|
+
## Retries
|
|
142
|
+
|
|
143
|
+
On 429 the client waits `Retry-After` seconds (1 second if absent). On 5xx and network errors it waits 0.5, 1 and 2 seconds. GET, PUT, PATCH and DELETE retry freely. `POST /v1/slices` retries only when you pass `idempotency_key`, because a retry without one could slice twice. The default is 2 retries; set `max_retries:` to change it.
|
|
144
|
+
|
|
145
|
+
## Webhooks
|
|
146
|
+
|
|
147
|
+
Verify the `Superslice-Signature` header against the raw request body, before parsing. `verify` returns the parsed event or raises `Superslice::SignatureError`.
|
|
148
|
+
|
|
149
|
+
```ruby
|
|
150
|
+
# Rack or Sinatra style
|
|
151
|
+
post "/superslice" do
|
|
152
|
+
raw = request.body.read
|
|
153
|
+
event = Superslice::Webhooks.verify(ENV["SUPERSLICE_WEBHOOK_SECRET"],
|
|
154
|
+
request.env["HTTP_SUPERSLICE_SIGNATURE"], raw)
|
|
155
|
+
if event[:event] == "slice.completed"
|
|
156
|
+
puts event[:data][:result][:print_time_s]
|
|
157
|
+
end
|
|
158
|
+
"ok"
|
|
159
|
+
rescue Superslice::SignatureError
|
|
160
|
+
halt 400, "invalid signature"
|
|
161
|
+
end
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Timestamps more than 300 seconds from now are rejected; change it with `tolerance_seconds:`. The same event can arrive twice, so ignore `event_id` values you have seen. `client.webhooks.verify` is the same function. `Webhooks.unwrap` is an alias.
|
|
165
|
+
|
|
166
|
+
## Configuration
|
|
167
|
+
|
|
168
|
+
```ruby
|
|
169
|
+
Superslice::Client.new(
|
|
170
|
+
api_key: "...", # default: ENV["SUPERSLICE_API_KEY"]; missing key raises ArgumentError
|
|
171
|
+
base_url: "https://api.superslice.co", # default
|
|
172
|
+
timeout: 60, # seconds, per request
|
|
173
|
+
max_retries: 2
|
|
174
|
+
)
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Development
|
|
178
|
+
|
|
179
|
+
```sh
|
|
180
|
+
bundle install
|
|
181
|
+
bundle exec rake test # unit tests, local mock server
|
|
182
|
+
SUPERSLICE_API_KEY=... SUPERSLICE_BASE_URL=http://localhost:8080 bundle exec rake live # live smoke test
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Links
|
|
186
|
+
|
|
187
|
+
- Docs: https://docs.superslice.co
|
|
188
|
+
- API reference: https://docs.superslice.co/api/post-slices
|
|
189
|
+
- Status: https://status.superslice.co
|
|
190
|
+
- Support: hello@superslice.co
|
|
191
|
+
|
|
192
|
+
MIT licence.
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
require "erb"
|
|
2
|
+
require "json"
|
|
3
|
+
require "net/http"
|
|
4
|
+
require "openssl"
|
|
5
|
+
require "securerandom"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
module Superslice
|
|
9
|
+
# Responses are plain Hashes with symbol keys. Unknown fields are kept.
|
|
10
|
+
class Client
|
|
11
|
+
DEFAULT_BASE_URL = "https://api.superslice.co".freeze
|
|
12
|
+
|
|
13
|
+
attr_reader :slices, :models, :engines, :presets, :pricing, :slicers, :webhooks
|
|
14
|
+
|
|
15
|
+
def initialize(api_key: nil, base_url: DEFAULT_BASE_URL, timeout: 60, max_retries: 2, sleeper: nil)
|
|
16
|
+
api_key ||= ENV["SUPERSLICE_API_KEY"]
|
|
17
|
+
if api_key.nil? || api_key.empty?
|
|
18
|
+
raise ArgumentError, "Superslice API key missing: pass api_key: or set SUPERSLICE_API_KEY"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
@api_key = api_key
|
|
22
|
+
@base_url = base_url.to_s.chomp("/")
|
|
23
|
+
@timeout = timeout
|
|
24
|
+
@max_retries = max_retries
|
|
25
|
+
@sleeper = sleeper || ->(seconds) { sleep(seconds) }
|
|
26
|
+
@slices = Slices.new(self)
|
|
27
|
+
@models = Models.new(self)
|
|
28
|
+
@engines = Engines.new(self)
|
|
29
|
+
@presets = Presets.new(self)
|
|
30
|
+
@pricing = Pricing.new(self)
|
|
31
|
+
@slicers = Slicers.new(self)
|
|
32
|
+
@webhooks = Webhooks
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Sends a request and returns the parsed JSON body (nil when empty).
|
|
36
|
+
# +parts+ is a list of [name, value, filename_or_nil, content_type_or_nil] for multipart bodies.
|
|
37
|
+
def request(method, path, query: nil, json: nil, parts: nil, headers: {}, retry_ok: true)
|
|
38
|
+
uri = build_uri(path, query)
|
|
39
|
+
req = Net::HTTP.const_get(method.to_s.capitalize).new(uri.request_uri)
|
|
40
|
+
req["Authorization"] = "Bearer #{@api_key}"
|
|
41
|
+
req["User-Agent"] = "superslice-ruby/#{VERSION}"
|
|
42
|
+
req["Accept"] = "application/json"
|
|
43
|
+
headers.each { |k, v| req[k] = v }
|
|
44
|
+
if parts
|
|
45
|
+
boundary = "superslice-#{SecureRandom.hex(16)}"
|
|
46
|
+
req["Content-Type"] = "multipart/form-data; boundary=#{boundary}"
|
|
47
|
+
req.body = multipart_body(parts, boundary)
|
|
48
|
+
elsif json
|
|
49
|
+
req["Content-Type"] = "application/json"
|
|
50
|
+
req.body = JSON.generate(json)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
attempt = 0
|
|
54
|
+
loop do
|
|
55
|
+
retryable = retry_ok && attempt < @max_retries
|
|
56
|
+
begin
|
|
57
|
+
res = perform(uri, req)
|
|
58
|
+
rescue ConnectionError, TimeoutError
|
|
59
|
+
raise unless retryable
|
|
60
|
+
|
|
61
|
+
@sleeper.call(0.5 * (2**attempt))
|
|
62
|
+
attempt += 1
|
|
63
|
+
next
|
|
64
|
+
end
|
|
65
|
+
status = res.code.to_i
|
|
66
|
+
return parse(res.body) if status < 400
|
|
67
|
+
|
|
68
|
+
retry_after = res["Retry-After"] && Float(res["Retry-After"], exception: false)
|
|
69
|
+
if retryable && (status == 429 || status >= 500)
|
|
70
|
+
@sleeper.call(status == 429 ? (retry_after || 1) : 0.5 * (2**attempt))
|
|
71
|
+
attempt += 1
|
|
72
|
+
next
|
|
73
|
+
end
|
|
74
|
+
raise Superslice.error_for(status, parse(res.body, tolerant: true), retry_after)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Reads a file argument (path, Pathname or IO) into [bytes, filename].
|
|
79
|
+
def self.read_file(file, filename = nil)
|
|
80
|
+
if file.respond_to?(:read)
|
|
81
|
+
name = filename || (file.respond_to?(:path) && file.path ? File.basename(file.path) : "upload")
|
|
82
|
+
[file.read, name]
|
|
83
|
+
else
|
|
84
|
+
path = file.to_s
|
|
85
|
+
[File.binread(path), filename || File.basename(path)]
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def build_uri(path, query)
|
|
92
|
+
q = (query || {}).reject { |_, v| v.nil? }
|
|
93
|
+
.map { |k, v| "#{ERB::Util.url_encode(k.to_s)}=#{ERB::Util.url_encode(v.to_s)}" }.join("&")
|
|
94
|
+
URI("#{@base_url}#{path}#{q.empty? ? '' : "?#{q}"}")
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def perform(uri, req)
|
|
98
|
+
Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https",
|
|
99
|
+
open_timeout: @timeout, read_timeout: @timeout, write_timeout: @timeout) do |http|
|
|
100
|
+
http.max_retries = 0 # retries are ours: Net::HTTP would silently replay idempotent requests
|
|
101
|
+
http.request(req)
|
|
102
|
+
end
|
|
103
|
+
rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout => e
|
|
104
|
+
raise TimeoutError, "request timed out: #{e.message}"
|
|
105
|
+
rescue SocketError, SystemCallError, IOError, OpenSSL::SSL::SSLError => e
|
|
106
|
+
raise ConnectionError, "connection failed: #{e.message}"
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def parse(body, tolerant: false)
|
|
110
|
+
return nil if body.nil? || body.empty?
|
|
111
|
+
|
|
112
|
+
JSON.parse(body, symbolize_names: true)
|
|
113
|
+
rescue JSON::ParserError
|
|
114
|
+
raise unless tolerant
|
|
115
|
+
|
|
116
|
+
nil
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def multipart_body(parts, boundary)
|
|
120
|
+
out = "".b
|
|
121
|
+
parts.each do |name, value, filename, content_type|
|
|
122
|
+
out << "--#{boundary}\r\n".b
|
|
123
|
+
disposition = "Content-Disposition: form-data; name=\"#{name}\""
|
|
124
|
+
disposition += "; filename=\"#{filename.gsub(/["\r\n]/, '_')}\"" if filename
|
|
125
|
+
out << "#{disposition}\r\n".b
|
|
126
|
+
out << "Content-Type: #{content_type}\r\n".b if content_type
|
|
127
|
+
out << "\r\n".b << value.to_s.b << "\r\n".b
|
|
128
|
+
end
|
|
129
|
+
out << "--#{boundary}--\r\n".b
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
class Resource
|
|
134
|
+
def initialize(client)
|
|
135
|
+
@client = client
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
private
|
|
139
|
+
|
|
140
|
+
def enc(value)
|
|
141
|
+
ERB::Util.url_encode(value.to_s)
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
class Slices < Resource
|
|
146
|
+
# Without +file+, +params+ is sent as JSON and +params[:model]+ is
|
|
147
|
+
# { id: } or { source_url:, filename: }. With +file+ (path, Pathname or IO),
|
|
148
|
+
# the file is uploaded and +params+ goes in the "request" part.
|
|
149
|
+
# A POST is retried only when +idempotency_key+ is given.
|
|
150
|
+
def create(params, file: nil, filename: nil, idempotency_key: nil)
|
|
151
|
+
headers = idempotency_key ? { "Idempotency-Key" => idempotency_key } : {}
|
|
152
|
+
retry_ok = !idempotency_key.nil?
|
|
153
|
+
if file
|
|
154
|
+
bytes, name = Client.read_file(file, filename)
|
|
155
|
+
parts = [["file", bytes, name, "application/octet-stream"],
|
|
156
|
+
["request", JSON.generate(params), nil, "application/json"]]
|
|
157
|
+
@client.request(:post, "/v1/slices", parts: parts, headers: headers, retry_ok: retry_ok)
|
|
158
|
+
else
|
|
159
|
+
@client.request(:post, "/v1/slices", json: params, headers: headers, retry_ok: retry_ok)
|
|
160
|
+
end
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def get(id)
|
|
164
|
+
@client.request(:get, "/v1/slices/#{enc(id)}")
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Returns { data: [...], next_cursor: String or nil }.
|
|
168
|
+
def list(limit: nil, cursor: nil)
|
|
169
|
+
@client.request(:get, "/v1/slices", query: { limit: limit, cursor: cursor })
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Enumerator over every slice, newest first, fetching pages as needed.
|
|
173
|
+
def list_all(limit: nil)
|
|
174
|
+
Enumerator.new do |y|
|
|
175
|
+
cursor = nil
|
|
176
|
+
loop do
|
|
177
|
+
page = list(limit: limit, cursor: cursor)
|
|
178
|
+
page[:data].each { |s| y << s }
|
|
179
|
+
cursor = page[:next_cursor]
|
|
180
|
+
break if cursor.nil?
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Polls until the slice is succeeded or failed and returns it; a failed
|
|
186
|
+
# slice is returned, not raised. Raises TimeoutError when time runs out.
|
|
187
|
+
def wait(id, interval: 2, timeout: 600)
|
|
188
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
189
|
+
loop do
|
|
190
|
+
slice = get(id)
|
|
191
|
+
return slice if %w[succeeded failed].include?(slice[:status])
|
|
192
|
+
if Process.clock_gettime(Process::CLOCK_MONOTONIC) + interval > deadline
|
|
193
|
+
raise TimeoutError, "slice #{id} still #{slice[:status]} after #{timeout}s"
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
sleep(interval)
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
|
|
201
|
+
class Models < Resource
|
|
202
|
+
def get(id) = @client.request(:get, "/v1/models/#{enc(id)}")
|
|
203
|
+
|
|
204
|
+
def delete(id) = @client.request(:delete, "/v1/models/#{enc(id)}")
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
class Engines < Resource
|
|
208
|
+
def list = @client.request(:get, "/v1/engines")
|
|
209
|
+
|
|
210
|
+
def printers(release) = @client.request(:get, "/v1/engines/#{enc(release)}/printers")
|
|
211
|
+
|
|
212
|
+
def processes(release, printer)
|
|
213
|
+
@client.request(:get, "/v1/engines/#{enc(release)}/processes", query: { printer: printer })
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def filaments(release, printer)
|
|
217
|
+
@client.request(:get, "/v1/engines/#{enc(release)}/filaments", query: { printer: printer })
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def plate_types(release, printer)
|
|
221
|
+
@client.request(:get, "/v1/engines/#{enc(release)}/plate-types", query: { printer: printer })
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
class Presets < Resource
|
|
226
|
+
# +file+ is a path, Pathname or IO; +slicer+ is needed except for .ini files.
|
|
227
|
+
def create(file, slicer = nil, filename: nil)
|
|
228
|
+
bytes, name = Client.read_file(file, filename)
|
|
229
|
+
parts = [["file", bytes, name, "application/octet-stream"]]
|
|
230
|
+
parts << ["slicer", slicer] if slicer
|
|
231
|
+
@client.request(:post, "/v1/presets", parts: parts)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
def list = @client.request(:get, "/v1/presets")
|
|
235
|
+
|
|
236
|
+
def get(id) = @client.request(:get, "/v1/presets/#{enc(id)}")
|
|
237
|
+
|
|
238
|
+
def update(id, name:) = @client.request(:patch, "/v1/presets/#{enc(id)}", json: { name: name })
|
|
239
|
+
|
|
240
|
+
def delete(id) = @client.request(:delete, "/v1/presets/#{enc(id)}")
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
class Pricing < Resource
|
|
244
|
+
def get = @client.request(:get, "/v1/pricing")
|
|
245
|
+
|
|
246
|
+
def set(rules) = @client.request(:put, "/v1/pricing", json: rules)
|
|
247
|
+
|
|
248
|
+
def delete = @client.request(:delete, "/v1/pricing")
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
class Slicers < Resource
|
|
252
|
+
def list = @client.request(:get, "/v1/slicers")
|
|
253
|
+
|
|
254
|
+
# update("bambu-studio", mode: "pick", engine_releases: ["..."])
|
|
255
|
+
def update(slicer, mode:, engine_releases: nil)
|
|
256
|
+
body = { mode: mode }
|
|
257
|
+
body[:engine_releases] = engine_releases if engine_releases
|
|
258
|
+
@client.request(:put, "/v1/slicers/#{enc(slicer)}", json: body)
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
module Superslice
|
|
2
|
+
# Base class for everything this library raises.
|
|
3
|
+
class SupersliceError < StandardError
|
|
4
|
+
attr_reader :status, :code, :title, :errors, :retry_after, :body
|
|
5
|
+
|
|
6
|
+
def initialize(message = nil, status: nil, code: nil, title: nil, errors: nil, retry_after: nil, body: nil)
|
|
7
|
+
super(message || title || code || "Superslice error")
|
|
8
|
+
@status = status
|
|
9
|
+
@code = code
|
|
10
|
+
@title = title
|
|
11
|
+
@errors = errors
|
|
12
|
+
@retry_after = retry_after
|
|
13
|
+
@body = body
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
Error = SupersliceError
|
|
17
|
+
|
|
18
|
+
class AuthenticationError < SupersliceError; end # 401
|
|
19
|
+
class PlanLimitError < SupersliceError; end # 402
|
|
20
|
+
class PermissionError < SupersliceError; end # 403
|
|
21
|
+
class NotFoundError < SupersliceError; end # 404
|
|
22
|
+
class ConflictError < SupersliceError; end # 409
|
|
23
|
+
class InvalidRequestError < SupersliceError; end # 400, 413
|
|
24
|
+
class RateLimitError < SupersliceError; end # 429
|
|
25
|
+
class ServerError < SupersliceError; end # 5xx
|
|
26
|
+
class ConnectionError < SupersliceError; end # transport failure
|
|
27
|
+
class TimeoutError < SupersliceError; end # request or wait() timed out
|
|
28
|
+
class SignatureError < SupersliceError; end # webhook verification failed
|
|
29
|
+
|
|
30
|
+
# Builds the error class for a problem+json response.
|
|
31
|
+
def self.error_for(status, body, retry_after = nil)
|
|
32
|
+
problem = body.is_a?(Hash) ? body : {}
|
|
33
|
+
klass =
|
|
34
|
+
case status
|
|
35
|
+
when 400, 413 then InvalidRequestError
|
|
36
|
+
when 401 then AuthenticationError
|
|
37
|
+
when 402 then PlanLimitError
|
|
38
|
+
when 403 then PermissionError
|
|
39
|
+
when 404 then NotFoundError
|
|
40
|
+
when 409 then ConflictError
|
|
41
|
+
when 429 then RateLimitError
|
|
42
|
+
when 500..599 then ServerError
|
|
43
|
+
else SupersliceError
|
|
44
|
+
end
|
|
45
|
+
title = problem[:title]
|
|
46
|
+
code = problem[:code]
|
|
47
|
+
message = [title, code && "(#{code})"].compact.join(" ")
|
|
48
|
+
message = "HTTP #{status}" if message.empty?
|
|
49
|
+
klass.new(message, status: status, code: code, title: title, errors: problem[:errors],
|
|
50
|
+
retry_after: retry_after, body: body)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "openssl"
|
|
3
|
+
|
|
4
|
+
module Superslice
|
|
5
|
+
module Webhooks
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# Checks the Superslice-Signature header against the raw request body and
|
|
9
|
+
# returns the parsed event (a Hash with symbol keys). Raises SignatureError
|
|
10
|
+
# when the signature is missing, wrong, or older than +tolerance_seconds+.
|
|
11
|
+
# Pass the raw bytes, not re-encoded JSON.
|
|
12
|
+
def verify(secret, signature_header, raw_body, tolerance_seconds: 300, now: Time.now.to_i)
|
|
13
|
+
t = nil
|
|
14
|
+
signatures = []
|
|
15
|
+
signature_header.to_s.split(",").each do |part|
|
|
16
|
+
key, value = part.strip.split("=", 2)
|
|
17
|
+
t = Integer(value, 10) if key == "t" && value =~ /\A\d+\z/
|
|
18
|
+
signatures << value if key == "v1" && value && !value.empty?
|
|
19
|
+
end
|
|
20
|
+
raise SignatureError, "missing or malformed Superslice-Signature header" if t.nil? || signatures.empty?
|
|
21
|
+
raise SignatureError, "timestamp outside the tolerance" if (now - t).abs > tolerance_seconds
|
|
22
|
+
|
|
23
|
+
expected = OpenSSL::HMAC.hexdigest("SHA256", secret.to_s, "#{t}.".b + raw_body.to_s.b)
|
|
24
|
+
ok = signatures.any? do |s|
|
|
25
|
+
s.bytesize == expected.bytesize && OpenSSL.fixed_length_secure_compare(s, expected)
|
|
26
|
+
end
|
|
27
|
+
raise SignatureError, "no matching signature" unless ok
|
|
28
|
+
|
|
29
|
+
JSON.parse(raw_body.to_s.dup.force_encoding("UTF-8"), symbolize_names: true)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
class << self
|
|
33
|
+
alias_method :unwrap, :verify
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
data/lib/superslice.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: superslice
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Superslice
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Slice 3D models on the real Bambu Studio, Creality Print, ElegooSlicer
|
|
14
|
+
and PrusaSlicer engines and get print time, filament use and a price back.
|
|
15
|
+
email:
|
|
16
|
+
- hello@superslice.co
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/superslice.rb
|
|
24
|
+
- lib/superslice/client.rb
|
|
25
|
+
- lib/superslice/errors.rb
|
|
26
|
+
- lib/superslice/version.rb
|
|
27
|
+
- lib/superslice/webhooks.rb
|
|
28
|
+
homepage: https://docs.superslice.co
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
source_code_uri: https://github.com/superslice-co/superslice-ruby
|
|
33
|
+
documentation_uri: https://docs.superslice.co
|
|
34
|
+
rubygems_mfa_required: 'true'
|
|
35
|
+
post_install_message:
|
|
36
|
+
rdoc_options: []
|
|
37
|
+
require_paths:
|
|
38
|
+
- lib
|
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
40
|
+
requirements:
|
|
41
|
+
- - ">="
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '3.0'
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
requirements: []
|
|
50
|
+
rubygems_version: 3.5.22
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 4
|
|
53
|
+
summary: Official Ruby library for the Superslice API
|
|
54
|
+
test_files: []
|