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

Inherits:
Object
  • Object
show all
Defined in:
lib/azure/storage/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

Constructor Details

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

Public: Create the HttpRequest


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

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::Storage

  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)


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

def body
  @body
end

#clientAzure::Storage::Client

Azure storage client which contains configuration context and http agents


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

def client
  @client
end

#headersObject

The header values as a Hash


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

def headers
  @headers
end

#methodObject Also known as: _method

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


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

def method
  @method
end

#uriObject

The URI of the HTTP endpoint to query


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

def uri
  @uri
end

Instance Method Details

#callHttpResponse

Sends request to HTTP server and returns a HttpResponse


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

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


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

def default_headers(current_time)
  {}.tap do |def_headers|
    def_headers['User-Agent'] = Azure::Storage::Default::USER_AGENT
    def_headers['x-ms-date'] = current_time
    def_headers['x-ms-version'] = Azure::Storage::Default::STG_VERSION
    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


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

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


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

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