module Api::Connection
Network layer for API clients.
Constants
- CONVENIENCE_HEADERS
Header keys that can be passed in options hash to {#get},{#head}
Public Instance Methods
Hypermedia agent for the API
@return [Sawyer::Agent]
# File lib/api/connection.rb, line 103 def agent @agent ||= Sawyer::Agent.new(endpoint, sawyer_options) do |http| http.headers[:content_type] = "application/json" http.headers[:user_agent] = user_agent if basic_authenticated? http.basic_auth(@basic_login, @basic_password) elsif token_authenticated? http.authorization @access_token_prefix, @access_token end end end
Make a HTTP DELETE request
@param url [String] The path, relative to {#api_endpoint/#api_version} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/api/connection.rb, line 53 def delete(url, options = {}) request :delete, url, options end
Make a HTTP GET request
@param url [String] The path, relative to {#api_endpoint/#api_version} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/api/connection.rb, line 17 def get(url, options = {}) request :get, url, parse_query_and_convenience_headers(options) end
Make a HTTP HEAD request
@param url [String] The path, relative to {#api_endpoint/#api_version} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/api/connection.rb, line 62 def head(url, options = {}) request :head, url, parse_query_and_convenience_headers(options) end
Response
for last HTTP request
@return [Sawyer::Response]
# File lib/api/connection.rb, line 125 def last_response @last_response if defined? @last_response end
Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in {#auto_paginate}.
@param url [String] The path, relative to {#api_endpoint/#api_version} @param options [Hash] Query and header params for request @param block [Block] Block to perform the data concatination of the
multiple requests. The block is called with two parameters, the first contains the contents of the requests so far and the second parameter contains the latest response.
@return [Sawyer::Resource]
# File lib/api/connection.rb, line 77 def paginate(url, options = {}, &block) opts = parse_query_and_convenience_headers(options.dup) if @auto_paginate || @per_page opts[:query][:per_page] ||= @per_page || (@auto_paginate ? 100 : nil) end data = request(:get, url, opts.dup) if @auto_paginate while @last_response.rels[:next] && rate_limit.remaining > 0 @last_response = @last_response.rels[:next].get(:headers => opts[:headers]) if block_given? yield(data, @last_response) else data.concat(@last_response.data) if @last_response.data.is_a?(Array) end end end data end
Make a HTTP PATCH request
@param url [String] The path, relative to {#api_endpoint/#api_version} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/api/connection.rb, line 44 def patch(url, options = {}) request :patch, url, options end
Make a HTTP POST request
@param url [String] The path, relative to {#api_endpoint/#api_version} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/api/connection.rb, line 26 def post(url, options = {}) request :post, url, options end
Make a HTTP PUT request
@param url [String] The path, relative to {#api_endpoint/#api_version} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/api/connection.rb, line 35 def put(url, options = {}) request :put, url, options end
Fetch the root resource for the API
@return [Sawyer::Resource]
# File lib/api/connection.rb, line 118 def root get "/" end
Protected Instance Methods
# File lib/api/connection.rb, line 131 def endpoint api_endpoint end
Private Instance Methods
# File lib/api/connection.rb, line 171 def parse_query_and_convenience_headers(options) headers = options.delete(:headers) { Hash.new } CONVENIENCE_HEADERS.each do |h| if header = options.delete(h) headers[h] = header end end query = options.delete(:query) opts = {:query => options} opts[:query].merge!(query) if query && query.is_a?(Hash) opts[:headers] = headers unless headers.empty? opts end
# File lib/api/connection.rb, line 159 def raise_on_unsuccessful_status(response) Response::RaiseError.try(response) end
# File lib/api/connection.rb, line 141 def request(method, path, data, options = {}) if data.is_a?(Hash) options[:query] = data.delete(:query) || {} options[:headers] = data.delete(:headers) || {} if accept = data.delete(:accept) options[:headers][:accept] = accept end end if api_version path = File.join(api_version, path) end @last_response = response = agent.call(method, URI::Parser.new.escape(path.to_s), data, options) raise_on_unsuccessful_status(response) response.data end
# File lib/api/connection.rb, line 137 def reset_agent @agent = nil end
# File lib/api/connection.rb, line 163 def sawyer_options opts = @sawyer_options conn_opts = @connection_options opts[:faraday] = Faraday.new(conn_opts) opts end