mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-20 14:45:56 +08:00
Added error handling to API wrapper [SCI-5939]
This commit is contained in:
parent
61919e81b6
commit
53e072f79e
|
@ -3,6 +3,10 @@
|
|||
module LabelPrinters
|
||||
module Fluics
|
||||
class ApiClient
|
||||
class NotFoundError < StandardError; end
|
||||
class ServerError < StandardError; end
|
||||
class BadRequestError < StandardError; end
|
||||
|
||||
include HTTParty
|
||||
base_uri 'https://print-api.fluics.com/latest'
|
||||
|
||||
|
@ -14,26 +18,46 @@ module LabelPrinters
|
|||
end
|
||||
|
||||
def list
|
||||
self.class.get('/get_printers_list')
|
||||
do_request(:get, '/get_printers_list')
|
||||
end
|
||||
|
||||
def status(lid)
|
||||
self.class.get("/#{lid}/status")
|
||||
do_request(:get, "/#{lid}/status")
|
||||
end
|
||||
|
||||
def calibrate(lid)
|
||||
self.class.post("/#{lid}/calibrate")
|
||||
do_request(:post, "/#{lid}/calibrate")
|
||||
end
|
||||
|
||||
def print(lid, zpl)
|
||||
self.class.post(
|
||||
do_request(
|
||||
:post,
|
||||
"/#{lid}/print",
|
||||
headers: {
|
||||
'Content-Type' => 'text/plain'
|
||||
},
|
||||
body: zpl
|
||||
params: {
|
||||
headers: { 'Content-Type' => 'text/plain' },
|
||||
body: zpl
|
||||
}
|
||||
)
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue