class Sumo::Client

This class has the lowest-level interface to interact with the Sumo Job API.

Constants

DEFAULT_ERROR_MESSAGE

The error message raised when the result can be parsed from Sumo.

REDIRECT_STATUSES

Attributes

email[R]
password[R]

Public Class Methods

new(credentials = Sumo.creds) click to toggle source

Create a new `Sumo::Client` with the given credentials.

# File lib/sumo/client.rb, line 13
def initialize(credentials = Sumo.creds)
  @email = credentials['email'].freeze
  @password = credentials['password'].freeze
end

Public Instance Methods

request(hash, &block) click to toggle source

Send a request to the API and retrieve processed data.

# File lib/sumo/client.rb, line 19
def request(hash, &block)
  handle_request(hash, &block).body
end

Private Instance Methods

add_defaults(hash) click to toggle source

Private functions that operate on the request and response.

# File lib/sumo/client.rb, line 48
def add_defaults(hash)
  hash[:headers] = default_headers.merge(hash[:headers] || {})
  hash[:path] = "/api/v#{Sumo::API_VERSION}#{hash[:path]}" unless
    hash[:path].index("/api/v#{Sumo::API_VERSION}") == 0
  hash
end
connection(endpoint = nil) click to toggle source
# File lib/sumo/client.rb, line 110
def connection(endpoint = nil)
  @connections ||= {}
  endpoint ||= 'https://api.sumologic.com'

  fail 'Base url out of allowed domain.' unless
    endpoint.match(%r{^https://.+\.sumologic\.com$})
  @connections[endpoint] ||= Excon.new(endpoint)
end
creds() click to toggle source
# File lib/sumo/client.rb, line 105
def creds
  [email, password].join(':')
end
default_headers() click to toggle source
# File lib/sumo/client.rb, line 90
def default_headers
  {
    'Authorization' => "Basic #{encoded_creds}",
    'Content-Type' => 'application/json',
    'Cookie' => cookie,
    'Accept' => 'application/json'
  }.reject { |_, value| value.nil? }
end
encoded_creds() click to toggle source
# File lib/sumo/client.rb, line 100
def encoded_creds
  @encoded_creds ||= Base64.encode64(creds).gsub(/\s+/, '')
end
extract_error_message(body) click to toggle source
# File lib/sumo/client.rb, line 83
def extract_error_message(body)
  JSON.parse(body)['message'] || DEFAULT_ERROR_MESSAGE
rescue
  DEFAULT_ERROR_MESSAGE
end
handle_errors!(response) click to toggle source
# File lib/sumo/client.rb, line 70
def handle_errors!(response)
  case response.status
  when 400..499 then raise ClientError, extract_error_message(response.body)
  when 500..599 then raise ServerError, extract_error_message(response.body)
  end
end
handle_redirect(response, hash, depth, &block) click to toggle source

Recursively handle redirection up to 10 level depth

# File lib/sumo/client.rb, line 57
def handle_redirect(response, hash, depth, &block)
  fail 'Too many redirections.' if depth > 9

  endpoint = response.headers['Location']
             .match(%r{^(https://.+\.[a-z]+)/}).to_a[1]

  depth += 1
  # I tried to blindly follow redirection path, but it omits the job ID.
  # hash[:path] = path
  handle_request(hash, endpoint, depth, &block)
end
handle_request(hash, endpoint = nil, depth = 0, &block) click to toggle source

Send a HTTP request to the server, handling any errors that may occur.

# File lib/sumo/client.rb, line 24
def handle_request(hash, endpoint = nil, depth = 0, &block)
  response = connection(endpoint).request(add_defaults(hash), &block)

  if REDIRECT_STATUSES.include?(response.status) &&
     response.headers['Location']
    response = handle_redirect(response, hash, depth, &block)
  end

  handle_errors!(response)
  set_cookie!(response)
  response
end