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,39 +1,39 @@
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
- # Mixin to provide simple serialization / deserialization in AutoRest generated model classes
7
- module JSONable
8
- include MsRest::Serialization
9
-
10
- #
11
- # Serialize the object to JSON
12
- # @param options [Hash] hash map contains options to convert to json.
13
- # @return [String] JSON serialized version of the object
14
- #
15
- def to_json(options = nil)
16
- mapper = (options.nil? || !options.key?(:mapper))? self.class.mapper: options[:mapper]
17
- object = (options.nil? || !options.key?(:object))? self: options[:object]
18
- serialize(mapper, object)
19
- end
20
-
21
- #
22
- # Deserialize the object from JSON
23
- # @param json [String] JSON string representation of the object
24
- # @return [JSONable] object built from json input
25
- #
26
- def from_json(json, mapper = nil)
27
- mapper = self.class.mapper if mapper.nil?
28
- deserialize(mapper, json)
29
- end
30
-
31
- #
32
- # String representation of the object
33
- # @return [String]
34
- #
35
- def to_s
36
- "#{super} #{to_json.to_s}"
37
- end
38
- end
39
- 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
+ # Mixin to provide simple serialization / deserialization in AutoRest generated model classes
7
+ module JSONable
8
+ include MsRest::Serialization
9
+
10
+ #
11
+ # Serialize the object to JSON
12
+ # @param options [Hash] hash map contains options to convert to json.
13
+ # @return [String] JSON serialized version of the object
14
+ #
15
+ def to_json(options = nil)
16
+ mapper = (options.nil? || !options.key?(:mapper))? self.class.mapper: options[:mapper]
17
+ object = (options.nil? || !options.key?(:object))? self: options[:object]
18
+ serialize(mapper, object)
19
+ end
20
+
21
+ #
22
+ # Deserialize the object from JSON
23
+ # @param json [String] JSON string representation of the object
24
+ # @return [JSONable] object built from json input
25
+ #
26
+ def from_json(json, mapper = nil)
27
+ mapper = self.class.mapper if mapper.nil?
28
+ deserialize(mapper, json)
29
+ end
30
+
31
+ #
32
+ # String representation of the object
33
+ # @return [String]
34
+ #
35
+ def to_s
36
+ "#{super} #{to_json.to_s}"
37
+ end
38
+ end
39
+ end
@@ -1,11 +1,11 @@
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 an general exception for REST client.
8
- #
9
- class RestError < StandardError
10
- end
11
- 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 an general exception for REST client.
8
+ #
9
+ class RestError < StandardError
10
+ end
11
+ end
@@ -1,44 +1,44 @@
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 handles retry policy.
8
- #
9
- class RetryPolicyMiddleware < Faraday::Response::Middleware
10
- #
11
- # Initializes a new instance of the RetryPolicyMiddleware class.
12
- #
13
- def initialize(app, options = {})
14
- @times = options[:times] || 5
15
- @delay = options[:delay] || 0.01
16
-
17
- super(app)
18
- end
19
-
20
- #
21
- # Performs request and response processing.
22
- #
23
- def call(request_env)
24
- request_body = request_env[:body]
25
-
26
- begin
27
- request_env[:body] = request_body
28
-
29
- @app.call(request_env).on_complete do |response_env|
30
- status_code = response_env.status
31
-
32
- if @times > 0 && (status_code == 408 || (status_code >= 500 && status_code != 501 && status_code != 505))
33
- sleep @delay
34
- fail 'faraday_retry'
35
- end
36
- end
37
- rescue Exception => e
38
- raise e if e.message != 'faraday_retry'
39
- @times = @times - 1
40
- retry
41
- end
42
- end
43
- end
44
- 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 handles retry policy.
8
+ #
9
+ class RetryPolicyMiddleware < Faraday::Response::Middleware
10
+ #
11
+ # Initializes a new instance of the RetryPolicyMiddleware class.
12
+ #
13
+ def initialize(app, options = {})
14
+ @times = options[:times] || 5
15
+ @delay = options[:delay] || 0.01
16
+
17
+ super(app)
18
+ end
19
+
20
+ #
21
+ # Performs request and response processing.
22
+ #
23
+ def call(request_env)
24
+ request_body = request_env[:body]
25
+
26
+ begin
27
+ request_env[:body] = request_body
28
+
29
+ @app.call(request_env).on_complete do |response_env|
30
+ status_code = response_env.status
31
+
32
+ if @times > 0 && (status_code == 408 || (status_code >= 500 && status_code != 501 && status_code != 505))
33
+ sleep @delay
34
+ fail 'faraday_retry'
35
+ end
36
+ end
37
+ rescue Exception => e
38
+ raise e if e.message != 'faraday_retry'
39
+ @times = @times - 1
40
+ retry
41
+ end
42
+ end
43
+ end
44
+ end
@@ -60,7 +60,7 @@ module MsRest
60
60
  object_name = mapper[:serialized_name]
