require 'rest-client' require 'uuid' class Fanforce::API include Fanforce::API::Utils # Initialize a new API connection # @param auth_data [String,Hash] is a string of api_key or hash with api_key def initialize(auth_data=nil) set_auth(auth_data) if auth_data end # Get the CURL command for a request # @param method [Symbol] (get/post/put/delete) # @param path [String] # @param params [Hash] # @return [String] curl command def curl_command(method, path, params={}) params = inject_auth_params(params) Fanforce.curl_command(method, request_url(path), params) end # Handles a GET request for Fanforce API # @param path [String] # @param params [Hash] # @return [Fanforce::API::Response] def get(path, params={}) params = inject_auth_params(params) headers = {'Accept' => 'application/json'} RestClient.get(request_url(path, params), headers) do |response, request, result, &block| Fanforce::API::Response.process(response, request, request_url(path), params) end end # Handles a POST request for Fanforce API # @param path [String] # @param params [Hash] # @return [Fanforce::API::Response] def post(path, params={}, retries=0) url = request_url(path) params = inject_auth_params(params) unless retries > 0 headers = {'Content-Type' => 'application/json', 'Accept' => 'application/json'} RestClient.post(url, MultiJson.dump(params), headers) do |response, request, result, &block| if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2 puts "retried #{path} #{retries+1} times" post(path, params, retries+1) else Fanforce::API::Response.process(response, request, url, params) end end end # Handles a PUT request for Fanforce API # @param path [String] # @param params [Hash] # @return [Fanforce::API::Response] def put(path, params={}, retries=0) url = request_url(path) params = inject_auth_params(params) unless retries > 0 headers = {'Content-Type' => 'application/json', 'Accept' => 'application/json'} RestClient.put(url, MultiJson.dump(params), headers) do |response, request, result, &block| if response.code == 400 and response.body == '{"error":"Invalid JSON"}' and retries <= 2 puts "retried #{path} #{retries+1} times" post(path, params, retries+1) else Fanforce::API::Response.process(response, request, url, params) end end end # Handles a DELETE request for Fanforce API # @param path [String] # @param params [Hash] # @return [Fanforce::API::Response] def delete(path, params={}) url = request_url(path) params = inject_auth_params(params) headers = {'Accept' => 'application/json'} RestClient.delete(request_url(path, params), headers) do |response, request, result, &block| Fanforce::API::Response.process(response, request, url, params) end end # Get the auth params for current instance # @return [Hash] current auth params def auth_params @auth_params.clone end def organization_id @auth_params[:organization_id] end def api_key @auth_params[:api_key] end # Auth the current instance. Will raise error if api_key not found or is not of UUID format. # @param auth_data [String, Hash] either an api_key string or a hash containing it # @return [nil] def set_auth(auth_data={}) auth_data = {} if is_blank?(auth_data) if auth_data.is_a?(String) @auth_params = {api_key: auth_data} elsif auth_data.is_a?(Hash) auth_data = symbolize_keys(auth_data) @auth_params = { api_key: auth_data[:api_key], organization_id: auth_data[:organization_id] } end raise 'A valid api_key is required to auth Fanforce::API' if !has_valid_api_key? end # Check if current instance has a valid api_key # @return [Boolean] def has_valid_api_key? is_present?(@auth_params) and is_present?(@auth_params[:api_key]) and UUID.validate(@auth_params[:api_key]) end alias_method :has_valid_auth?, :has_valid_api_key? ###################################################################### private def inject_auth_params(params) params.merge(@auth_params || {}) end def request_url(path, params={}) "http://#{Fanforce.api_domain}#{path}#{'?'+to_query_string(params) if params.size > 0}" end end