Class: MsRest2::HttpOperationRequest
- Inherits:
-
Object
- Object
- MsRest2::HttpOperationRequest
- Defined in:
- lib/ms_rest2/http_operation_request.rb
Overview
Class which represents the data received and deserialized from server.
Instance Attribute Summary collapse
-
#base_uri ⇒ String
Base uri of the request.
-
#body ⇒ String
The HTTP response body.
-
#headers ⇒ Hash
Request headers.
-
#log ⇒ String
Full - to log requests, responses and bodies, partial - just requests and responses without body.
-
#method ⇒ String
Http request method.
-
#middlewares ⇒ Array
The list of middlewares to apply to the Request.
-
#path_params ⇒ Hash
Path parameters to be ERB::Util.url_encode encoded.
-
#path_template ⇒ String
Path template /replace/url_param.
-
#query_params ⇒ Hash
Query parameters to be ERB::Util.url_encode encoded.
-
#skip_encoding_path_params ⇒ Hash
Path parameters not to be ERB::Util.url_encode encoded.
-
#skip_encoding_query_params ⇒ Hash
Query parameters to be ERB::Util.url_encode encoded.
-
#user_agent_extended ⇒ Array
Strings to be appended to the user agent in the request.
Instance Method Summary collapse
-
#build_path ⇒ URI
Creates a path from the path template and the path_params and skip_encoding_path_params.
- #full_uri ⇒ Object
-
#initialize(base_uri, path_template, method, options = {}) ⇒ HttpOperationRequest
constructor
Creates and initialize new instance of the HttpOperationResponse class.
-
#run_promise(&block) ⇒ URI
Creates a promise which will execute the request.
- #to_json(*a) ⇒ Object
- #user_agent ⇒ Object
Constructor Details
#initialize(base_uri, path_template, method, options = {}) ⇒ HttpOperationRequest
Creates and initialize new instance of the HttpOperationResponse class.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ms_rest2/http_operation_request.rb', line 52 def initialize(base_uri, path_template, method, = {}) fail 'path_template must not be nil' if path_template.nil? fail 'method must not be nil' if method.nil? @base_uri = base_uri || '' @path_template = path_template @method = method @headers = {} @user_agent_extended = [] .each do |k,v| instance_variable_set("@#{k}", v) unless v.nil? end end |
Instance Attribute Details
#base_uri ⇒ String
Returns base uri of the request.
24 25 26 |
# File 'lib/ms_rest2/http_operation_request.rb', line 24 def base_uri @base_uri end |
#body ⇒ String
Returns the HTTP response body.
36 37 38 |
# File 'lib/ms_rest2/http_operation_request.rb', line 36 def body @body end |
#headers ⇒ Hash
Returns request headers.
30 31 32 |
# File 'lib/ms_rest2/http_operation_request.rb', line 30 def headers @headers end |
#log ⇒ String
Returns full - to log requests, responses and bodies, partial - just requests and responses without body.
42 43 44 |
# File 'lib/ms_rest2/http_operation_request.rb', line 42 def log @log end |
#method ⇒ String
Returns http request method.
33 34 35 |
# File 'lib/ms_rest2/http_operation_request.rb', line 33 def method @method end |
#middlewares ⇒ Array
Returns the list of middlewares to apply to the Request.
39 40 41 |
# File 'lib/ms_rest2/http_operation_request.rb', line 39 def middlewares @middlewares end |
#path_params ⇒ Hash
Returns path parameters to be ERB::Util.url_encode encoded.
12 13 14 |
# File 'lib/ms_rest2/http_operation_request.rb', line 12 def path_params @path_params end |
#path_template ⇒ String
Returns path template /replace/url_param.
27 28 29 |
# File 'lib/ms_rest2/http_operation_request.rb', line 27 def path_template @path_template end |
#query_params ⇒ Hash
Returns query parameters to be ERB::Util.url_encode encoded.
18 19 20 |
# File 'lib/ms_rest2/http_operation_request.rb', line 18 def query_params @query_params end |
#skip_encoding_path_params ⇒ Hash
Returns path parameters not to be ERB::Util.url_encode encoded.
15 16 17 |
# File 'lib/ms_rest2/http_operation_request.rb', line 15 def skip_encoding_path_params @skip_encoding_path_params end |
#skip_encoding_query_params ⇒ Hash
Returns query parameters to be ERB::Util.url_encode encoded.
21 22 23 |
# File 'lib/ms_rest2/http_operation_request.rb', line 21 def skip_encoding_query_params @skip_encoding_query_params end |
#user_agent_extended ⇒ Array
Returns strings to be appended to the user agent in the request.
45 46 47 |
# File 'lib/ms_rest2/http_operation_request.rb', line 45 def user_agent_extended @user_agent_extended end |
Instance Method Details
#build_path ⇒ URI
Creates a path from the path template and the path_params and skip_encoding_path_params
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/ms_rest2/http_operation_request.rb', line 98 def build_path template = path_template.dup path_params.each{ |key, value| template["{#{key}}"] = ERB::Util.url_encode(value) if template.include?("{#{key}}") } unless path_params.nil? skip_encoding_path_params.each{ |key, value| template["{#{key}}"] = value } unless skip_encoding_path_params.nil? path = URI.parse(template.gsub(/([^:]|\A)\/\//, '\1/')) unless skip_encoding_query_params.nil? path.query = [(path.query || ""), skip_encoding_query_params.reject{|_, v| v.nil?}.map{|k,v| "#{k}=#{v}"}].join('&') end path end |
#full_uri ⇒ Object
109 110 111 |
# File 'lib/ms_rest2/http_operation_request.rb', line 109 def full_uri URI.join(base_uri || '', build_path) end |
#run_promise(&block) ⇒ URI
Creates a promise which will execute the request. Block will yield the request for customization.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ms_rest2/http_operation_request.rb', line 69 def run_promise(&block) Concurrent::Promise.new do @connection ||= Faraday.new(:url => base_uri, :ssl => MsRest2.) do |faraday| middlewares.each{ |args| faraday.use(*args) } unless middlewares.nil? faraday.adapter Faraday.default_adapter logging = ENV['AZURE_HTTP_LOGGING'] || log if logging faraday.response :logger, nil, { :bodies => logging == 'full' } end end loop do @response = @connection.run_request(:"#{method}", build_path, body, {'User-Agent' => user_agent}.merge(headers)) do |req| req.params = req.params.merge(query_params.reject{|_, v| v.nil?}) unless query_params.nil? yield(req) if block_given? end break if ((@response.status != 429) || (@response.status == 429 && @response.headers['retry-after'].nil?)) if(@response.status == 429 && !@response.headers['retry-after'].nil?) sleep(@response.headers['retry-after'].to_i) end end @response end end |
#to_json(*a) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/ms_rest2/http_operation_request.rb', line 117 def to_json(*a) { base_uri: base_uri, path_template: path_template, method: method, path_params: path_params, skip_encoding_path_params: skip_encoding_path_params, query_params: query_params, skip_encoding_query_params: skip_encoding_query_params, headers: headers, body: body, middlewares: middlewares, log: log }.to_json(*a) end |
#user_agent ⇒ Object
113 114 115 |
# File 'lib/ms_rest2/http_operation_request.rb', line 113 def user_agent "Ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM}) #{user_agent_extended.join(' ')}" end |