Class: Azure::Core::Http::HttpRequest
- Inherits:
-
Object
- Object
- Azure::Core::Http::HttpRequest
- 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
-
#body ⇒ Object
The body of the request (IO or String).
-
#client ⇒ Azure::Client
Azure client which contains configuration context and http agents.
-
#has_retry_filter ⇒ Object
The http filter.
-
#headers ⇒ Object
The header values as a Hash.
-
#method ⇒ Object
(also: #_method)
The HTTP method to use (:get, :post, :put, :delete, etc…).
-
#uri ⇒ Object
The URI of the HTTP endpoint to query.
Instance Method Summary collapse
-
#call ⇒ HttpResponse
Sends request to HTTP server and returns a HttpResponse.
-
#default_headers(current_time) ⇒ Object
Build a default headers Hash.
- #http_setup ⇒ Object
-
#initialize(method, uri, options_or_body = {}) ⇒ HttpRequest
constructor
Public: Create the HttpRequest.
-
#with_filter(filter = nil, options = {}, &block) ⇒ Object
Public: Applies a HttpFilter to the HTTP Pipeline.
Methods included from HttpResponseHelper
Constructor Details
#initialize(method, uri, options_or_body = {}) ⇒ HttpRequest
Public: Create the HttpRequest
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, = {}) ||= unless .is_a?(Hash) {body: } end || || {} @method = method @uri = if uri.is_a?(String) URI.parse(uri) else uri end @client = [:client] || Azure self.headers = default_headers([:current_time] || Time.now.httpdate).merge([:headers] || {}) self.body = [:body] end |
Instance Attribute Details
#body ⇒ Object
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 |
#client ⇒ Azure::Client
Azure client which contains configuration context and http agents
50 51 52 |
# File 'lib/azure/core/http/http_request.rb', line 50 def client @client end |
#has_retry_filter ⇒ Object
The http filter
53 54 55 |
# File 'lib/azure/core/http/http_request.rb', line 53 def has_retry_filter @has_retry_filter end |
#headers ⇒ Object
The header values as a Hash
43 44 45 |
# File 'lib/azure/core/http/http_request.rb', line 43 def headers @headers end |
#method ⇒ Object 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 |
#uri ⇒ Object
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
#call ⇒ HttpResponse
Sends request to HTTP server and returns a HttpResponse
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_setup ⇒ Object
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, ={}, &block) filter = filter || block if filter is_retry_policy = filter.is_a?(Azure::Core::Http::RetryPolicy) filter.retry_data[:request_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 |