lib/cdnconnect_api.rb in cdnconnect-api-0.1.0 vs lib/cdnconnect_api.rb in cdnconnect-api-0.1.1

- old
+ new

@@ -21,11 +21,11 @@ module CDNConnect class APIClient @@application_name = 'cdnconnect-api-ruby' - @@application_version = '0.1.0' + @@application_version = '0.1.1' @@api_host = 'https://api.cdnconnect.com' @@user_agent = @@application_name + ' v' + @@application_version ## # Creates a client to authorize interactions with the API using the OAuth 2.0 protocol. @@ -35,13 +35,10 @@ # @param [Hash] options # The configuration parameters for the client. # - <code>:api_key</code> - # An API Key (commonly known as an access_token) which was previously # created within CDN Connect's account for a specific app. - # - <code>:response_format</code> - - # How data should be formatted on the response. Possible values for - # include application/json, application/xml. JSON is the default. # - <code>:client_id</code> - # A unique identifier issued to the client to identify itself to CDN Connect's # authorization server. This is issued by CDN Connect to external clients. # This is only needed if an API Key isn't already known. # - <code>:client_secret</code> - @@ -59,10 +56,12 @@ # The authorization code received from the authorization server. # - <code>:redirect_uri</code> - # The redirection URI used in the initial request. # - <code>:access_token</code> - # The current access token for this client, also known as the API Key. + # - <code>:debug</code> - + # Print out any debugging information. Default is false. def initialize(options={}) # Normalize key to String to allow indifferent access. options = options.inject({}) { |accu, (k, v)| accu[k.to_s] = v; accu } # Initialize all of the options @@ -72,11 +71,11 @@ @state = options["state"] @code = options["code"] @redirect_uri = options["redirect_uri"] options["access_token"] = options["access_token"] || options["api_key"] # both work @access_token = options["access_token"] - @response_format = options["response_format"] || 'application/json' + @debug = options["debug"] || false @prefetched_upload_urls = {} # Create the OAuth2 client which will be used to authorize the requests @client = Signet::OAuth2::Client.new(:client_id => client_id, :client_secret => @client_secret, @@ -91,55 +90,47 @@ ## # Executes a GET request to an API URL and returns a response object. # GET requests are used when reading data. # - # @param url [String] The API URL to send a GET request to. + # @param path [String] The API path to send the GET request to. # @return [APIResponse] A response object with helper methods to read the response. - def get(url) - options[:path] = url - options[:method] = 'GET' - return self.fetch(options) + def get(path) + return self.fetch(:path => path, :method => 'GET') end ## # Executes a POST request to an API URL and returns a response object. # POST requests are used when creating data. # - # @param url [String] The API URL to send a POST request to. + # @param path [String] The API path to send the POST request to. # @return [APIResponse] A response object with helper methods to read the response. - def post(url) - options[:path] = url - options[:method] = 'POST' - return self.fetch(options) + def post(path) + return self.fetch(:path => path, :method => 'POST') end ## # Executes a PUT request to an API URL and returns a response object. # PUT requests are used when updating data. # - # @param url [String] The API URL to send a GET request to. + # @param path [String] The API path to send the POST request to. # @return [APIResponse] A response object with helper methods to read the response. - def post(url) - options[:path] = url - options[:method] = 'POST' - return self.fetch(options) + def put(path) + return self.fetch(:path => path, :method => 'PUT') end ## # Executes a DELETE request to an API URL and returns a response object. # DELETE requests are used when (you guessed it) deleting data. # - # @param url [String] The API URL to send a DELETE request to. + # @param path [String] The API path to send the DELETE request to. # @return [APIResponse] A response object with helper methods to read the response. def delete(url) - options[:path] = url - options[:method] = 'DELETE' - return self.fetch(options) + return self.fetch(:path => path, :method => 'DELETE') end ## # Used to upload a file or files within a folder to a destination folder within @@ -157,10 +148,13 @@ # - <code>:obj_id</code> - # The obj_id of the folder to upload to. If the app_id or obj_id options # are not provided then you must use the url option. # - <code>:source_file_local_path</code> - # A string of a source file's local paths to upload. + # - <code>:response_format</code> - + # How data should be formatted on the response. Possible values + # include json (default) or xml. # @return [APIResponse] A response object with helper methods to read the response. def upload(options={}) # Make sure we've got good data before starting the upload prepare_upload(options) @@ -196,10 +190,13 @@ req.request :multipart req.adapter :net_http end # Kick it off! + if @debug + puts 'upload, source: ' + options[:source_file_local_path] + ', destination: ' + options[:destination_folder_url] + end api_response = conn.post upload_url, post_data # Woot! Convert the response to our model and see what's up response = APIResponse.new(api_response) @@ -288,15 +285,23 @@ def get_upload_url(options={}) destination_folder_url = options[:destination_folder_url] path = nil if destination_folder_url != nil - path = '/v1/' + destination_folder_url + '/upload' + path = destination_folder_url + '/upload' else path = generate_obj_path(options) + '/upload' end + # Default Content-Type is application/json with a .json extension + response_extension = 'json' + if options[:response_format] == 'xml' + response_extension = 'xml' + end + + path = '/v1/' + path + '.' + response_extension + i = 1 begin response = self.fetch(:path => path) if not response.is_server_error or i > 2 return response @@ -331,11 +336,11 @@ if path == nil raise ArgumentError, "missing url or both app_id and obj_id" end - return '/v1/' + path + return path end ## # This method should not be called directly, but is used to validate data @@ -344,27 +349,12 @@ def prepare(options={}) if options[:path] == nil raise ArgumentError, 'missing api path' end - options[:response_format] = options[:response_format] || @response_format - - headers = { 'User-Agent' => @@user_agent } - - # There are three possible response content-types: JSON, XML - # Default Content-Type is application/json with a .json extension - if options[:response_format] == 'application/xml' - headers['Content-Type'] = 'application/xml' - response_extension = 'xml' - else - options[:response_format] = 'application/json' - headers['Content-Type'] = 'application/json' - response_extension = 'json' - end - options[:headers] = headers - - options[:uri] = @@api_host + options[:path] + '.' + response_extension + options[:headers] = { 'User-Agent' => @@user_agent } + options[:uri] = @@api_host + options[:path] options[:url] = options[:uri] options[:method] = options[:method] || 'GET' return options end @@ -375,10 +365,14 @@ # @!visibility private def fetch(options={}) # Prepare the data to be shipped in the request options = self.prepare(options) + if @debug + puts 'fetch: ' + options[:uri] + end + begin # Send the request and get the response response = @client.fetch_protected_resource(options) # Return the API response @@ -386,15 +380,9 @@ rescue Signet::AuthorizationError => detail return APIResponse.new(detail.response) end end - # - <code>:code</code> - - # The authorization code received from the authorization server. - # - <code>:redirect_uri</code> - - # The redirection URI used in the initial request. - # - <code>:access_token</code> - - # The current access token for this client, also known as the API Key. ## # A unique identifier issued to the client to identify itself to CDN Connect's # authorization server. This is issued by CDN Connect to external clients.