require 'api-auth' require 'uri' require 'net/http' require 'json' module Hover module Client class HTTP attr_accessor :prefix, :site def initialize(site = 'http://localhost:3000', prefix = 'api/v1') @prefix = prefix @site = site end def authenticate(request) end def make_uri(path, parameters = {}) parts = [site, prefix, path] uri = URI.parse(parts.map(&:presence).compact.join('/')) uri = URI.parse("#{uri.to_s}?#{URI.encode_www_form(parameters)}") unless parameters.empty? uri end def make_request(request, uri) authenticate(request) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true if 'https' == uri.scheme response = http.request(request) raise_errors_for_cloudfront_response(response) response end def get(path, parameters = {}) uri = make_uri(path, parameters) request = Net::HTTP::Get.new(uri) response = make_request(request, uri) response end def get_redirect_location(path, parameters = {}, &block) uri = make_uri(path, parameters) request = Net::HTTP::Get.new(uri) redirect_response = make_request(request, uri) if redirect_response.is_a?(Net::HTTPRedirection) && redirect_response['location'] redirect_response['location'] else nil end end def post(path, parameters = {}) uri = make_uri(path) request = Net::HTTP::Post.new(uri) make_form_request(request, uri, parameters) end def make_form_request(request, uri, parameters) request.set_form_data(parameters) response = make_request(request, uri) response end def put(path, parameters = {}) uri = make_uri(path) request = Net::HTTP::Put.new(uri) make_form_request(request, uri, parameters) end def patch(path, parameters = {}) uri = make_uri(path) request = Net::HTTP::Patch.new(uri) make_form_request(request, uri, parameters) end def delete(path, parameters = {}) uri = make_uri(path) request = Net::HTTP::Delete.new(uri) make_form_request(request, uri, parameters) end def parse_response(response) raise_errors(response) case response.code.to_i when 204 # no content {} else ::JSON.parse(response.body) end end def raise_errors_for_cloudfront_response(response) # # If the server can't be reached cloudfront will respond with an # error message of it's own. We need to catch those so that we can retry # the requests. # return unless (response.header['Server'] =~ /Cloudfront/i) raise ::Hover::Client::Unavailable end def raise_errors(response) message = "(#{response.code}): - #{response.body}" case response.code.to_i when 400 raise BadRequest, message when 401 raise Unauthorized, message when 403 raise General, message when 404 raise NotFound, message when 500 raise InternalError, "Internal error: #{message}" when 502..503 raise Unavailable, message end end end class BadRequest < StandardError; end class Unauthorized < StandardError; end class General < StandardError; end class Unavailable < StandardError; end class InternalError < StandardError; end class NotFound < StandardError; end class AloomaReportingError < StandardError; end end end