ms_rest 0.7.4 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,72 +1,72 @@
1
- # encoding: utf-8
2
- # Copyright (c) Microsoft Corporation. All rights reserved.
3
- # Licensed under the MIT License. See License.txt in the project root for license information.
4
- require 'json'
5
-
6
- module MsRest
7
- #
8
- # Class which represents an error meaning that either HTTP request or HTTP response was invalid.
9
- #
10
- class HttpOperationError < RestError
11
-
12
- # @return [Hash] the HTTP request data (uri, body, headers).
13
- attr_accessor :request
14
-
15
- # @return [Faraday::Response] the HTTP response object.
16
- attr_accessor :response
17
-
18
- # @return [String] the HTTP response body.
19
- attr_accessor :body
20
-
21
- #
22
- # Creates and initialize new instance of the HttpOperationException class.
23
- # @param [Hash] the HTTP request data (uri, body, headers).
24
- # @param [Faraday::Response] the HTTP response object.
25
- # @param [String] body the HTTP response body.
26
- # @param [String] error message.
27
- #
28
- def initialize(*args)
29
- @msg = self.class.name
30
- if args.size == 1
31
- # When only message is provided.
32
- @msg = args[0]
33
- super(args[0])
34
- elsif args.size == 2
35
- # When only request and response provided, body is nil.
36
- @request = args[0]
37
- @response = args[1]
38
- @body = nil
39
- super()
40
- elsif args.size == 3
41
- # When request, response and body were provided.
42
- @request = args[0]
43
- @response = args[1]
44
- @body = args[2]
45
- super()
46
- elsif args.size == 4
47
- # When request, response, body and message were provided.
48
- @request = args[0]
49
- @response = args[1]
50
- @body = args[2]
51
- @msg = args[3]
52
- super(args[3])
53
- else
54
- fail ArgumentError, 'Invalid number of arguments was provided, valid number: 1, 2, 3 or 4'
55
- end
56
- end
57
-
58
- def to_json(*a)
59
- res_dict = response ? { body: response.body, headers: response.headers, status: response.status } : nil
60
- {message: @msg, request: request, response: res_dict}.to_json(*a)
61
- end
62
-
63
- def to_s
64
- begin
65
- JSON.pretty_generate(self)
66
- rescue Exception => ex
67
- "#{self.class.name} failed in \n\t#{backtrace.join("\n\t")}"
68
- end
69
- end
70
- end
71
-
72
- end
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+ require 'json'
5
+
6
+ module MsRest
7
+ #
8
+ # Class which represents an error meaning that either HTTP request or HTTP response was invalid.
9
+ #
10
+ class HttpOperationError < RestError
11
+
12
+ # @return [Hash] the HTTP request data (uri, body, headers).
13
+ attr_accessor :request
14
+
15
+ # @return [Faraday::Response] the HTTP response object.
16
+ attr_accessor :response
17
+
18
+ # @return [String] the HTTP response body.
19
+ attr_accessor :body
20
+
21
+ #
22
+ # Creates and initialize new instance of the HttpOperationException class.
23
+ # @param [Hash] the HTTP request data (uri, body, headers).
24
+ # @param [Faraday::Response] the HTTP response object.
25
+ # @param [String] body the HTTP response body.
26
+ # @param [String] error message.
27
+ #
28
+ def initialize(*args)
29
+ @msg = self.class.name
30
+ if args.size == 1
31
+ # When only message is provided.
32
+ @msg = args[0]
33
+ super(args[0])
34
+ elsif args.size == 2
35
+ # When only request and response provided, body is nil.
36
+ @request = args[0]
37
+ @response = args[1]
38
+ @body = nil
39
+ super()
40
+ elsif args.size == 3
41
+ # When request, response and body were provided.
42
+ @request = args[0]
43
+ @response = args[1]
44
+ @body = args[2]
45
+ super()
46
+ elsif args.size == 4
47
+ # When request, response, body and message were provided.
48
+ @request = args[0]
49
+ @response = args[1]
50
+ @body = args[2]
51
+ @msg = args[3]
52
+ super(args[3])
53
+ else
54
+ fail ArgumentError, 'Invalid number of arguments was provided, valid number: 1, 2, 3 or 4'
55
+ end
56
+ end
57
+
58
+ def to_json(*a)
59
+ res_dict = response ? { body: response.body, headers: response.headers, status: response.status } : nil
60
+ {message: @msg, request: request, response: res_dict}.to_json(*a)
61
+ end
62
+
63
+ def to_s
64
+ begin
65
+ JSON.pretty_generate(self)
66
+ rescue Exception => ex
67
+ "#{self.class.name} failed in \n\t#{backtrace.join("\n\t")}"
68
+ end
69
+ end
70
+ end
71
+
72
+ end
@@ -1,135 +1,135 @@
1
- # encoding: utf-8
2
- # Copyright (c) Microsoft Corporation. All rights reserved.
3
- # Licensed under the MIT License. See License.txt in the project root for license information.
4
-
5
- module MsRest
6
- #
7
- # Class which represents the data received and deserialized from server.
8
- #
9
- class HttpOperationRequest
10
-
11
- # @return [Hash] path parameters to be ERB::Util.url_encode encoded
12
- attr_accessor :path_params
13
-
14
- # @return [Hash] path parameters not to be ERB::Util.url_encode encoded
15
- attr_accessor :skip_encoding_path_params
16
-
17
- # @return [Hash] query parameters to be ERB::Util.url_encode encoded
18
- attr_accessor :query_params
19
-
20
- # @return [Hash] query parameters to be ERB::Util.url_encode encoded
21
- attr_accessor :skip_encoding_query_params
22
-
23
- # @return [String] base uri of the request
24
- attr_accessor :base_uri
25
-
26
- # @return [String] path template /{replace}/{url_param}
27
- attr_accessor :path_template
28
-
29
- # @return [Hash] request headers
30
- attr_accessor :headers
31
-
32
- # @return [String] http request method
33
- attr_accessor :method
34
-
35
- # @return [String] the HTTP response body.
36
- attr_accessor :body
37
-
38
- # @return [Array] the list of middlewares to apply to the Request
39
- attr_accessor :middlewares
40
-
41
- # @return [String] full - to log requests, responses and bodies, partial - just requests and responses without body
42
- attr_accessor :log
43
-
44
- # @return [Array] strings to be appended to the user agent in the request
45
- attr_accessor :user_agent_extended
46
-
47
- # Creates and initialize new instance of the HttpOperationResponse class.
48
- # @param [String|URI] base uri for requests
49
- # @param [String] path template /{replace}/{url_param}
50
- # @param [String] http method for the request
51
- # @param [Hash] body the HTTP response body.
52
- def initialize(base_uri, path_template, method, options = {})
53
- fail 'path_template must not be nil' if path_template.nil?
54
- fail 'method must not be nil' if method.nil?
55
-
56
- @base_uri = base_uri || ''
57
- @path_template = path_template
58
- @method = method
59
- @headers = {}
60
- @user_agent_extended = []
61
-
62
- options.each do |k,v|
63
- instance_variable_set("@#{k}", v) unless v.nil?
64
- end
65
- end
66
-
67
- # Creates a promise which will execute the request. Block will yield the request for customization.
68
- # @return [URI] body the HTTP response body.
69
- def run_promise(&block)
70
- Concurrent::Promise.new do
71
- @connection ||= Faraday.new(:url => base_uri, :ssl => MsRest.ssl_options) do |faraday|
72
- middlewares.each{ |args| faraday.use(*args) } unless middlewares.nil?
73
- faraday.adapter Faraday.default_adapter
74
- logging = ENV['AZURE_HTTP_LOGGING'] || log
75
- if logging
76
- faraday.response :logger, nil, { :bodies => logging == 'full' }
77
- end
78
- end
79
-
80
- loop do
81
- @response = @connection.run_request(:"#{method}", build_path, body, {'User-Agent' => user_agent}.merge(headers)) do |req|
82
- req.params = req.params.merge(query_params.reject{|_, v| v.nil?}) unless query_params.nil?
83
- yield(req) if block_given?
84
- end
85
-
86
- break if ((@response.status != 429) || (@response.status == 429 && @response.headers['retry-after'].nil?))
87
-
88
- if(@response.status == 429 && !@response.headers['retry-after'].nil?)
89
- sleep(@response.headers['retry-after'].to_i)
90
- end
91
- end
92
- @response
93
- end
94
- end
95
-
96
- # Creates a path from the path template and the path_params and skip_encoding_path_params
97
- # @return [URI] body the HTTP response body.
98
- def build_path
99
- template = path_template.dup
100
- path_params.each{ |key, value| template["{#{key}}"] = ERB::Util.url_encode(value) if template.include?("{#{key}}") } unless path_params.nil?
101
- skip_encoding_path_params.each{ |key, value| template["{#{key}}"] = value } unless skip_encoding_path_params.nil?
102
- path = URI.parse(template.gsub(/([^:]|\A)\/\//, '\1/'))
103
- unless skip_encoding_query_params.nil?
104
- path.query = [(path.query || ""), skip_encoding_query_params.reject{|_, v| v.nil?}.map{|k,v| "#{k}=#{v}"}].join('&')
105
- end
106
- path
107
- end
108
-
109
- def full_uri
110
- URI.join(base_uri || '', build_path)
111
- end
112
-
113
- def user_agent
114
- "Ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM}) #{user_agent_extended.join(' ')}"
115
- end
116
-
117
- def to_json(*a)
118
- {
119
- base_uri: base_uri,
120
- path_template: path_template,
121
- method: method,
122
- path_params: path_params,
123
- skip_encoding_path_params: skip_encoding_path_params,
124
- query_params: query_params,
125
- skip_encoding_query_params: skip_encoding_query_params,
126
- headers: headers,
127
- body: body,
128
- middlewares: middlewares,
129
- log: log
130
- }.to_json(*a)
131
- end
132
-
133
- end
134
-
135
- end
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRest
6
+ #
7
+ # Class which represents the data received and deserialized from server.
8
+ #
9
+ class HttpOperationRequest
10
+
11
+ # @return [Hash] path parameters to be ERB::Util.url_encode encoded
12
+ attr_accessor :path_params
13
+
14
+ # @return [Hash] path parameters not to be ERB::Util.url_encode encoded
15
+ attr_accessor :skip_encoding_path_params
16
+
17
+ # @return [Hash] query parameters to be ERB::Util.url_encode encoded
18
+ attr_accessor :query_params
19
+
20
+ # @return [Hash] query parameters to be ERB::Util.url_encode encoded
21
+ attr_accessor :skip_encoding_query_params
22
+
23
+ # @return [String] base uri of the request
24
+ attr_accessor :base_uri
25
+
26
+ # @return [String] path template /{replace}/{url_param}
27
+ attr_accessor :path_template
28
+
29
+ # @return [Hash] request headers
30
+ attr_accessor :headers
31
+
32
+ # @return [String] http request method
33
+ attr_accessor :method
34
+
35
+ # @return [String] the HTTP response body.
36
+ attr_accessor :body
37
+
38
+ # @return [Array] the list of middlewares to apply to the Request
39
+ attr_accessor :middlewares
40
+
41
+ # @return [String] full - to log requests, responses and bodies, partial - just requests and responses without body
42
+ attr_accessor :log
43
+
44
+ # @return [Array] strings to be appended to the user agent in the request
45
+ attr_accessor :user_agent_extended
46
+
47
+ # Creates and initialize new instance of the HttpOperationResponse class.
48
+ # @param [String|URI] base uri for requests
49
+ # @param [String] path template /{replace}/{url_param}
50
+ # @param [String] http method for the request
51
+ # @param [Hash] body the HTTP response body.
52
+ def initialize(base_uri, path_template, method, options = {})
53
+ fail 'path_template must not be nil' if path_template.nil?
54
+ fail 'method must not be nil' if method.nil?
55
+
56
+ @base_uri = base_uri || ''
57
+ @path_template = path_template
58
+ @method = method
59
+ @headers = {}
60
+ @user_agent_extended = []
61
+
62
+ options.each do |k,v|
63
+ instance_variable_set("@#{k}", v) unless v.nil?
64
+ end
65
+ end
66
+
67
+ # Creates a promise which will execute the request. Block will yield the request for customization.
68
+ # @return [URI] body the HTTP response body.
69
+ def run_promise(&block)
70
+ Concurrent::Promise.new do
71
+ @connection ||= Faraday.new(:url => base_uri, :ssl => MsRest.ssl_options) do |faraday|
72
+ middlewares.each{ |args| faraday.use(*args) } unless middlewares.nil?
73
+ faraday.adapter Faraday.default_adapter
74
+ logging = ENV['AZURE_HTTP_LOGGING'] || log
75
+ if logging
76
+ faraday.response :logger, nil, { :bodies => logging == 'full' }
77
+ end
78
+ end
79
+
80
+ loop do
81
+ @response = @connection.run_request(:"#{method}", build_path, body, {'User-Agent' => user_agent}.merge(headers)) do |req|
82
+ req.params = req.params.merge(query_params.reject{|_, v| v.nil?}) unless query_params.nil?
83
+ yield(req) if block_given?
84
+ end
85
+
86
+ break if ((@response.status != 429) || (@response.status == 429 && @response.headers['retry-after'].nil?))
87
+
88
+ if(@response.status == 429 && !@response.headers['retry-after'].nil?)
89
+ sleep(@response.headers['retry-after'].to_i)
90
+ end
91
+ end
92
+ @response
93
+ end
94
+ end
95
+
96
+ # Creates a path from the path template and the path_params and skip_encoding_path_params
97
+ # @return [URI] body the HTTP response body.
98
+ def build_path
99
+ template = path_template.dup
100
+ path_params.each{ |key, value| template["{#{key}}"] = ERB::Util.url_encode(value) if template.include?("{#{key}}") } unless path_params.nil?
101
+ skip_encoding_path_params.each{ |key, value| template["{#{key}}"] = value } unless skip_encoding_path_params.nil?
102
+ path = URI.parse(template.gsub(/([^:]|\A)\/\//, '\1/'))
103
+ unless skip_encoding_query_params.nil?
104
+ path.query = [(path.query || ""), skip_encoding_query_params.reject{|_, v| v.nil?}.map{|k,v| "#{k}=#{v}"}].join('&')
105
+ end
106
+ path
107
+ end
108
+
109
+ def full_uri
110
+ URI.join(base_uri || '', build_path)
111
+ end
112
+
113
+ def user_agent
114
+ "Ruby/#{RUBY_VERSION} (#{RUBY_PLATFORM}) #{user_agent_extended.join(' ')}"
115
+ end
116
+
117
+ def to_json(*a)
118
+ {
119
+ base_uri: base_uri,
120
+ path_template: path_template,
121
+ method: method,
122
+ path_params: path_params,
123
+ skip_encoding_path_params: skip_encoding_path_params,
124
+ query_params: query_params,
125
+ skip_encoding_query_params: skip_encoding_query_params,
126
+ headers: headers,
127
+ body: body,
128
+ middlewares: middlewares,
129
+ log: log
130
+ }.to_json(*a)
131
+ end
132
+
133
+ end
134
+
135
+ end
@@ -1,37 +1,37 @@
1
- # encoding: utf-8
2
- # Copyright (c) Microsoft Corporation. All rights reserved.
3
- # Licensed under the MIT License. See License.txt in the project root for license information.
4
-
5
- module MsRest
6
- #
7
- # Class which represents the data received and deserialized from server.
8
- #
9
- class HttpOperationResponse
10
-
11
- # @param [MsRest::HttpOperationRequest] the HTTP request data.
12
- attr_accessor :request
13
-
14
- # @return [Faraday::Response] the HTTP response object.
15
- attr_accessor :response
16
-
17
- # @return [String] the HTTP response body.
18
- attr_accessor :body
19
-
20
- #
21
- # Creates and initialize new instance of the HttpOperationResponse class.
22
- # @param [MsRest::HttpOperationRequest] request the HTTP request object.
23
- # @param [Faraday::Response] response the HTTP response object.
24
- # @param [String] body the HTTP response body.
25
- def initialize(request, response, body = nil)
26
- @request = request
27
- @response = response
28
- @body = body
29
- end
30
-
31
- def to_json(*a)
32
- res_dict = response ? { body: response.body, headers: response.headers, status: response.status } : nil
33
- {response: res_dict, request: request}.to_json(*a)
34
- end
35
-
36
- end
37
- end
1
+ # encoding: utf-8
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # Licensed under the MIT License. See License.txt in the project root for license information.
4
+
5
+ module MsRest
6
+ #
7
+ # Class which represents the data received and deserialized from server.
8
+ #
9
+ class HttpOperationResponse
10
+
11
+ # @param [MsRest::HttpOperationRequest] the HTTP request data.
12
+ attr_accessor :request
13
+
14
+ # @return [Faraday::Response] the HTTP response object.
15
+ attr_accessor :response
16
+
17
+ # @return [String] the HTTP response body.
18
+ attr_accessor :body
19
+
20
+ #
21
+ # Creates and initialize new instance of the HttpOperationResponse class.
22
+ # @param [MsRest::HttpOperationRequest] request the HTTP request object.
23
+ # @param [Faraday::Response] response the HTTP response object.
24
+ # @param [String] body the HTTP response body.
25
+ def initialize(request, response, body = nil)
26
+ @request = request
27
+ @response = response
28
+ @body = body
29
+ end
30
+
31
+ def to_json(*a)
32
+ res_dict = response ? { body: response.body, headers: response.headers, status: response.status } : nil
33
+ {response: res_dict, request: request}.to_json(*a)
34
+ end
35
+
36
+ end
37
+ end