octoprint 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/CHANGELOG.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +336 -0
- data/lib/octoprint/access/group.rb +88 -0
- data/lib/octoprint/access/groups.rb +166 -0
- data/lib/octoprint/access/login_response.rb +63 -0
- data/lib/octoprint/access/permission.rb +71 -0
- data/lib/octoprint/access/permissions.rb +29 -0
- data/lib/octoprint/access/user.rb +95 -0
- data/lib/octoprint/access/users.rb +347 -0
- data/lib/octoprint/auto_initializable.rb +219 -0
- data/lib/octoprint/base_resource.rb +139 -0
- data/lib/octoprint/client.rb +151 -0
- data/lib/octoprint/connection/options.rb +44 -0
- data/lib/octoprint/connection/settings.rb +35 -0
- data/lib/octoprint/connection.rb +74 -0
- data/lib/octoprint/deserializable.rb +483 -0
- data/lib/octoprint/exceptions.rb +30 -0
- data/lib/octoprint/files/file.rb +55 -0
- data/lib/octoprint/files/folder.rb +29 -0
- data/lib/octoprint/files/operation_result.rb +37 -0
- data/lib/octoprint/files/refs.rb +19 -0
- data/lib/octoprint/files.rb +238 -0
- data/lib/octoprint/job/information.rb +42 -0
- data/lib/octoprint/job/progress.rb +46 -0
- data/lib/octoprint/job.rb +140 -0
- data/lib/octoprint/languages/language_pack.rb +31 -0
- data/lib/octoprint/languages.rb +102 -0
- data/lib/octoprint/location.rb +23 -0
- data/lib/octoprint/logs/log_file.rb +59 -0
- data/lib/octoprint/logs/references.rb +28 -0
- data/lib/octoprint/logs.rb +98 -0
- data/lib/octoprint/printer/bed.rb +32 -0
- data/lib/octoprint/printer/chamber.rb +29 -0
- data/lib/octoprint/printer/command.rb +129 -0
- data/lib/octoprint/printer/custom_control/input.rb +45 -0
- data/lib/octoprint/printer/custom_control.rb +119 -0
- data/lib/octoprint/printer/error_information.rb +104 -0
- data/lib/octoprint/printer/heater.rb +113 -0
- data/lib/octoprint/printer/history_query.rb +25 -0
- data/lib/octoprint/printer/printhead.rb +105 -0
- data/lib/octoprint/printer/sd.rb +97 -0
- data/lib/octoprint/printer/state.rb +41 -0
- data/lib/octoprint/printer/state_flags.rb +70 -0
- data/lib/octoprint/printer/temperature_attributes.rb +70 -0
- data/lib/octoprint/printer/temperature_data.rb +53 -0
- data/lib/octoprint/printer/temperature_data_point.rb +73 -0
- data/lib/octoprint/printer/temperature_reading.rb +47 -0
- data/lib/octoprint/printer/temperature_state.rb +67 -0
- data/lib/octoprint/printer/tool.rb +154 -0
- data/lib/octoprint/printer.rb +126 -0
- data/lib/octoprint/printer_profiles/printer_profile.rb +40 -0
- data/lib/octoprint/printer_profiles.rb +158 -0
- data/lib/octoprint/server_information.rb +51 -0
- data/lib/octoprint/server_version.rb +52 -0
- data/lib/octoprint/settings/webcam.rb +135 -0
- data/lib/octoprint/settings.rb +133 -0
- data/lib/octoprint/slicing/profile.rb +65 -0
- data/lib/octoprint/slicing/slicer.rb +83 -0
- data/lib/octoprint/slicing.rb +227 -0
- data/lib/octoprint/system_commands/command.rb +41 -0
- data/lib/octoprint/system_commands.rb +110 -0
- data/lib/octoprint/timelapse/config.rb +68 -0
- data/lib/octoprint/timelapse/file_attributes.rb +50 -0
- data/lib/octoprint/timelapse/rendered.rb +51 -0
- data/lib/octoprint/timelapse/unrendered.rb +43 -0
- data/lib/octoprint/timelapse.rb +221 -0
- data/lib/octoprint/types.rb +140 -0
- data/lib/octoprint/util.rb +151 -0
- data/lib/octoprint/version.rb +7 -0
- data/lib/octoprint/wizard.rb +57 -0
- data/lib/octoprint.rb +57 -0
- data/rbi/octoprint/access/group.rbi +46 -0
- data/rbi/octoprint/access/login_response.rbi +22 -0
- data/rbi/octoprint/access/permission.rbi +34 -0
- data/rbi/octoprint/access/user.rbi +43 -0
- data/rbi/octoprint/connection/options.rbi +28 -0
- data/rbi/octoprint/connection/settings.rbi +19 -0
- data/rbi/octoprint/connection.rbi +13 -0
- data/rbi/octoprint/files/file.rbi +61 -0
- data/rbi/octoprint/files/folder.rbi +22 -0
- data/rbi/octoprint/files/operation_result.rbi +22 -0
- data/rbi/octoprint/files/refs.rbi +16 -0
- data/rbi/octoprint/job/information.rbi +22 -0
- data/rbi/octoprint/job/progress.rbi +25 -0
- data/rbi/octoprint/job.rbi +22 -0
- data/rbi/octoprint/languages/language_pack.rbi +19 -0
- data/rbi/octoprint/languages.rbi +13 -0
- data/rbi/octoprint/logs/log_file.rbi +19 -0
- data/rbi/octoprint/logs/references.rbi +13 -0
- data/rbi/octoprint/logs.rbi +13 -0
- data/rbi/octoprint/printer/bed.rbi +22 -0
- data/rbi/octoprint/printer/chamber.rbi +22 -0
- data/rbi/octoprint/printer/custom_control/input.rbi +22 -0
- data/rbi/octoprint/printer/custom_control.rbi +67 -0
- data/rbi/octoprint/printer/error_information.rbi +25 -0
- data/rbi/octoprint/printer/sd.rbi +13 -0
- data/rbi/octoprint/printer/state.rbi +19 -0
- data/rbi/octoprint/printer/state_flags.rbi +43 -0
- data/rbi/octoprint/printer/temperature_data.rbi +19 -0
- data/rbi/octoprint/printer/temperature_data_point.rbi +22 -0
- data/rbi/octoprint/printer/temperature_reading.rbi +19 -0
- data/rbi/octoprint/printer/temperature_state.rbi +22 -0
- data/rbi/octoprint/printer/tool.rbi +22 -0
- data/rbi/octoprint/printer.rbi +19 -0
- data/rbi/octoprint/printer_profiles/printer_profile.rbi +46 -0
- data/rbi/octoprint/printer_profiles.rbi +13 -0
- data/rbi/octoprint/server_information.rbi +16 -0
- data/rbi/octoprint/server_version.rbi +19 -0
- data/rbi/octoprint/settings/webcam.rbi +34 -0
- data/rbi/octoprint/slicing/profile.rbi +28 -0
- data/rbi/octoprint/slicing/slicer.rbi +31 -0
- data/rbi/octoprint/system_commands/command.rbi +25 -0
- data/rbi/octoprint/system_commands.rbi +16 -0
- data/rbi/octoprint/timelapse/config.rbi +31 -0
- data/rbi/octoprint/timelapse/rendered.rbi +31 -0
- data/rbi/octoprint/timelapse/unrendered.rbi +34 -0
- data/rbi/octoprint/timelapse.rbi +22 -0
- data/rbi/octoprint/util.rbi +19 -0
- data/rbi/octoprint.rbi +1848 -0
- metadata +264 -0
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module Octoprint
|
|
5
|
+
# AutoInitializable provides automatic initialization and typed attribute generation.
|
|
6
|
+
#
|
|
7
|
+
# This module eliminates repetitive initialization code by automatically generating:
|
|
8
|
+
# - Typed instance variables from keyword arguments
|
|
9
|
+
# - Sorbet-typed attr_reader methods
|
|
10
|
+
# - Type conversion for nested objects
|
|
11
|
+
#
|
|
12
|
+
# @example Basic usage
|
|
13
|
+
# class User
|
|
14
|
+
# include AutoInitializable
|
|
15
|
+
#
|
|
16
|
+
# auto_attr :name, type: String, nilable: false
|
|
17
|
+
# auto_attr :email, type: String
|
|
18
|
+
# auto_attr :age, type: Integer
|
|
19
|
+
#
|
|
20
|
+
# auto_initialize!
|
|
21
|
+
# end
|
|
22
|
+
#
|
|
23
|
+
# user = User.new(name: "John", email: "john@example.com", age: 30)
|
|
24
|
+
# user.name # => "John" (typed as String)
|
|
25
|
+
# user.email # => "john@example.com" (typed as T.nilable(String))
|
|
26
|
+
#
|
|
27
|
+
# @example With custom classes and arrays
|
|
28
|
+
# class Profile
|
|
29
|
+
# include AutoInitializable
|
|
30
|
+
#
|
|
31
|
+
# auto_attr :user, type: User
|
|
32
|
+
# auto_attr :tags, type: String, array: true
|
|
33
|
+
# auto_attr :metadata, type: Hash
|
|
34
|
+
#
|
|
35
|
+
# auto_initialize!
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
38
|
+
# profile = Profile.new(
|
|
39
|
+
# user: { name: "John", email: "john@example.com" }, # Auto-converted to User
|
|
40
|
+
# tags: ["admin", "developer"], # Typed as T.nilable(T::Array[String])
|
|
41
|
+
# metadata: { created_at: Time.now } # Preserved as Hash
|
|
42
|
+
# )
|
|
43
|
+
#
|
|
44
|
+
# @example Type conversion behavior
|
|
45
|
+
# # Basic Ruby types (Hash, Array, String, Integer) are preserved as-is
|
|
46
|
+
# auto_attr :data, type: Hash # No conversion, preserves original hash
|
|
47
|
+
#
|
|
48
|
+
# # Custom classes are auto-converted from hash data
|
|
49
|
+
# auto_attr :user, type: User # Calls User.new(**hash_data)
|
|
50
|
+
#
|
|
51
|
+
# # Arrays of custom classes
|
|
52
|
+
# auto_attr :users, type: User, array: true # Maps each hash to User.new(**hash)
|
|
53
|
+
module AutoInitializable
|
|
54
|
+
extend T::Helpers
|
|
55
|
+
|
|
56
|
+
# Class methods added when AutoInitializable is included.
|
|
57
|
+
#
|
|
58
|
+
# These methods provide the DSL for declaring auto-initialized attributes
|
|
59
|
+
# and generating the corresponding initialization logic.
|
|
60
|
+
module ClassMethods
|
|
61
|
+
# Declares an attribute that will be automatically initialized and typed.
|
|
62
|
+
#
|
|
63
|
+
# @param name [Symbol] The name of the attribute
|
|
64
|
+
# @param type [Class, T.untyped] The type of the attribute (String, Integer, Hash, custom classes, etc.)
|
|
65
|
+
# @param from [Symbol, nil] The source key in initialization hash (defaults to name)
|
|
66
|
+
# @param array [Boolean] Whether this attribute is an array of the specified type
|
|
67
|
+
# @param nilable [Boolean] Whether this attribute can be nil
|
|
68
|
+
#
|
|
69
|
+
# @example Basic attributes
|
|
70
|
+
# auto_attr :name, type: String, nilable: false
|
|
71
|
+
# auto_attr :email, type: String
|
|
72
|
+
# auto_attr :age, type: Integer
|
|
73
|
+
#
|
|
74
|
+
# @example Arrays
|
|
75
|
+
# auto_attr :tags, type: String, array: true
|
|
76
|
+
# auto_attr :users, type: User, array: true
|
|
77
|
+
#
|
|
78
|
+
# @example Custom key mapping
|
|
79
|
+
# auto_attr :display_name, type: String, from: :display
|
|
80
|
+
#
|
|
81
|
+
# @example Complex types
|
|
82
|
+
# auto_attr :metadata, type: Hash
|
|
83
|
+
# auto_attr :profile, type: Profile
|
|
84
|
+
#
|
|
85
|
+
# @note Basic Ruby types (Hash, Array, String, Integer, Float) are preserved without conversion.
|
|
86
|
+
# Custom classes will be instantiated from hash data using Class.new(**hash).
|
|
87
|
+
#: (
|
|
88
|
+
#| Symbol name,
|
|
89
|
+
#| ?type: untyped,
|
|
90
|
+
#| ?from: Symbol?,
|
|
91
|
+
#| ?array: bool,
|
|
92
|
+
#| ?nilable: bool
|
|
93
|
+
#| ) -> void
|
|
94
|
+
def auto_attr(name, type: T.untyped, from: nil, array: false, nilable: true)
|
|
95
|
+
@auto_attrs ||= {}
|
|
96
|
+
@auto_attrs[name] = {
|
|
97
|
+
type: type,
|
|
98
|
+
from: from || name,
|
|
99
|
+
array: array,
|
|
100
|
+
nilable: nilable
|
|
101
|
+
}
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
# Generates the initialize method and typed attr_readers based on declared auto_attr calls.
|
|
105
|
+
#
|
|
106
|
+
# This method must be called after all auto_attr declarations to generate the actual
|
|
107
|
+
# initialization code and Sorbet-typed attribute readers.
|
|
108
|
+
#
|
|
109
|
+
# @example
|
|
110
|
+
# class User
|
|
111
|
+
# include AutoInitializable
|
|
112
|
+
#
|
|
113
|
+
# auto_attr :name, type: String
|
|
114
|
+
# auto_attr :age, type: Integer
|
|
115
|
+
#
|
|
116
|
+
# auto_initialize! # Must be called after auto_attr declarations
|
|
117
|
+
# end
|
|
118
|
+
#
|
|
119
|
+
# @note This method generates:
|
|
120
|
+
# - An initialize method that accepts keyword arguments
|
|
121
|
+
# - Type conversion for nested objects
|
|
122
|
+
# - Sorbet-typed attr_reader methods
|
|
123
|
+
# - Automatic validation that required attributes are provided
|
|
124
|
+
#: -> void
|
|
125
|
+
def auto_initialize!
|
|
126
|
+
attrs = @auto_attrs || {}
|
|
127
|
+
klass = self
|
|
128
|
+
|
|
129
|
+
T.unsafe(self).define_method :initialize do |**kwargs|
|
|
130
|
+
attrs.each do |name, config|
|
|
131
|
+
source_key = config[:from]
|
|
132
|
+
value = kwargs[source_key]
|
|
133
|
+
|
|
134
|
+
# Skip if nil and nilable
|
|
135
|
+
if value.nil?
|
|
136
|
+
T.unsafe(self).instance_variable_set("@#{name}", nil)
|
|
137
|
+
next
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# Handle type conversion
|
|
141
|
+
converted_value = if config[:type]
|
|
142
|
+
if config[:array] && value.is_a?(Array)
|
|
143
|
+
# Handle array of objects
|
|
144
|
+
value.map do |item|
|
|
145
|
+
T.unsafe(klass).send(:convert_value, item, config[:type])
|
|
146
|
+
end
|
|
147
|
+
else
|
|
148
|
+
T.unsafe(klass).send(:convert_value, value, config[:type])
|
|
149
|
+
end
|
|
150
|
+
else
|
|
151
|
+
value
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
T.unsafe(self).instance_variable_set("@#{name}", converted_value)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Call parent initializer if it exists and has an initialize method
|
|
158
|
+
return unless T.unsafe(self).class.superclass.method_defined?(:initialize)
|
|
159
|
+
|
|
160
|
+
super()
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Create simple attr_readers
|
|
164
|
+
# Type signatures are provided by generated RBI files
|
|
165
|
+
attrs.each_key do |name|
|
|
166
|
+
T.unsafe(self).attr_reader name
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Returns the hash of declared attributes and their configuration.
|
|
171
|
+
#
|
|
172
|
+
# @return [Hash<Symbol, Hash>] Hash mapping attribute names to their configuration
|
|
173
|
+
#
|
|
174
|
+
# @example
|
|
175
|
+
# User.auto_attrs
|
|
176
|
+
# # => {
|
|
177
|
+
# # name: { type: String, from: :name, array: false, nilable: false },
|
|
178
|
+
# # email: { type: String, from: :email, array: false, nilable: true }
|
|
179
|
+
# # }
|
|
180
|
+
#: -> Hash[Symbol, untyped]
|
|
181
|
+
def auto_attrs
|
|
182
|
+
@auto_attrs || {}
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
private
|
|
186
|
+
|
|
187
|
+
# Converts a value to the specified type if appropriate.
|
|
188
|
+
#
|
|
189
|
+
# This method handles type conversion for nested objects during initialization.
|
|
190
|
+
# Basic Ruby types (Hash, Array, String, etc.) are preserved as-is to avoid
|
|
191
|
+
# unwanted conversions. Custom classes are instantiated from hash data.
|
|
192
|
+
#
|
|
193
|
+
# @param value [Object] The value to potentially convert
|
|
194
|
+
# @param type [Class] The target type for conversion
|
|
195
|
+
# @return [Object] The converted value or original value if no conversion needed
|
|
196
|
+
#
|
|
197
|
+
# @example
|
|
198
|
+
# convert_value({ name: "John" }, User) # => User.new(name: "John")
|
|
199
|
+
# convert_value({ key: "value" }, Hash) # => { key: "value" } (unchanged)
|
|
200
|
+
# convert_value("text", String) # => "text" (unchanged)
|
|
201
|
+
#: (untyped value, untyped type) -> untyped
|
|
202
|
+
def convert_value(value, type)
|
|
203
|
+
return value unless value.is_a?(Hash)
|
|
204
|
+
|
|
205
|
+
# Don't try to convert basic Ruby types like Hash, Array, String, etc.
|
|
206
|
+
return value if [Hash, Array, String, Integer, Float, TrueClass, FalseClass].include?(type)
|
|
207
|
+
|
|
208
|
+
# Check if type is a class that responds to new (for our custom classes)
|
|
209
|
+
if type.is_a?(Class) && type.respond_to?(:new)
|
|
210
|
+
type.new(**value)
|
|
211
|
+
else
|
|
212
|
+
value
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
mixes_in_class_methods(ClassMethods)
|
|
218
|
+
end
|
|
219
|
+
end
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module Octoprint
|
|
5
|
+
# Base class for all API resources.
|
|
6
|
+
class BaseResource
|
|
7
|
+
class << self
|
|
8
|
+
# Defines the path for the resource's endpoint
|
|
9
|
+
#
|
|
10
|
+
# @param [String] path The API's endpoint path
|
|
11
|
+
def resource_path(path)
|
|
12
|
+
@path = path
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Gets a single resource and instantiates the object.
|
|
16
|
+
#
|
|
17
|
+
# @return [BaseResource]
|
|
18
|
+
def fetch_resource(path = nil, deserialize: true, options: {})
|
|
19
|
+
path = [@path, path].compact.join("/")
|
|
20
|
+
path += "?#{Faraday::Utils.build_query(options)}" if options.any?
|
|
21
|
+
response = client.request(path, http_method: :get)
|
|
22
|
+
return deserialize(response) if deserialize
|
|
23
|
+
|
|
24
|
+
response
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Sends a POST request to the resource's endpoint.
|
|
28
|
+
#
|
|
29
|
+
# @param [String] path the path of the post request
|
|
30
|
+
# @param [Hash] params parameters to the request
|
|
31
|
+
# @param [Hash] options options
|
|
32
|
+
# @return [Object]
|
|
33
|
+
#: (
|
|
34
|
+
#| ?path: String,
|
|
35
|
+
#| ?params: Hash[Symbol, untyped],
|
|
36
|
+
#| ?headers: Hash[Symbol, untyped],
|
|
37
|
+
#| ?options: Hash[Symbol, untyped]
|
|
38
|
+
#| ) -> untyped
|
|
39
|
+
def post(path: @path, params: {}, headers: {}, options: {})
|
|
40
|
+
client.request(
|
|
41
|
+
path,
|
|
42
|
+
http_method: :post,
|
|
43
|
+
body: params,
|
|
44
|
+
headers: headers,
|
|
45
|
+
options: options
|
|
46
|
+
)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Sends a PUT request to the resource's endpoint.
|
|
50
|
+
#
|
|
51
|
+
# @param [String] path the path of the put request
|
|
52
|
+
# @param [Hash] params parameters to the request
|
|
53
|
+
# @param [Hash] headers headers to the request
|
|
54
|
+
# @param [Hash] options options
|
|
55
|
+
# @return [Object]
|
|
56
|
+
#: (
|
|
57
|
+
#| ?path: String,
|
|
58
|
+
#| ?params: Hash[Symbol, untyped],
|
|
59
|
+
#| ?headers: Hash[Symbol, untyped],
|
|
60
|
+
#| ?options: Hash[Symbol, untyped]
|
|
61
|
+
#| ) -> untyped
|
|
62
|
+
def put(path: @path, params: {}, headers: {}, options: {})
|
|
63
|
+
client.request(
|
|
64
|
+
path,
|
|
65
|
+
http_method: :put,
|
|
66
|
+
body: params,
|
|
67
|
+
headers: headers,
|
|
68
|
+
options: options
|
|
69
|
+
)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Sends a PATCH request to the resource's endpoint.
|
|
73
|
+
#
|
|
74
|
+
# @param [String] path the path of the patch request
|
|
75
|
+
# @param [Hash] params parameters to the request
|
|
76
|
+
# @param [Hash] headers headers to the request
|
|
77
|
+
# @param [Hash] options options
|
|
78
|
+
# @return [Object]
|
|
79
|
+
#: (
|
|
80
|
+
#| ?path: String,
|
|
81
|
+
#| ?params: Hash[Symbol, untyped],
|
|
82
|
+
#| ?headers: Hash[Symbol, untyped],
|
|
83
|
+
#| ?options: Hash[Symbol, untyped]
|
|
84
|
+
#| ) -> untyped
|
|
85
|
+
def patch(path: @path, params: {}, headers: {}, options: {})
|
|
86
|
+
client.request(
|
|
87
|
+
path,
|
|
88
|
+
http_method: :patch,
|
|
89
|
+
body: params,
|
|
90
|
+
headers: headers,
|
|
91
|
+
options: options
|
|
92
|
+
)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Sends a DELETE request to the resource's endpoint.
|
|
96
|
+
#
|
|
97
|
+
# @param [String] path the path of the delete request
|
|
98
|
+
# @param [Hash] params parameters to the request
|
|
99
|
+
# @param [Hash] headers headers to the request
|
|
100
|
+
# @param [Hash] options options
|
|
101
|
+
# @return [Object]
|
|
102
|
+
#: (
|
|
103
|
+
#| ?path: String,
|
|
104
|
+
#| ?params: Hash[Symbol, untyped],
|
|
105
|
+
#| ?headers: Hash[Symbol, untyped],
|
|
106
|
+
#| ?options: Hash[Symbol, untyped]
|
|
107
|
+
#| ) -> untyped
|
|
108
|
+
def delete(path: @path, params: {}, headers: {}, options: {})
|
|
109
|
+
client.request(
|
|
110
|
+
path,
|
|
111
|
+
http_method: :delete,
|
|
112
|
+
body: params.empty? ? nil : params,
|
|
113
|
+
headers: headers,
|
|
114
|
+
options: options
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Instantiates an object from a hash. Can be overridden by child classes
|
|
119
|
+
#
|
|
120
|
+
# @param [Hash] attrs the object's attributes
|
|
121
|
+
# @return [BaseResource]
|
|
122
|
+
def deserialize(attrs)
|
|
123
|
+
new(**attrs)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
private
|
|
127
|
+
|
|
128
|
+
#: -> Client
|
|
129
|
+
def client
|
|
130
|
+
client = Octoprint.client
|
|
131
|
+
raise "No client configured" unless client
|
|
132
|
+
|
|
133
|
+
client
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def initialize(**attrs); end
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "faraday"
|
|
5
|
+
require "faraday/multipart"
|
|
6
|
+
|
|
7
|
+
module Octoprint
|
|
8
|
+
# The API client object.
|
|
9
|
+
#
|
|
10
|
+
# @example Configure a client with custom timeouts
|
|
11
|
+
# client = Octoprint::Client.new(host: "http://octopi.local", api_key: "key", open_timeout: 1, timeout: 5)
|
|
12
|
+
#
|
|
13
|
+
# @example Allow more time for large file uploads
|
|
14
|
+
# client = Octoprint::Client.new(host: "http://octopi.local", api_key: "key", timeout: 120)
|
|
15
|
+
#
|
|
16
|
+
# @attr [String] host
|
|
17
|
+
# @attr [String] api_key
|
|
18
|
+
# @attr [Numeric, nil] open_timeout Seconds to wait for the connection to open. nil falls back to `timeout`
|
|
19
|
+
# @attr [Numeric, nil] timeout Seconds to wait for a read or a write. nil: adapter default (60s)
|
|
20
|
+
# @attr [Client] client
|
|
21
|
+
class Client
|
|
22
|
+
# Default number of seconds to wait for the connection to open
|
|
23
|
+
DEFAULT_OPEN_TIMEOUT = 2
|
|
24
|
+
# Default number of seconds to wait for a read or a write
|
|
25
|
+
DEFAULT_TIMEOUT = 3
|
|
26
|
+
|
|
27
|
+
attr_reader :host, :api_key, :open_timeout, :timeout, :client
|
|
28
|
+
|
|
29
|
+
# @param host [String, URI] Server's host address
|
|
30
|
+
# @param api_key [String] Server's API key
|
|
31
|
+
# @param open_timeout [Numeric, nil] Seconds to wait for the connection to open. nil falls back to `timeout`
|
|
32
|
+
# @param timeout [Numeric, nil] Seconds to wait for a read or a write. nil: adapter default (60s)
|
|
33
|
+
# @raise [Exceptions::MissingCredentialsError] if host or api_key is missing
|
|
34
|
+
#: (
|
|
35
|
+
#| host: (String | URI::Generic)?,
|
|
36
|
+
#| api_key: String?,
|
|
37
|
+
#| ?open_timeout: Numeric?,
|
|
38
|
+
#| ?timeout: Numeric?
|
|
39
|
+
#| ) -> void
|
|
40
|
+
def initialize(host:, api_key:, open_timeout: DEFAULT_OPEN_TIMEOUT, timeout: DEFAULT_TIMEOUT)
|
|
41
|
+
raise Exceptions::MissingCredentialsError if host.nil? || api_key.nil?
|
|
42
|
+
|
|
43
|
+
@host = host
|
|
44
|
+
@api_key = api_key
|
|
45
|
+
@open_timeout = open_timeout
|
|
46
|
+
@timeout = timeout
|
|
47
|
+
@client = new_client
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Instantiates an object from a hash. Can be overridden by child classes
|
|
51
|
+
#
|
|
52
|
+
# @param [String] path the path of the request
|
|
53
|
+
# @param [Symbol|String] http_method the http method of the request
|
|
54
|
+
# @param [Hash] body the body of the request
|
|
55
|
+
# @param [Hash] options options
|
|
56
|
+
# @return [Hash]
|
|
57
|
+
# @raise [Exceptions::TimeoutError] if the server doesn't respond within the configured timeouts
|
|
58
|
+
# @raise [Exceptions::ConnectionError] if the server can't be reached
|
|
59
|
+
def request(path, options_or_http_method = nil, body: nil, headers: {}, options: {}, http_method: :get)
|
|
60
|
+
# Handle both keyword arguments and hash format for backward compatibility
|
|
61
|
+
if options_or_http_method.is_a?(Hash)
|
|
62
|
+
# Called with a hash as second parameter (legacy format from tests)
|
|
63
|
+
options_hash = options_or_http_method
|
|
64
|
+
http_method = options_hash[:http_method] || http_method
|
|
65
|
+
body = options_hash[:params] || options_hash[:body] || body
|
|
66
|
+
headers = options_hash[:headers] || headers
|
|
67
|
+
options = options_hash[:options] || options
|
|
68
|
+
else
|
|
69
|
+
# Called with keyword arguments (new format)
|
|
70
|
+
http_method = options_or_http_method || http_method
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
response = request_with_client(http_method, path, body, headers,
|
|
74
|
+
force_multipart: options.fetch(:force_multipart, false))
|
|
75
|
+
|
|
76
|
+
return true if response.status == 204 # No content
|
|
77
|
+
|
|
78
|
+
process_error(response) if response.status >= 400
|
|
79
|
+
|
|
80
|
+
parse_response(response)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Every request inside the block will be executed as this client without affecting the gem's initial configuration
|
|
84
|
+
# @return [Client]
|
|
85
|
+
def use
|
|
86
|
+
config_client = Octoprint.client
|
|
87
|
+
Octoprint.client = self
|
|
88
|
+
yield
|
|
89
|
+
ensure
|
|
90
|
+
Octoprint.client = config_client
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
private
|
|
94
|
+
|
|
95
|
+
def process_error(response)
|
|
96
|
+
errors = {
|
|
97
|
+
400 => Exceptions::BadRequestError,
|
|
98
|
+
403 => Exceptions::AuthenticationError,
|
|
99
|
+
404 => Exceptions::NotFoundError,
|
|
100
|
+
409 => Exceptions::ConflictError,
|
|
101
|
+
415 => Exceptions::UnsupportedMediaTypeError,
|
|
102
|
+
500 => Exceptions::InternalServerError
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
error_class = errors[response.status] || Exceptions::UnknownError
|
|
106
|
+
|
|
107
|
+
raise(error_class, "[#{response.status}] " + parse_response(response).fetch(:error))
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def parse_response(response)
|
|
111
|
+
parsed_json = JSON.parse(response.body)
|
|
112
|
+
transform_keys_recursively(parsed_json)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def transform_keys_recursively(obj)
|
|
116
|
+
case obj
|
|
117
|
+
when Hash
|
|
118
|
+
obj.deep_transform_keys { |key| key.underscore.to_sym }
|
|
119
|
+
when Array
|
|
120
|
+
obj.map { |item| transform_keys_recursively(item) }
|
|
121
|
+
else
|
|
122
|
+
obj
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def request_with_client(http_method, path, body, headers, force_multipart: false)
|
|
127
|
+
if force_multipart || body&.any? { |_key, value| value.is_a?(Faraday::UploadIO) }
|
|
128
|
+
file_client = new_client(multipart: true)
|
|
129
|
+
file_client.public_send(http_method, path, body, headers)
|
|
130
|
+
else
|
|
131
|
+
client.public_send(http_method, path, body&.to_json, headers)
|
|
132
|
+
end
|
|
133
|
+
rescue Faraday::TimeoutError => e
|
|
134
|
+
raise Exceptions::TimeoutError, e.message
|
|
135
|
+
rescue Faraday::ConnectionFailed, Faraday::SSLError => e
|
|
136
|
+
# Faraday reports open timeouts (Net::OpenTimeout) as connection failures
|
|
137
|
+
error_class = e.wrapped_exception.is_a?(Timeout::Error) ? Exceptions::TimeoutError : Exceptions::ConnectionError
|
|
138
|
+
raise error_class, e.message
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def new_client(multipart: false)
|
|
142
|
+
Faraday.new(host) do |client|
|
|
143
|
+
client.headers["Authorization"] = "Bearer #{api_key}" unless api_key.nil?
|
|
144
|
+
client.headers["Content-Type"] = multipart ? "multipart/form-data" : "application/json"
|
|
145
|
+
client.request :multipart if multipart
|
|
146
|
+
client.options.open_timeout = open_timeout
|
|
147
|
+
client.options.timeout = timeout
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module Octoprint
|
|
5
|
+
class Connection
|
|
6
|
+
# Available connection options of the printer.
|
|
7
|
+
#
|
|
8
|
+
# Octoprint's API doc: https://docs.octoprint.org/en/master/api/connection.html
|
|
9
|
+
#
|
|
10
|
+
# @attr [Array<String>] ports Available ports
|
|
11
|
+
# @attr [Array<Integer>] baudrates Available baudrates
|
|
12
|
+
# @attr [Array<Hash>] printer_profiles
|
|
13
|
+
# @attr [String | nil] port_preference
|
|
14
|
+
# @attr [Integer] baudrate_preference
|
|
15
|
+
# @attr [String] printer_profile_preference
|
|
16
|
+
# @attr [Boolean | nil] autoconnect
|
|
17
|
+
#
|
|
18
|
+
# @example
|
|
19
|
+
# Octoprint.configure(host: 'https://octopi.local/', api_key: 'j98G2nsJq...')
|
|
20
|
+
#
|
|
21
|
+
# options = Octoprint::Connection.get.options
|
|
22
|
+
#
|
|
23
|
+
# options.ports #=> ["/dev/ttyACM0"]
|
|
24
|
+
# options.baudrates #=> [250000, 230400, 115200, 57600, 38400, 19200, 9600]
|
|
25
|
+
# options.printer_profiles #=> [{:id=>"_default", :name=>"Creality CR-10s"}]
|
|
26
|
+
# options.port_preference #=> nil
|
|
27
|
+
# options.baudrate_preference #=> 250000
|
|
28
|
+
# options.printer_profile_preference #=> "_default"
|
|
29
|
+
# options.autoconnect #=> nil
|
|
30
|
+
class Options
|
|
31
|
+
include AutoInitializable
|
|
32
|
+
|
|
33
|
+
auto_attr :ports, type: String, array: true
|
|
34
|
+
auto_attr :baudrates, type: Integer, array: true
|
|
35
|
+
auto_attr :printer_profiles, type: Hash, array: true
|
|
36
|
+
auto_attr :port_preference, type: String
|
|
37
|
+
auto_attr :baudrate_preference, type: Integer
|
|
38
|
+
auto_attr :printer_profile_preference, type: String
|
|
39
|
+
auto_attr :autoconnect, type: T::Boolean
|
|
40
|
+
|
|
41
|
+
auto_initialize!
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module Octoprint
|
|
5
|
+
class Connection
|
|
6
|
+
# Current connection settings of the printer.
|
|
7
|
+
#
|
|
8
|
+
# Octoprint's API doc: https://docs.octoprint.org/en/master/api/connection.html
|
|
9
|
+
#
|
|
10
|
+
# @attr [String] state Connection state
|
|
11
|
+
# @attr [String] port Serial port
|
|
12
|
+
# @attr [Integer] baudrate Serial communication baud rate
|
|
13
|
+
# @attr [String] printer_profile Current printer profile
|
|
14
|
+
#
|
|
15
|
+
# @example
|
|
16
|
+
# Octoprint.configure(host: 'https://octopi.local/', api_key: 'j98G2nsJq...')
|
|
17
|
+
#
|
|
18
|
+
# settings = Octoprint::Connection.get.current
|
|
19
|
+
#
|
|
20
|
+
# settings.state #=> "Operational"
|
|
21
|
+
# settings.port #=> "/dev/ttyACM0"
|
|
22
|
+
# settings.baudrate #=> 250000
|
|
23
|
+
# settings.printer_profile #=> "_default"
|
|
24
|
+
class Settings
|
|
25
|
+
include AutoInitializable
|
|
26
|
+
|
|
27
|
+
auto_attr :state, type: String
|
|
28
|
+
auto_attr :port, type: String
|
|
29
|
+
auto_attr :baudrate, type: Integer
|
|
30
|
+
auto_attr :printer_profile, type: String
|
|
31
|
+
|
|
32
|
+
auto_initialize!
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# typed: true
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
module Octoprint
|
|
5
|
+
# Connection handling with the 3D printer
|
|
6
|
+
#
|
|
7
|
+
# Octoprint's API doc: https://docs.octoprint.org/en/master/api/connection.html
|
|
8
|
+
#
|
|
9
|
+
# @attr [Connection::Settings] current connection state
|
|
10
|
+
# @attr [Connection::Options] options connection options
|
|
11
|
+
#
|
|
12
|
+
# @example
|
|
13
|
+
# Octoprint.configure(host: 'https://octopi.local/', api_key: 'j98G2nsJq...')
|
|
14
|
+
#
|
|
15
|
+
# connection = Octoprint::Connection.get
|
|
16
|
+
# connection.current.state= #=> "Printing"
|
|
17
|
+
# connection.options.ports #=> ["/dev/ttyACM0"]
|
|
18
|
+
class Connection < BaseResource
|
|
19
|
+
include AutoInitializable
|
|
20
|
+
|
|
21
|
+
resource_path("/api/connection")
|
|
22
|
+
|
|
23
|
+
auto_attr :current, type: Settings, from: :current
|
|
24
|
+
auto_attr :options, type: Options, from: :options
|
|
25
|
+
|
|
26
|
+
auto_initialize!
|
|
27
|
+
|
|
28
|
+
# Retrieve the current connection settings, including information regarding the available baudrates and serial ports
|
|
29
|
+
# and the current connection state.
|
|
30
|
+
#
|
|
31
|
+
# @return [Connection]
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
# client = Octoprint::Client.new(host: 'https://octopi.local/', api_key: 'j98G2nsJq...')
|
|
35
|
+
#
|
|
36
|
+
# connection = Octoprint::Connection.get(client: client)
|
|
37
|
+
# connection.current.state= #=> "Printing"
|
|
38
|
+
# connection.options.ports #=> ["/dev/ttyACM0"]
|
|
39
|
+
def self.get
|
|
40
|
+
fetch_resource
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Closses the serial connection from Octoprint to the printer
|
|
44
|
+
#
|
|
45
|
+
# @return [True]
|
|
46
|
+
def self.disconnect
|
|
47
|
+
post(params: { command: "disconnect" })
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Opens the serial connection from Octoprint to the printer. When the options are left out, OctoPrint will use
|
|
51
|
+
# existing preference if available or attempt autodetection.
|
|
52
|
+
#
|
|
53
|
+
# @option [String] port The port to connect to.
|
|
54
|
+
# @option [Integer] baudrate The baudrate to connect with.
|
|
55
|
+
# @option [String] printerProfile The id of the printer profile to use for the connection.
|
|
56
|
+
# @option [Boolean] save Whether to save the supplied connection settings as the new preference.
|
|
57
|
+
# @option [Boolean] autoconnect Whether to attempt to automatically connect to the printer on server startup.
|
|
58
|
+
#
|
|
59
|
+
# @return [True]
|
|
60
|
+
def self.connect(params = {})
|
|
61
|
+
params[:command] = "connect"
|
|
62
|
+
post(params: params)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Fakes an acknowledgment message for OctoPrint in case one got lost on the serial line and the communication with
|
|
66
|
+
# the printer since stalled. This should only be used in “emergencies” (e.g. to save prints), the reason for the
|
|
67
|
+
# lost acknowledgment should always be properly investigated and removed instead of depending on this “symptom
|
|
68
|
+
# solver”.
|
|
69
|
+
# @return [True]
|
|
70
|
+
def self.fake_ack
|
|
71
|
+
post(params: { command: "fake_ack" })
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|