class Azure::Core::Http::HttpRequest

Represents a HTTP request can perform synchronous queries to a HTTP server, returning a HttpResponse

Attributes

_method[RW]

The HTTP method to use (:get, :post, :put, :delete, etc…)

body[RW]

The body of the request (IO or String)

client[RW]

Azure client which contains configuration context and http agents @return [Azure::Client]

has_retry_filter[RW]

The http filter

headers[RW]

The header values as a Hash

method[RW]

The HTTP method to use (:get, :post, :put, :delete, etc…)

uri[RW]

The URI of the HTTP endpoint to query

Public Class Methods

new(method, uri, options_or_body = {}) click to toggle source

Public: Create the HttpRequest

@param method [Symbol] The HTTP method to use (:get, :post, :put, :del, etc…) @param uri [URI] The URI of the HTTP endpoint to query @param options_or_body [Hash|IO|String] The request options including {:client, :body} or raw body only

# File lib/azure/core/http/http_request.rb, line 60
def initialize(method, uri, options_or_body = {})
  options ||= unless options_or_body.is_a?(Hash)
                {body: options_or_body}
              end || options_or_body || {}

  @method = method
  @uri = if uri.is_a?(String)
           URI.parse(uri)
         else
           uri
         end

  @client = options[:client] || Azure

  self.headers = default_headers(options[:current_time] || Time.now.httpdate).merge(options[:headers] || {})
  self.body = options[:body]
end

Public Instance Methods

body=(body) click to toggle source
# File lib/azure/core/http/http_request.rb, line 140
def body=(body)
  @body = body
  apply_body_headers
end
call() click to toggle source

Sends request to HTTP server and returns a HttpResponse

@return [HttpResponse]

# File lib/azure/core/http/http_request.rb, line 148
def call
  conn = http_setup
  res = set_up_response(method.to_sym, uri, conn, headers ,body)

  response = HttpResponse.new(res)
  response.uri = uri
  raise response.error if !response.success? && !@has_retry_filter
  response
end
default_headers(current_time) click to toggle source

Build a default headers Hash

# File lib/azure/core/http/http_request.rb, line 125
def default_headers(current_time)
  {}.tap do |def_headers|
    def_headers['User-Agent'] = Azure::Core::Default::USER_AGENT
    def_headers['x-ms-date'] = current_time
    def_headers['x-ms-version'] = '2014-02-14'
    def_headers['DataServiceVersion'] = '1.0;NetFx'
    def_headers['MaxDataServiceVersion'] = '3.0;NetFx'
    def_headers['Content-Type'] = 'application/atom+xml; charset=utf-8'
  end
end
http_setup() click to toggle source
# File lib/azure/core/http/http_request.rb, line 136
def http_setup
  @client.agents(uri)
end
with_filter(filter=nil, options={}, &block) click to toggle source

Public: Applies a HttpFilter to the HTTP Pipeline

filter - Any object that responds to .call(req, _next) and

returns a HttpResponse eg. HttpFilter, Proc,
lambda, etc. (optional)

options - The options that are used when call Azure::Core::FilteredService.call.

It can be used by retry policies to determine changes in the retry.

&block - An inline block may be used instead of a filter

example:

   request.with_filter do |req, _next|
     _next.call
   end

NOTE:

The code block provided must call _next or the filter pipeline will not complete and the HTTP request will never execute

# File lib/azure/core/http/http_request.rb, line 100
def with_filter(filter=nil, options={}, &block)
  filter = filter || block
  if filter
    is_retry_policy = filter.is_a?(Azure::Core::Http::RetryPolicy)
    filter.retry_data[:request_options] = options if is_retry_policy
    @has_retry_filter ||= is_retry_policy
    
    original_call = self._method(:call)

    # support 1.8.7 (define_singleton_method doesn't exist until 1.9.1)
    filter_call = Proc.new do
      filter.call(self, original_call)
    end
    k = class << self;
      self;
    end
    if k.method_defined? :define_singleton_method
      self.define_singleton_method(:call, filter_call)
    else
      k.send(:define_method, :call, filter_call)
    end
  end
end

Private Instance Methods

apply_body_headers() click to toggle source
# File lib/azure/core/http/http_request.rb, line 160
def apply_body_headers
  return headers['Content-Length'] = '0' unless body

  return apply_io_headers        if IO === body || Tempfile === body
  return apply_string_io_headers if StringIO === body
  return apply_miscellaneous_headers
end
apply_io_headers() click to toggle source
# File lib/azure/core/http/http_request.rb, line 168
def apply_io_headers
  headers['Content-Length'] = body.size.to_s if body.respond_to?('size')
  if headers['Content-Length'].nil?
    raise ArgumentError, '\'Content-Length\' must be defined if size cannot be obtained from body IO.'
  end
  headers['Content-MD5'] = Digest::MD5.file(body.path).base64digest unless headers['Content-MD5']
end
apply_miscellaneous_headers() click to toggle source
# File lib/azure/core/http/http_request.rb, line 188
def apply_miscellaneous_headers
  headers['Content-Length'] = body.size.to_s
  headers['Content-MD5'] = Base64.strict_encode64(Digest::MD5.digest(body.to_s)) unless headers['Content-MD5']
end
apply_string_io_headers() click to toggle source
# File lib/azure/core/http/http_request.rb, line 176
def apply_string_io_headers
  headers['Content-Length'] = body.size.to_s
  unless headers['Content-MD5']
    headers['Content-MD5'] = Digest::MD5.new.tap do |checksum|
                               while chunk = body.read(5242880)
                                 checksum << chunk
                               end
                               body.rewind
                             end.base64digest
  end
end