class Delphix::Connection

This class represents a Connection to a Delphix Engine. The Connection is immutable in that once the url and options is set they cannot be changed.

Attributes

options[R]
url[R]

Public Class Methods

new(url, opts) click to toggle source
# File lib/delphix/connection.rb, line 10
def initialize(url, opts)
  case
  when !url.is_a?(String)
    raise ArgumentError, "Expected a String, got: '#{url}'"
  when !opts.is_a?(Hash)
    raise ArgumentError, "Expected a Hash, got: '#{opts}'"
  else
    @url, @options = url, opts
    
    # new HTTP client and session
    @session = Excon.new(url, options)
    @session_cookie = nil
  end
end

Public Instance Methods

log_request(request) click to toggle source
# File lib/delphix/connection.rb, line 59
def log_request(request)
  puts "#{[request[:method], request[:path], request[:body]]}" if Delphix.debug
end
log_response(response) click to toggle source
# File lib/delphix/connection.rb, line 63
def log_response(response)
  puts "#{[response.headers, response.body]}" if Delphix.debug
end
request(*args, &block) click to toggle source

Send a request to the server

# File lib/delphix/connection.rb, line 26
def request(*args, &block)
  
  request = compile_request_params(*args, &block)
  
  log_request(request) if Delphix.debug
  
  # execute the request and grao the session cookie if not already set
  response = @session.request(request)
  @session_cookie ||= cookie?(response)
  
  log_response(response) if Delphix.debug
  
  return Delphix::Util.parse_json( response.body)
  
rescue Excon::Errors::BadRequest => ex
  raise ClientError, ex.response.body
rescue Excon::Errors::Unauthorized => ex
  raise UnauthorizedError, ex.response.body
rescue Excon::Errors::NotFound => ex
  raise NotFoundError, ex.response.body
rescue Excon::Errors::Conflict => ex
  raise ConflictError, ex.response.body
rescue Excon::Errors::InternalServerError => ex
  raise ServerError, ex.response.body
rescue Excon::Errors::Timeout => ex
  raise TimeoutError, ex.message
end
to_s() click to toggle source
# File lib/delphix/connection.rb, line 67
def to_s
  "Delphix::Connection { :url => #{url}, :options => #{options} }"
end

Private Instance Methods

compile_request_params(http_method, path, query = nil, opts = nil, &block) click to toggle source

Given an HTTP method, path, optional query, extra options, and block compiles a request.

# File lib/delphix/connection.rb, line 74
def compile_request_params(http_method, path, query = nil, opts = nil, &block)
  query ||= {}
  opts ||= {}
  headers = opts.delete(:headers) || {}
  headers = { 'Cookie' => @session_cookie}.merge(headers) if @session_cookie
  content_type = 'application/json'
  user_agent = "Delphix/Delphix-API"
  
  {
    :method        => http_method,
    :path          => "#{path}",
    :query         => query,
    :headers       => { 'Content-Type' => content_type,
                        'User-Agent'   => user_agent,
                      }.merge(headers),
    :expects       => (200..204).to_a << 304 << 403 << 500,
    :idempotent    => http_method == :get,
    :request_block => block
  }.merge(opts).reject { |_, v| v.nil? }
  
end