scinote-web/app/services/label_printers/fluics/api_client.rb

71 lines
1.6 KiB
Ruby
Raw Normal View History

2021-07-23 19:52:28 +08:00
# frozen_string_literal: true
module LabelPrinters
module Fluics
class ApiClient
class NotFoundError < StandardError; end
class ServerError < StandardError; end
class BadRequestError < StandardError; end
2021-07-23 19:52:28 +08:00
include HTTParty
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
do_request(:get, '/get_printers_list')
2021-07-23 19:52:28 +08:00
end
def status(lid)
do_request(:get, "/#{lid}/status")
2021-07-23 19:52:28 +08:00
end
def calibrate(lid)
do_request(:post, "/#{lid}/calibrate")
2021-07-23 19:52:28 +08:00
end
def print(lid, zpl)
do_request(
:post,
2021-07-23 19:52:28 +08:00
"/#{lid}/print",
params: {
headers: { 'Content-Type' => 'text/plain' },
body: zpl
}
2021-07-23 19:52:28 +08:00
)
end
def list_templates
group_name = ENV['FLUICS_TEMPLATES_GROUP'] || ''
do_request(:get, '/get_templates', params: { query: { templateGroups: group_name } })
end
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