class Ubiquity::VDMS::Uplynk::API::Client::HTTPClient

Constants

DEFAULT_BASE_PATH
DEFAULT_HEADER_ACCEPTS
DEFAULT_HEADER_CONTENT_TYPE
DEFAULT_HTTP_HOST_ADDRESS
DEFAULT_HTTP_HOST_PORT

Attributes

authorization_header_key[RW]
authorization_header_value[RW]
base_uri[RW]
default_request_headers[RW]
http[RW]
http_host_address[RW]
http_host_port[RW]
log_pretty_print_body[RW]
log_request_body[RW]
log_response_body[RW]
logger[RW]
password[RW]
request[RW]
response[RW]
use_exceptions[RW]
username[RW]

Public Class Methods

new(args = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 43
def initialize(args = {})
  args = args.dup

  @use_exceptions = args.fetch(:use_exceptions, true)

  initialize_logger(args)
  initialize_http(args)

  logger.debug { "#{self.class.name}::#{__method__} Arguments: #{args.inspect}" }

  @username = args[:username] || args[:owner]
  @password = args[:password] || args[:secret]

  @base_uri = args[:base_uri] || "http#{http.use_ssl? ? 's' : ''}://#{http.address}:#{http.port}"
  @default_base_path = args[:default_base_path] || DEFAULT_BASE_PATH

  # @user_agent_default = "#{@hostname}:#{@username} Ruby SDK Version #{Vidispine::VERSION}"

  @authorization_header_key ||= 'Authorization' #CaseSensitiveHeaderKey.new('Authorization')
  @authorization_header_value ||= %(Basic #{["#{username}:#{password}"].pack('m').delete("\r\n")})

  content_type = args[:content_type_header] ||= DEFAULT_HEADER_CONTENT_TYPE
  accepts = args[:accepts_header] ||= args[:accept_header] || DEFAULT_HEADER_ACCEPTS

  @default_request_headers = {
      'Content-Type' => content_type,
      'Accept' => accepts,
  }

  @log_request_body = args.fetch(:log_request_body, true)
  @log_response_body = args.fetch(:log_response_body, true)
  @log_pretty_print_body = args.fetch(:log_pretty_print_body, true)

  @parse_response = args.fetch(:parse_response, true)
end

Public Instance Methods

build_uri(path = '', query = nil) click to toggle source

@param [String] path @param [Hash|String|Nil] query @return [URI]

# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 157
def build_uri(path = '', query = nil)
  _query = query.is_a?(Hash) ? query.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.respond_to?(:to_s) ? v.to_s : v)}" }.join('&') : query
  _path = "#{path}#{_query and _query.respond_to?(:empty?) and !_query.empty? ? "?#{_query}" : ''}"
  URI.parse(File.join(base_uri, _path))
end
call_method(method_name = :get, args = {}, options = {}) click to toggle source

@param [Symbol] method_name (:get) @param [Hash] args @option args [Hash] :headers ({}) @option args [String] :path ('') @option args [Hash] :query ({}) @option args [Any] :body (nil) @param [Hash] options @option options [Hash] :default_request_headers (@default_request_headers)

# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 181
def call_method(method_name = :get, args = {}, options = {})
  headers = args[:headers] || options[:headers] || {}
  path = args[:path] || ''
  query = args[:query] || {}
  body = args[:body]

  # Allow the default request headers to be overridden
  _default_request_headers = options.fetch(:default_request_headers, default_request_headers)
  _default_request_headers ||= {}
  _headers = _default_request_headers.merge(headers)

  @uri = build_uri(path, query)
  klass_name = request_method_name_to_class_name(method_name)
  klass_name = :GetWithBody if klass_name == :GET || klass_name == 'GET'
  klass = Net::HTTP.const_get(klass_name)

  request = klass.new(@uri.request_uri, _headers)

  if request.request_body_permitted?
    _body = (body and !body.is_a?(String)) ? JSON.generate(body) : body
    logger.debug { "Processing Body: '#{_body}'" }
    request.body = _body if _body
  end

  send_request(request)