61
61
  mapper_type = mapper[:type][:name]
62
62
 
63
- if !mapper_type.match(/^(Number|Double|ByteArray|Boolean|Date|DateTime|DateTimeRfc1123|UnixTime|Enum|String|Object|Stream)$/i).nil?
63
+ if !mapper_type.match(/^(Number|Double|ByteArray|Boolean|Date|DateTime|DateTimeRfc1123|TimeSpan|UnixTime|Enum|String|Object|Stream)$/i).nil?
64
64
  payload = deserialize_primary_type(mapper, response_body)
65
65
  elsif !mapper_type.match(/^Dictionary$/i).nil?
66
66
  payload = deserialize_dictionary_type(mapper, response_body, object_name)
@@ -92,7 +92,7 @@ module MsRest
92
92
  result = Float(response_body) unless response_body.to_s.empty?
93
93
  when 'ByteArray'
94
94
  result = Base64.strict_decode64(response_body).unpack('C*') unless response_body.to_s.empty?
95
- when 'String', 'Boolean', 'Object', 'Stream'
95
+ when 'String', 'Boolean', 'Object', 'Stream', 'TimeSpan'
96
96
  result = response_body
97
97
  when 'Enum'
98
98
  unless response_body.nil?
@@ -229,7 +229,7 @@ module MsRest
229
229
 
230
230
  payload = Hash.new
231
231
  mapper_type = mapper[:type][:name]
232
- if !mapper_type.match(/^(Number|Double|ByteArray|Boolean|Date|DateTime|DateTimeRfc1123|UnixTime|Enum|String|Object|Stream)$/i).nil?
232
+ if !mapper_type.match(/^(Number|Double|ByteArray|Boolean|Date|DateTime|DateTimeRfc1123|TimeSpan|UnixTime|Enum|String|Object|Stream)$/i).nil?
233
233
  payload = serialize_primary_type(mapper, object)
234
234
  elsif !mapper_type.match(/^Dictionary$/i).nil?
235
235
  payload = serialize_dictionary_type(mapper, object, object_name)
@@ -289,7 +289,7 @@ module MsRest
289
289
  mapper_type = mapper[:type][:name]
290
290
  payload = nil
291
291
  case mapper_type
292
- when 'Number', 'Double', 'String', 'Date', 'Boolean', 'Object', 'Stream'
292
+ when 'Number', 'Double', 'String', 'Date', 'TimeSpan', 'Boolean', 'Object', 'Stream'
293
293
  payload = object != nil ? object : nil
294
294
  when 'Enum'
295
295
  unless object.nil?
