Skip to content

Commit ff5cfd3

Browse files
authored
Merge pull request #6393 from manyfold3d/print-with-moonraker
Add Moonraker API (Klipper) print service
2 parents 537705f + c8d419c commit ff5cfd3

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,5 @@ gem "marcel", "~> 1.2"
237237
gem "naturally", "~> 2.3"
238238

239239
gem "rouge", "~> 5.0"
240+
241+
gem "faraday-multipart", "~> 1.2"

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ GEM
276276
logger
277277
faraday-follow_redirects (0.5.0)
278278
faraday (>= 1, < 3)
279+
faraday-multipart (1.2.0)
280+
multipart-post (~> 2.0)
279281
faraday-net_http (3.4.2)
280282
net-http (~> 0.5)
281283
fasp_client (0.6.0)
@@ -511,6 +513,7 @@ GEM
511513
mittsu (~> 0.4)
512514
msgpack (1.8.1)
513515
multi_json (1.21.1)
516+
multipart-post (2.4.1)
514517
mutex_m (0.3.0)
515518
mysql2 (0.5.6)
516519
nanoid (2.0.0)
@@ -1018,6 +1021,7 @@ DEPENDENCIES
10181021
erb_lint (~> 0.9)
10191022
factory_bot
10201023
faker (~> 3.8)
1024+
faraday-multipart (~> 1.2)
10211025
fasp_client (~> 0.6)
10221026
federails!
10231027
federails-moderation (~> 0.4)
@@ -1204,6 +1208,7 @@ CHECKSUMS
12041208
faker (3.8.0) sha256=c147b308df73a90f27a4fc84f18d4c22ef0ad9c2a64b2b61c86fd0ca71753efc
12051209
faraday (2.14.2) sha256=73ccb9994a9e8648f010e32eca2ae82e41c57860aa10932cda29418b9e0223ad
12061210
faraday-follow_redirects (0.5.0) sha256=5cde93c894b30943a5d2b93c2fe9284216a6b756f7af406a1e55f211d97d10ad
1211+
faraday-multipart (1.2.0) sha256=7d89a949693714176f612323ca13746a2ded204031a6ba528adee788694ef757
12071212
faraday-net_http (3.4.2) sha256=f147758260d3526939bf57ecf911682f94926a3666502e24c69992765875906c
12081213
fasp_client (0.6.0) sha256=c1fba106ec3608bc9e9d83b9384645e5339ad637169b696d28a1823164352775
12091214
federails (0.8.0)
@@ -1294,6 +1299,7 @@ CHECKSUMS
12941299
mittsu-mesh_analysis (0.1.0) sha256=13550c35e56bcd10cbe8284fe1b3cf3a43f7bb7133f47995e0c8a8b5d2a9cd50
12951300
msgpack (1.8.1) sha256=3fef787cd3965fd119c08a22724a56a93ca25008c3421fc15039f603a8b7c86c
12961301
multi_json (1.21.1) sha256=e6126a31808e3b4d19f483c775ceac34df190dffa62adfb63a165ee14ba68080
1302+
multipart-post (2.4.1) sha256=9872d03a8e552020ca096adadbf5e3cb1cd1cdd6acd3c161136b8a5737cdb4a8
12971303
mutex_m (0.3.0) sha256=cfcb04ac16b69c4813777022fdceda24e9f798e48092a2b817eb4c0a782b0751
12981304
mysql2 (0.5.6) sha256=70f447d45d6b3cc16b00f7dd30366f708a81b4093a35d026ff7135d778d8da33
12991305
nanoid (2.0.0) sha256=78a5c93c1595981fb37712a166d7ea1ef15a679553e5d3c72337f231febd8bfc
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "faraday"
2+
require "faraday/multipart"
3+
4+
class Print::MoonrakerService
5+
# i18n-tasks-use t("print_hosts.protocols.moonraker")
6+
PROTOCOL = "moonraker".freeze
7+
8+
INPUT_TYPES = [Mime[:gcode]].freeze
9+
10+
def initialize(print_host:)
11+
@print_host = print_host
12+
end
13+
14+
def ok?
15+
response = connection.get(info_uri, {}, headers)
16+
response.success?
17+
rescue
18+
false
19+
end
20+
21+
def upload(file:, start_print: true)
22+
raise ArgumentError unless file.mime_type.to_sym == :gcode
23+
connection.post(upload_uri, payload(file: file, start_print: start_print), headers)
24+
end
25+
26+
private
27+
28+
def connection
29+
Faraday.new do
30+
it.request :multipart
31+
end
32+
end
33+
34+
def payload(file:, start_print: true)
35+
{
36+
print: start_print ? "true" : "false",
37+
file: Faraday::Multipart::FilePart.new(
38+
file.attachment.open,
39+
file.mime_type.to_s,
40+
file.filename
41+
)
42+
}
43+
end
44+
45+
def info_uri
46+
"#{@print_host.endpoint}/server/info"
47+
end
48+
49+
def upload_uri
50+
"#{@print_host.endpoint}/server/files/upload"
51+
end
52+
53+
def headers
54+
{
55+
"X-Api-Key" => @print_host.credentials
56+
}.compact_blank
57+
end
58+
end

0 commit comments

Comments
 (0)