class Bugsnag::Api::Client
Constants
- CONVENIENCE_HEADERS
Header keys that can be passed in options hash to {#get},{#head}
Public Class Methods
# File lib/bugsnag/api/client.rb, line 41 def initialize(options = {}, &block) configuration.load(options) yield(configuration) if block_given? end
Public Instance Methods
Indicates if the client was supplied Basic Auth username and password
@see bugsnag.com/docs/api#user-authentication @return [Boolean]
# File lib/bugsnag/api/client.rb, line 140 def basic_authenticated? !!(configuration.email && configuration.password) end
Get client’s configuration options
@return [Bugsnag::Api::Configuration] configuration wrapper
# File lib/bugsnag/api/client.rb, line 55 def configuration @configuration ||= Configuration.new end
Set configuration options using a block
# File lib/bugsnag/api/client.rb, line 47 def configure yield(configuration) if block_given? reset_agent end
Merges hashes together cleanly, favouring RHS values
@return [Hash]
# File lib/bugsnag/api/client.rb, line 155 def deep_merge(l_hash, r_hash) l_hash.merge(r_hash) do |_key, l_val, r_val| if l_val.is_a?(Hash) && r_val.is_a?(Hash) deep_merge(l_val, r_val) else r_val end end end
Make a HTTP DELETE request
@param url [String] The path, relative to {#endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/bugsnag/api/client.rb, line 91 def delete(url, options = {}) request :delete, url, options end
Make a HTTP GET request
@param url [String] The path, relative to {#endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]
# File lib/bugsnag/api/client.rb, line 64 def get(url, options = {}) request :get, url, parse_query_and_convenience_headers(options) end
Response
for last HTTP request
@return [Sawyer::Response]
# File lib/bugsnag/api/client.rb, line 131 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 {#endpoint} @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/bugsnag/api/client.rb, line 106 def paginate(url, options = {}, &block) opts = parse_query_and_convenience_headers(options.dup) if configuration.auto_paginate || configuration.per_page opts[:query][:per_page] ||= configuration.per_page || (configuration.auto_paginate ? 100 : nil) end data = request(:get, url, opts) if configuration.auto_paginate while @last_response.rels[:next] @last_response = @last_response.rels[:next].get 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 {#endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/bugsnag/api/client.rb, line 82 def patch(url, options = {}) request :patch, url, options end
Make a HTTP POST request
@param url [String] The path, relative to {#endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]
# File lib/bugsnag/api/client.rb, line 73 def post(url, options = {}) request :post, url, options end
Indicates if the client was supplied an auth token
@see bugsnag.com/docs/api#account-authentication @return [Boolean]
# File lib/bugsnag/api/client.rb, line 148 def token_authenticated? !!configuration.auth_token end
Private Instance Methods
# File lib/bugsnag/api/client.rb, line 166 def agent @agent ||= Sawyer::Agent.new(configuration.endpoint, sawyer_options) do |http| http.headers[:content_type] = "application/json" http.headers[:'X-Version'] = "2" http.headers[:'X-Bugsnag-Api'] = "true" http.headers[:user_agent] = configuration.user_agent if basic_authenticated? credentials = Base64.strict_encode64("#{configuration.email}:#{configuration.password}") http.headers[:Authorization] = "Basic #{credentials}" elsif token_authenticated? http.headers[:Authorization] = "token #{configuration.auth_token}" end end end
# File lib/bugsnag/api/client.rb, line 200 def boolean_from_response(method, path, options = {}) request(method, path, options) @last_response.status == 204 rescue Bugsnag::Api::NotFound false end
# File lib/bugsnag/api/client.rb, line 219 def parse_query_and_convenience_headers(options) headers = options.fetch(:headers, {}) 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/bugsnag/api/client.rb, line 187 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 @last_response = response = agent.call(method, path.to_s, data, options) response.data end
# File lib/bugsnag/api/client.rb, line 183 def reset_agent @agent = nil end
# File lib/bugsnag/api/client.rb, line 207 def sawyer_options opts = { :links_parser => Sawyer::LinkParsers::Simple.new } conn_opts = configuration.connection_options conn_opts[:builder] = configuration.middleware if configuration.middleware conn_opts[:proxy] = configuration.proxy if configuration.proxy opts[:faraday] = Faraday.new(conn_opts) opts end