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