class Datacentred::Request::Base
Base
class for all API requests.
Uses the Faraday library for HTTP interactions.
Public Class Methods
Access a resource via HTTP DELETE.
@param [String] path Desired server path. @raise [Errors::Error] Raised if the server returns a non 2xx error code. @return [nil] Returns nil on success.
# File lib/datacentred/request/base.rb, line 42 def delete(path) action :delete, path end
Access a resource via HTTP GET.
@param [String] path Desired server path. @raise [Errors::Error] Raised if the server returns a non 2xx error code. @return [Object] Parsed server response body.
# File lib/datacentred/request/base.rb, line 13 def get(path) action :get, path end
Access a resource via HTTP POST.
@param [String] path Desired server path. @param [Object] payload JSON serializable object. @raise [Errors::Error] Raised if the server returns a non 2xx error code. @return [Object] Parsed server response body.
# File lib/datacentred/request/base.rb, line 23 def post(path, payload=nil) action :post, path, payload end
Access a resource via HTTP PUT.
@param [String] path Desired server path. @param [Object] payload JSON serializable object. @raise [Errors::Error] Raised if the server returns a non 2xx error code. @return [Object] Parsed server response body.
# File lib/datacentred/request/base.rb, line 33 def put(path, payload=nil) action :put, path, payload end
Private Class Methods
# File lib/datacentred/request/base.rb, line 65 def accept_type "application/vnd.datacentred.api+json" end
# File lib/datacentred/request/base.rb, line 48 def action(verb, path, payload=nil) params = [path, payload&.to_json].compact response = Datacentred::Response.new connection.send verb, *params response.body end
# File lib/datacentred/request/base.rb, line 69 def api_version "1".freeze end
# File lib/datacentred/request/base.rb, line 73 def base_url "https://my.datacentred.io" end
# File lib/datacentred/request/base.rb, line 54 def connection Faraday.new(url: base_url) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter faraday.headers['Accept'] = "#{accept_type}; version=#{api_version}" faraday.headers['Authorization'] = "Token token=#{credentials}" faraday.headers['Content-Type'] = "application/json" faraday.path_prefix = "/api/" end end
# File lib/datacentred/request/base.rb, line 77 def credentials [Datacentred.access_key, Datacentred.secret_key].join ":" end