Method: Orchestrate::Client#send_request

Defined in:
lib/orchestrate/client.rb

#send_request(method, path, options = {}) ⇒ Object

Performs an HTTP request against the Orchestrate API

Parameters:

  • method (Symbol)

    The HTTP method - :get, :post, :put, :delete

  • path (Array<#to_s>)

    Path segments for the request's URI. Prepended by 'v0'.

  • options (Hash) (defaults to: {})

    extra parameters

Options Hash (options):

  • :query (Hash) — default: {}

    Query String parameters.

  • :body (#to_json) — default: ''

    The request body.

  • :headers (Hash) — default: {}

    Extra request headers.

  • :response (Class) — default: Orchestrate::API::Response

    A subclass of Orchestrate::API::Response to instantiate and return

Returns:

  • API::Response

Raises:

[View source]

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/orchestrate/client.rb', line 434

def send_request(method, path, options={})
  path = ['/v0'].concat(path.map{|s| URI.escape(s.to_s).gsub('/','%2F') }).join('/')
  query_string = options.fetch(:query, {})
  body = options.fetch(:body, '')
  headers = options.fetch(:headers, {})
  headers['User-Agent'] = "ruby/orchestrate/#{Orchestrate::VERSION}"
  headers['Accept'] = 'application/json' if method == :get
  headers['Connection'] = 'close' if method == :head

  http_response = http.send(method) do |request|
    request.url path, query_string
    if [:put, :post].include?(method)
      headers['Content-Type'] = 'application/json'
      request.body = body.to_json
    elsif [:patch].include?(method)
      request.body = body.to_json
    end
    headers.each {|header, value| request[header] = value }
  end

  response_class = options.fetch(:response, API::Response)
  response_class.new(http_response, self)
end