end
delete(path, options = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 208
def delete(path, options = {})
  query = options.fetch(:query, {})
  base_path = options[:base_path] || (path.start_with?(DEFAULT_BASE_PATH) ? '' : @default_base_path)
  @uri = build_uri(File.join(base_path, path), query)
  request = Net::HTTP::Delete.new(@uri.request_uri, default_request_headers)
  send_request(request)
end
format_body_for_log_output(obj) click to toggle source

Formats a HTTPRequest or HTTPResponse body for log output. @param [HTTPRequest|HTTPResponse] obj @return [String]

# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 107
def format_body_for_log_output(obj)
  if obj.content_type == 'application/json'
    if @log_pretty_print_body
      _body = obj.body
      output = JSON.pretty_generate(JSON.parse(_body)) rescue _body
      return output
    else
      return obj.body
    end
  elsif obj.content_type == 'application/xml'
    return obj.body
  else
    return obj.body.inspect
  end
end
get(path, body, options = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 216
def get(path, body, options = {})
  # Allow the default request headers to be overridden
  headers = options[:headers] || {}
  _default_request_headers = options.fetch(:default_request_headers, default_request_headers) || {}
  _headers = _default_request_headers.merge(headers)

  query ||= options.fetch(:query, {})
  base_path = options[:base_path] || (path.start_with?(DEFAULT_BASE_PATH) ? '' : @default_base_path)
  @uri = build_uri(File.join(base_path, path), query)
  request = Net::HTTP::GetWithBody.new(@uri.request_uri, _headers)

  body = JSON.generate(body) if body and !body.is_a?(String)

  request.body = body if body

  send_request(request)
end
head(path, options = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 234
def head(path, options = {})
  # Allow the default request headers to be overridden
  headers = options[:headers] || {}
  _default_request_headers = options.fetch(:default_request_headers, default_request_headers) || {}
  _headers = _default_request_headers.merge(headers)

  query ||= options.fetch(:query, {})
  base_path = options[:base_path] || (path.start_with?(DEFAULT_BASE_PATH) ? '' : @default_base_path)
  @uri = build_uri(File.join(base_path, path), query)

  request = Net::HTTP::Head.new(@uri.request_uri, _headers)
  send_request(request)
end
initialize_http(args = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 89
def initialize_http(args = {})
  @http_host_address = args[:http_host_address] ||= DEFAULT_HTTP_HOST_ADDRESS
  @http_host_port = args[:http_host_port] ||= DEFAULT_HTTP_HOST_PORT
  @http = Net::HTTP.new(http_host_address, http_host_port)

  use_ssl = args[:http_host_use_ssl]
  if use_ssl
    @http.use_ssl = true
    http_verify_mode = args[:http_host_ssl_verify_mode] #|| OpenSSL::SSL::VERIFY_NONE
    @http.verify_mode = http_verify_mode if http_verify_mode
  end

  http
end
initialize_logger(args = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 79
def initialize_logger(args = {})
  @logger = args[:logger] ||= Logger.new(args[:log_to] || STDOUT)
  log_level = args[:log_level]
  if log_level
    @logger.level = log_level
    args[:logger] = @logger
  end
  @logger
end
options(path, options = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 248
def options(path, options = {})
  # Allow the default request headers to be overridden
  headers = options[:headers] || {}
  _default_request_headers = options.fetch(:default_request_headers, default_request_headers) || {}
  _headers = _default_request_headers.merge(headers)

  query ||= options.fetch(:query, {})
  base_path = options[:base_path] || (path.start_with?(DEFAULT_BASE_PATH) ? '' : @default_base_path)
  @uri = build_uri(File.join(base_path, path), query)
  request = Net::HTTP::Options.new(@uri.request_uri, _headers)
  send_request(request)
end
post(path, body, options = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 278
def post(path, body, options = {})
  # Allow the default request headers to be overridden
  headers = options[:headers] || {}
  _default_request_headers = options.fetch(:default_request_headers, default_request_headers) || {}
  _headers = _default_request_headers.merge(headers)

  query = options.fetch(:query, {})
  base_path = options[:base_path] || (path.start_with?(DEFAULT_BASE_PATH) ? '' : @default_base_path)
  @uri = build_uri(File.join(base_path, path), query)

  request = Net::HTTP::Post.new(@uri.request_uri, _headers)

  body = JSON.generate(body) if body and !body.is_a?(String)

  request.body = body if body
  send_request(request)
end
put(path, body, options = {}) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 261
def put(path, body, options = {})
  # Allow the default request headers to be overridden
  headers = options[:headers] || {}
  _default_request_headers = options.fetch(:default_request_headers, default_request_headers) || {}
  _headers = _default_request_headers.merge(headers)

  query = options.fetch(:query, {})
  base_path = options[:base_path] || (path.start_with?(DEFAULT_BASE_PATH) ? '' : @default_base_path)
  @uri = build_uri(File.join(base_path, path), query)
  request = Net::HTTP::Put.new(@uri.request_uri, _headers)

  body = JSON.generate(body) if body and !body.is_a?(String)

  request.body = body if body
  send_request(request)
end
request_method_name_to_class_name(method_name) click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 164
def request_method_name_to_class_name(method_name)
  method_name.to_s.capitalize
end
response_parsed() click to toggle source
# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 140
def response_parsed
  @response_parsed ||= begin
    response_body = response.respond_to?(:body) ? response.body : ''
    logger.debug { "Parsing Response. #{response_body.inspect}" }

    case response.content_type
      when 'application/json'
        response_body.empty? ? response_body : JSON.parse(response_body) # rescue response
      else
        response_body
    end
  end
end
send_request(request) click to toggle source

@param [HTTPRequest] request

# File lib/ubiquity/vdms/uplynk/api/client/http_client.rb, line 124
def send_request(request)
  @response_parsed = nil
  @request = request
  logger.debug { %(REQUEST: #{request.method} http#{http.use_ssl? ? 's' : ''}://#{http.address}:#{http.port}#{request.path} HEADERS: #{request.to_hash.inspect} #{log_request_body and request.request_body_permitted? ? "\n-- BODY BEGIN --\n#{format_body_for_log_output(request)}\n-- BODY END --" : ''}) }

  @request_time_start = Time.now
  @response = http.request(request)
  @request_time_end = Time.now
  logger.debug { %(RESPONSE: #{response.inspect} HEADERS: #{response.to_hash.inspect} #{log_response_body and response.respond_to?(:body) ? "\n-- BODY BEGIN --\n#{format_body_for_log_output(response)}\n-- BODY END--" : ''}\nTook: #{@request_time_end - @request_time_start} seconds) }
  #logger.debug { "Parse Response? #{@parse_response}" }

  raise HTTPAuthorizationError if @use_exceptions && @response.code == '401'

  @parse_response ? response_parsed : response.body
end