Class: Gleis::API
- Inherits:
-
Object
- Object
- Gleis::API
- Defined in:
- lib/gleis/api.rb
Overview
This class uses REST over HTTPS to communicate with the API server
Class Method Summary collapse
Class Method Details
.request(method, action, token = nil, body = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gleis/api.rb', line 6 def self.request(method, action, token = nil, body = {}) url = Config::API_URL + Config::API_VERSION + '/cli/' + action # Support legacy rest-client (<2.0) which does not have RestClient::NotFound notfound_exception = defined?(RestClient::ResourceNotFound) ? RestClient::ResourceNotFound : RestClient::NotFound begin case method when 'get', 'delete' resp = RestClient.send(method, url, 'X-Gleis-Token': token, content_type: :json, accept: :json) when 'post', 'put' if token resp = RestClient.send(method, url, body.to_json, 'X-Gleis-Token': token, content_type: :json, accept: :json) else resp = RestClient.send(method, url, body.to_json, content_type: :json, accept: :json) end end rescue RestClient::BadRequest abort('Authentication failed.') rescue RestClient::Unauthorized abort('Not authenticated, please login first.') rescue RestClient::Forbidden abort('You do not have the required permissions, please contact the app owner.') rescue notfound_exception abort('Application not found.') rescue RestClient::InternalServerError abort('An error occured on the platform. Please contact support if this problem persists.') rescue StandardError # (e.g. SocketError, Errno::ECONNREFUSED, RestClient::BadGateway, ...) abort('There was an issue connecting to the Gleis API server.') else if resp.body.empty? # If there is no body return whole response object resp else # Return Ruby data structure of the JSON parsed body JSON.parse(resp.body) end end end |