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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

Instance Attribute Details

#bodyObject

The body of the request (IO or String)



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

def body
  @body
end

#clientAzure::Client

Azure client which contains configuration context and http agents

Returns:

  • (Azure::Client)


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

def client
  @client
end

#has_retry_filterObject

The http filter



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

def has_retry_filter
  @has_retry_filter
end

#headersObject

The header values as a Hash



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

def headers
  @headers
end

#methodObject Also known as: _method

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



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

def method
  @method
end

#uriObject

The URI of the HTTP endpoint to query



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

def uri
  @uri
end

Instance Method Details

#callHttpResponse

Sends request to HTTP server and returns a HttpResponse

Returns:



148
149
150
151
152
153
154
155
156
# 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) ⇒ Object

Build a default headers Hash



125
126
127
128
129
130
131
132
133
134
# 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_setupObject



136
137
138
# File 'lib/azure/core/http/http_request.rb', line 136

def http_setup
  @client.agents(uri)
end

#with_filter(filter = nil, options = {}, &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)

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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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