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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/azure/core/http/http_request.rb', line 55

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)



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

def body
  @body
end

#clientAzure::Client

Azure client which contains configuration context and http agents



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

def client
  @client
end

#headersObject

The header values as a Hash



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

def headers
  @headers
end

#methodObject Also known as: _method

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



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

def method
  @method
end

#uriObject

The URI of the HTTP endpoint to query



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

def uri
  @uri
end

Instance Method Details

#callHttpResponse

Sends request to HTTP server and returns a HttpResponse



143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/azure/core/http/http_request.rb', line 143

def call
  conn = http_setup
  res = conn.run_request(method.to_sym, uri, nil, nil) do |req|
    req.body = body if body
    req.headers = headers if headers
  end

  response = HttpResponse.new(res)
  response.uri = uri
  raise response.error unless response.success?
  response
end

#default_headers(current_time) ⇒ Object

Build a default headers Hash



113
114
115
116
117
118
119
120
121
122
# File 'lib/azure/core/http/http_request.rb', line 113

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'] = '2.0;NetFx'
    def_headers['Content-Type'] = 'application/atom+xml; charset=utf-8'
  end
end

#http_setupObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/azure/core/http/http_request.rb', line 124

def http_setup
  http = @client.agents(uri)

  unless headers.nil?
    keep_alive = headers['Keep-Alive'] || headers['keep-alive']
    http.read_timeout = keep_alive.split('=').last.to_i unless keep_alive.nil?
  end

  http
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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/azure/core/http/http_request.rb', line 92

def with_filter(filter=nil, &block)
  filter = filter || block
  if filter
    old_impl = self._method(:call)

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