Class: Azure::Core::Http::HttpRequest

Inherits:
Object
  • Object
show all
Includes:
HttpResponseHelper
Defined in:
lib/azure/core/http/http_request.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HttpResponseHelper

#set_up_response

Constructor Details

#initialize(method, uri, options_or_body = {}) ⇒ HttpRequest

Public: Create the HttpRequest

Parameters:

  • method (Symbol)

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

  • uri (URI)

    The URI of the HTTP endpoint to query

  • options_or_body (Hash|IO|String) (defaults to: {})

    The request options including :body or raw body only



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/azure/core/http/http_request.rb', line 59

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

Instance Attribute Details

#bodyObject

The body of the request (IO or String)



45
46
47
# File 'lib/azure/core/http/http_request.rb', line 45

def body
  @body
end

#clientAzure::Client

Azure client which contains configuration context and http agents

Returns:

  • (Azure::Client)


49
50
51
# File 'lib/azure/core/http/http_request.rb', line 49

def client
  @client
end

#has_retry_filterObject

The http filter



52
53
54
# File 'lib/azure/core/http/http_request.rb', line 52

def has_retry_filter
  @has_retry_filter
end

#headersObject

The header values as a Hash



42
43
44
# File 'lib/azure/core/http/http_request.rb', line 42

def headers
  @headers
end

#methodObject Also known as: _method

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



36
37
38
# File 'lib/azure/core/http/http_request.rb', line 36

def method
  @method
end

#uriObject

The URI of the HTTP endpoint to query



39
40
41
# File 'lib/azure/core/http/http_request.rb', line 39

def uri
  @uri
end

Instance Method Details

#callHttpResponse

Sends request to HTTP server and returns a HttpResponse

Returns:



141
142
143
144
145
146
147
148
149
# File 'lib/azure/core/http/http_request.rb', line 141

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) ⇒ Object

Build a default headers Hash



118
119
120
121
122
123
124
125
126
127
# File 'lib/azure/core/http/http_request.rb', line 118

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_setupObject



129
130
131
# File 'lib/azure/core/http/http_request.rb', line 129

def http_setup
  @client.agents(uri)
end

#with_filter(filter = nil, &block) ⇒ Object

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)

&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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/azure/core/http/http_request.rb', line 96

def with_filter(filter=nil, &block)
  filter = filter || block
  if filter
    @has_retry_filter ||= filter.is_a?(Azure::Core::Http::RetryPolicy)
    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