class Printos::PrintosRestClient
Constants
- SERVICE_TOKEN_EXPIRED_CODE
Default reponse code that is expected from PrintOS when a service token has expired
Public Class Methods
execute(method, url, payload, as_service, service_token_expired_code, allow_retry=true)
click to toggle source
# File lib/printos/printos_rest_client.rb, line 20 def self.execute(method, url, payload, as_service, service_token_expired_code, allow_retry=true) return nil if url.nil? if as_service service_token = PrintosServiceToken.get_token token = service_token.auth_token else token = ::RequestStore.store[:printos_token] end Printos.config.logger.debug { "PrintOS API request: method=#{method}, url=#{url}, as_service=#{as_service}, payload=#{payload}" } begin response = RestClient::Request.execute( method: method, url: "#{Printos.config.api_host}/#{url}", payload: payload.to_json, headers: { :content_type => :json, :accept => :json, :cookies => {Printos.config.auth_token_key => token} } ) Printos.config.logger.debug { "PrintOS API response: status_code#{response.code}, body=#{response.body}" } response rescue RestClient::ExceptionWithResponse => e response = e.response Printos.config.logger.debug { "PrintOS API response: status_code#{response.code}, body=#{response.body}" } if as_service && allow_retry && response.code == service_token_expired_code Printos.config.logger.error('Service token is invalid. Will reset the token and retry request.') PrintosServiceToken.reset_token service_token execute(method, url, payload, as_service, service_token_expired_code, false) else raise end end end
get(url, as_service, expired_code=SERVICE_TOKEN_EXPIRED_CODE)
click to toggle source
# File lib/printos/printos_rest_client.rb, line 8 def self.get(url, as_service, expired_code=SERVICE_TOKEN_EXPIRED_CODE) execute(:get, url, {}, as_service, expired_code) end
post(url, payload, as_service, expired_code=SERVICE_TOKEN_EXPIRED_CODE)
click to toggle source
# File lib/printos/printos_rest_client.rb, line 12 def self.post(url, payload, as_service, expired_code=SERVICE_TOKEN_EXPIRED_CODE) execute(:post, url, payload, as_service, expired_code) end
put(url, payload, as_service, expired_code=SERVICE_TOKEN_EXPIRED_CODE)
click to toggle source
# File lib/printos/printos_rest_client.rb, line 16 def self.put(url, payload, as_service, expired_code=SERVICE_TOKEN_EXPIRED_CODE) execute(:put, url, payload, as_service, expired_code) end