Class: Printos::PrintosRestClient
- Inherits:
-
Object
- Object
- Printos::PrintosRestClient
- Defined in:
- lib/printos/printos_rest_client.rb
Constant Summary collapse
- SERVICE_TOKEN_EXPIRED_CODE =
Default reponse code that is expected from PrintOS when a service token has expired
401
Class Method Summary collapse
- .execute(method, url, payload, as_service, service_token_expired_code, allow_retry = true) ⇒ Object
- .get(url, as_service, expired_code = SERVICE_TOKEN_EXPIRED_CODE) ⇒ Object
- .post(url, payload, as_service, expired_code = SERVICE_TOKEN_EXPIRED_CODE) ⇒ Object
- .put(url, payload, as_service, expired_code = SERVICE_TOKEN_EXPIRED_CODE) ⇒ Object
Class Method Details
.execute(method, url, payload, as_service, service_token_expired_code, allow_retry = true) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# 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) ⇒ Object
8 9 10 |
# 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) ⇒ Object
12 13 14 |
# 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) ⇒ Object
16 17 18 |
# 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 |