2021-07-23 19:52:28 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module LabelPrinters
|
|
|
|
module Fluics
|
|
|
|
class ApiClient
|
2021-07-27 16:36:06 +08:00
|
|
|
class NotFoundError < StandardError; end
|
2021-07-27 21:26:18 +08:00
|
|
|
|
2021-07-27 16:36:06 +08:00
|
|
|
class ServerError < StandardError; end
|
2021-07-27 21:26:18 +08:00
|
|
|
|
2021-07-27 16:36:06 +08:00
|
|
|
class BadRequestError < StandardError; end
|
|
|
|
|
2021-07-23 19:52:28 +08:00
|
|
|
include HTTParty
|
2022-07-06 19:02:48 +08:00
|
|
|
base_uri 'https://print-api.fluics.com/v3'
|
2021-07-23 19:52:28 +08:00
|
|
|
|
|
|
|
def initialize(api_key)
|
|
|
|
self.class.headers(
|
|
|
|
'Accept' => 'application/json',
|
|
|
|
'x-api-key' => api_key
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
2021-07-27 16:36:06 +08:00
|
|
|
do_request(:get, '/get_printers_list')
|
2021-07-23 19:52:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def status(lid)
|
2021-07-27 16:36:06 +08:00
|
|
|
do_request(:get, "/#{lid}/status")
|
2021-07-23 19:52:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def calibrate(lid)
|
2021-07-27 16:36:06 +08:00
|
|
|
do_request(:post, "/#{lid}/calibrate")
|
2021-07-23 19:52:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def print(lid, zpl)
|
2021-07-27 16:36:06 +08:00
|
|
|
do_request(
|
|
|
|
:post,
|
2021-07-23 19:52:28 +08:00
|
|
|
"/#{lid}/print",
|
2021-07-27 16:36:06 +08:00
|
|
|
params: {
|
|
|
|
headers: { 'Content-Type' => 'text/plain' },
|
|
|
|
body: zpl
|
|
|
|
}
|
2021-07-23 19:52:28 +08:00
|
|
|
)
|
|
|
|
end
|
2021-07-27 16:36:06 +08:00
|
|
|
|
2022-08-13 01:47:29 +08:00
|
|
|
def list_templates
|
|
|
|
group_name = ENV['FLUICS_TEMPLATES_GROUP'] || ''
|
|
|
|
do_request(:get, '/get_templates', params: { query: { templateGroups: group_name } })
|
|
|
|
end
|
|
|
|
|
2021-07-27 16:36:06 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def do_request(http_method, path, params: {})
|
|
|
|
response = self.class.public_send(http_method, path, params)
|
|
|
|
|
|
|
|
case response.code
|
|
|
|
when 200..299
|
|
|
|
# success response
|
|
|
|
when 404
|
|
|
|
raise NotFoundError, "#{response.code}: #{response.message}"
|
|
|
|
when 400..499
|
|
|
|
raise BadRequestError, "#{response.code}: #{response.message}"
|
|
|
|
when 500...600
|
|
|
|
raise ServerError, "#{response.code}: #{response.message}"
|
|
|
|
end
|
|
|
|
|
|
|
|
response
|
|
|
|
end
|
2021-07-23 19:52:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|