@@ -1,115 +1,115 @@
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 a point of access to the REST API.
8
- #
9
- class ServiceClient
10
-
11
- # @return [MsRest::ServiceClientCredentials] the credentials object.
12
- attr_accessor :credentials
13
-
14
- # @return [Hash{String=>String}] default middlewares configuration for requests.
15
- attr_accessor :middlewares
16
-
17
- # @return [Hash{String=>String}] default request headers for requests.
18
- attr_accessor :request_headers
19
-
20
- # @return [Array] strings to be appended to the user agent in the request
21
- attr_accessor :user_agent_extended
22
-
23
- #
24
- # Creates and initialize new instance of the ServiceClient class.
25
- #
26
- # @param credentials [MsRest::ServiceClientCredentials] credentials to authorize
27
- # HTTP requests made by the service client.
28
- # @param options additional parameters for the HTTP request (not implemented yet).
29
- #
30
- def initialize(credentials = nil, options = nil)
31
- @credentials = credentials
32
- @request_headers = {}
33
- @middlewares = {middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]]}
34
- @user_agent_extended = []
35
- @user_agent_extended.push("ms_rest/#{MsRest::VERSION}")
36
- end
37
-
38
- #
39
- # @param base_url [String] the base url for the request.
40
- # @param method [Symbol] with any of the following values :get, :put, :post, :patch, :delete.
41
- # @param path [String] the path, relative to {base_url}.
42
- # @param options [Hash{String=>String}] specifying any request options like :credentials, :body, etc.
43
- # @return [Concurrent::Promise] Promise object which holds the HTTP response.
44
- #
45
- def make_request_async(base_url, method, path, options = {})
46
- options = @middlewares.merge(options)
47
- options[:credentials] = options[:credentials] || @credentials
48
- options[:user_agent_extended] = @user_agent_extended
49
- request = MsRest::HttpOperationRequest.new(base_url, path, method, options)
50
- promise = request.run_promise do |req|
51
- options[:credentials].sign_request(req) unless options[:credentials].nil?
52
- end
53
- promise = promise.then do |http_response|
54
- response_content = http_response.body.to_s.empty? ? nil : http_response.body
55
- # Create response
56
- create_response(request, http_response, response_content)
57
- end
58
- promise.execute
59
- end
60
-
61
- #
62
- # Add additional information into User-Agent header.
63
- # @param [String] additional_user_agent_information additional product information for user agent string.
64
- #
65
- # Example:
66
- # recommended format is Product/[version]
67
- # please refer https://github.com/Azure/azure-sdk-for-ruby/issues/517 for more information.
68
- #
69
- # add_user_agent_information('fog-azure-rm/0.2.0')
70
- #
71
- def add_user_agent_information(additional_user_agent_information)
72
- @user_agent_extended.push(additional_user_agent_information)
73
- end
74
-
75
- private
76
- #
77
- # Retrieves a new instance of the HttpOperationResponse class.
78
- # @param [MsRest::HttpOperationRequest] request the HTTP request object.
79
- # @param [Faraday::Response] response the HTTP response object.
80
- # @param [String] body the HTTP response body.
81
- # @return [MsRest::HttpOperationResponse] the operation response.
82
- #
83
- def create_response(request, http_response, body = nil)
84
- HttpOperationResponse.new(request, http_response, body)
85
- end
86
- end
87
-
88
- #
89
- # Hash of SSL options for Faraday connection. Default is nil.
90
- #
91
- @@ssl_options = {}
92
-
93
- #
94
- # Stores the SSL options to be used for Faraday connections.
95
- # ==== Examples
96
- # MsRest.use_ssl_cert # => Uses bundled certificate for all the connections
97
- # MsRest.use_ssl_cert({:ca_file => "path_to_ca_file"}) # => Uses supplied certificate for all the connections
98
- #
99
- # @param ssl_options [Hash] Hash of SSL options for Faraday connection. It defaults to the bundled certificate.
100
- #
101
- def self.use_ssl_cert(ssl_options = nil)
102
- if ssl_options.nil?
103
- @@ssl_options = {:ca_file => File.expand_path(File.join(File.dirname(__FILE__), '../..', 'ca-cert.pem')) }
104
- else
105
- @@ssl_options = ssl_options
106
- end
107
- end
108
-
109
- #
110
- # @return [Hash] Hash of SSL options to be used for Faraday connection.
111
- #
112
- def self.ssl_options
113
- @@ssl_options
114
- end
115
- 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 a point of access to the REST API.
8
+ #
9
+ class ServiceClient
10
+
11
+ # @return [MsRest::ServiceClientCredentials] the credentials object.
12
+ attr_accessor :credentials
13
+
14
+ # @return [Hash{String=>String}] default middlewares configuration for requests.
15
+ attr_accessor :middlewares
16
+
17
+ # @return [Hash{String=>String}] default request headers for requests.
18
+ attr_accessor :request_headers
19
+
20
+ # @return [Array] strings to be appended to the user agent in the request
21
+ attr_accessor :user_agent_extended
22
+
23
+ #
24
+ # Creates and initialize new instance of the ServiceClient class.
25
+ #
26
+ # @param credentials [MsRest::ServiceClientCredentials] credentials to authorize
27
+ # HTTP requests made by the service client.
28
+ # @param options additional parameters for the HTTP request (not implemented yet).
29
+ #
30
+ def initialize(credentials = nil, options = nil)
31
+ @credentials = credentials
32
+ @request_headers = {}
33
+ @middlewares = {middlewares: [[MsRest::RetryPolicyMiddleware, times: 3, retry: 0.02], [:cookie_jar]]}
34
+ @user_agent_extended = []
35
+ @user_agent_extended.push("ms_rest/#{MsRest::VERSION}")
36
+ end
37
+
38
+ #
39
+ # @param base_url [String] the base url for the request.
40
+ # @param method [Symbol] with any of the following values :get, :put, :post, :patch, :delete.
41
+ # @param path [String] the path, relative to {base_url}.
42
+ # @param options [Hash{String=>String}] specifying any request options like :credentials, :body, etc.
43
+ # @return [Concurrent::Promise] Promise object which holds the HTTP response.
44
+ #
45
+ def make_request_async(base_url, method, path, options = {})
46
+ options = @middlewares.merge(options)
47
+ options[:credentials] = options[:credentials] || @credentials
48
+ options[:user_agent_extended] = @user_agent_extended
49
+ request = MsRest::HttpOperationRequest.new(base_url, path, method, options)
50
+ promise = request.run_promise do |req|
51
+ options[:credentials].sign_request(req) unless options[:credentials].nil?
52
+ end
53
+ promise = promise.then do |http_response|
54
+ response_content = http_response.body.to_s.empty? ? nil : http_response.body
55
+ # Create response
56
+ create_response(request, http_response, response_content)
57
+ end
58
+ promise.execute
59
+ end
60
+
61
+ #
62
+ # Add additional information into User-Agent header.
63
+ # @param [String] additional_user_agent_information additional product information for user agent string.
64
+ #
65
+ # Example:
66
+ # recommended format is Product/[version]
67
+ # please refer https://github.com/Azure/azure-sdk-for-ruby/issues/517 for more information.
68
+ #
69
+ # add_user_agent_information('fog-azure-rm/0.2.0')
70
+ #
71
+ def add_user_agent_information(additional_user_agent_information)
72
+ @user_agent_extended.push(additional_user_agent_information)
73
+ end
74
+
75
+ private
76
+ #
77
+ # Retrieves a new instance of the HttpOperationResponse class.
78
+ # @param [MsRest::HttpOperationRequest] request the HTTP request object.
79
+ # @param [Faraday::Response] response the HTTP response object.
80
+ # @param [String] body the HTTP response body.
81
+ # @return [MsRest::HttpOperationResponse] the operation response.
82
+ #
83
+ def create_response(request, http_response, body = nil)
84
+ HttpOperationResponse.new(request, http_response, body)
85
+ end
86
+ end
87
+
88
+ #
89
+ # Hash of SSL options for Faraday connection. Default is nil.
90
+ #
91
+ @@ssl_options = {}
92
+
93
+ #
94
+ # Stores the SSL options to be used for Faraday connections.
95
+ # ==== Examples
96
+ # MsRest.use_ssl_cert # => Uses bundled certificate for all the connections
97
+ # MsRest.use_ssl_cert({:ca_file => "path_to_ca_file"}) # => Uses supplied certificate for all the connections
98
+ #
99
+ # @param ssl_options [Hash] Hash of SSL options for Faraday connection. It defaults to the bundled certificate.
100
+ #
101
+ def self.use_ssl_cert(ssl_options = nil)
102
+ if ssl_options.nil?
103
+ @@ssl_options = {:ca_file => File.expand_path(File.join(File.dirname(__FILE__), '../..', 'ca-cert.pem')) }
104
+ else
105
+ @@ssl_options = ssl_options
106
+ end
107
+ end
108
+
109
+ #
110
+ # @return [Hash] Hash of SSL options to be used for Faraday connection.
111
+ #
112
+ def self.ssl_options
113
+ @@ssl_options
114
+ end
115
+ end