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

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(method, uri, body = nil, current_time = Time.now.httpdate) ⇒ HttpRequest

Public: Create the HttpRequest

method - Symbol. The HTTP method to use (:get, :post, :put, :del, etc…) uri - URI. The URI of the HTTP endpoint to query body - IO or String. The request body (optional) current_time - String. The current time as a HTTP date string



50
51
52
53
54
55
56
57
# File 'lib/azure/core/http/http_request.rb', line 50

def initialize(method, uri, body=nil, current_time=Time.now.httpdate)
  @method  = method
  @uri     = uri
  @body    = body
  @headers = {}

  default_headers current_time
end

Instance Attribute Details

#bodyObject

The body of the request (IO or String)



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

def body
  @body
end

#headersObject

The header values as a Hash



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

def headers
  @headers
end

#methodObject Also known as: _method

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



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

def method
  @method
end

#uriObject

The URI of the HTTP endpoint to query



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

def uri
  @uri
end

Instance Method Details

#callObject

Public: Sends request to HTTP server and returns a HttpResponse

Returns a HttpResponse



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/azure/core/http/http_request.rb', line 126

def call
  request = http_request_class.new(uri.request_uri, headers)
  request.body = body if body

  http = nil
  if ENV['HTTP_PROXY'] || ENV['HTTPS_PROXY']
    if ENV['HTTP_PROXY']
      proxy_uri = URI::parse(ENV['HTTP_PROXY'])
    else
      proxy_uri = URI::parse(ENV['HTTPS_PROXY'])
    end

    http = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port).new(uri.host, uri.port)
  else
    http = Net::HTTP.new(uri.host, uri.port)
  end

  if uri.scheme.downcase == 'https'
    # require 'net/https'
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

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

#default_headers(current_time) ⇒ Object

Build a default headers Hash



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 95

def default_headers(current_time)
  headers["User-Agent"] = "Azure-SDK-for-Ruby/" + Azure::Version.to_s
  headers["x-ms-date"] = current_time
  headers["x-ms-version"] = "2012-02-12"
  headers["DataServiceVersion"] = "1.0;NetFx"
  headers["MaxDataServiceVersion"] = "2.0;NetFx"

  if body
    headers["Content-Type"]   = "application/atom+xml; charset=utf-8"
    headers["Content-Length"] = body.bytesize.to_s
    headers["Content-MD5"]    = Base64.strict_encode64(Digest::MD5.digest(body))
  else
    headers["Content-Length"] = "0"
    headers["Content-Type"] = ""
  end
end

#http_request_classObject

Obtain the Net::HTTP class that will handle this request

Returns a subclass of Net::HTTPRequest



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

def http_request_class
  Net::HTTP.const_get(method.to_s.capitalize)
rescue NameError => e
  e.message = "#{method} is an invalid HTTP method"
  raise
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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/azure/core/http/http_request.rb', line 78

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