ms_rest2 0.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +52 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/ca-cert.pem +3865 -0
- data/lib/ms_rest/credentials/basic_authentication_credentials.rb +62 -0
- data/lib/ms_rest/credentials/service_client_credentials.rb +22 -0
- data/lib/ms_rest/credentials/string_token_provider.rb +41 -0
- data/lib/ms_rest/credentials/token_credentials.rb +61 -0
- data/lib/ms_rest/credentials/token_provider.rb +19 -0
- data/lib/ms_rest/deserialization_error.rb +43 -0
- data/lib/ms_rest/http_operation_error.rb +72 -0
- data/lib/ms_rest/http_operation_request.rb +135 -0
- data/lib/ms_rest/http_operation_response.rb +37 -0
- data/lib/ms_rest/jsonable.rb +39 -0
- data/lib/ms_rest/rest_error.rb +11 -0
- data/lib/ms_rest/retry_policy_middleware.rb +44 -0
- data/lib/ms_rest/serialization.rb +483 -0
- data/lib/ms_rest/service_client.rb +115 -0
- data/lib/ms_rest/validation_error.rb +11 -0
- data/lib/ms_rest/version.rb +7 -0
- data/lib/ms_rest.rb +29 -0
- metadata +158 -0
@@ -0,0 +1,62 @@
|
|
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 keeps functionality and data for performing HTTP basic authentication.
|
8
|
+
|
9
|
+
#
|
10
|
+
class BasicAuthenticationCredentials < ServiceClientCredentials
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
DEFAULT_SCHEME = 'Basic'
|
15
|
+
|
16
|
+
# @return [String] the scheme for composing credentials.
|
17
|
+
attr_accessor :scheme
|
18
|
+
|
19
|
+
# @return [String] the username for authentication.
|
20
|
+
attr_accessor :user_name
|
21
|
+
|
22
|
+
# @return [String] password for authentication.
|
23
|
+
attr_accessor :password
|
24
|
+
|
25
|
+
public
|
26
|
+
|
27
|
+
#
|
28
|
+
# Creates and initialize new instance of the BasicAuthenticationCredentials class.
|
29
|
+
# @param user_name [String] the username for authentication.
|
30
|
+
# @param password [String] the password for authentication.
|
31
|
+
# @param scheme = DEFAULT_SCHEME [String] the scheme for composing credentials.
|
32
|
+
def initialize(user_name, password, scheme = DEFAULT_SCHEME)
|
33
|
+
fail ArgumentError, 'user_name cannot be nil' if user_name.nil?
|
34
|
+
fail ArgumentError, 'password cannot be nil' if password.nil?
|
35
|
+
fail ArgumentError, 'scheme cannot be nil' if scheme.nil?
|
36
|
+
|
37
|
+
@user_name = user_name
|
38
|
+
@password = password
|
39
|
+
@scheme = scheme
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Attaches basic authentication header to the given HTTP request.
|
44
|
+
# @param request [Net::HTTPRequest] the request authentication header needs to be attached to.
|
45
|
+
#
|
46
|
+
# @return [Net::HTTPRequest] request with attached authentication header.
|
47
|
+
def sign_request(request)
|
48
|
+
super(request)
|
49
|
+
encodeCredentials = Base64.strict_encode64("#{user_name}:#{password}")
|
50
|
+
credentials = "#{scheme} #{encodeCredentials}"
|
51
|
+
|
52
|
+
if (request.respond_to?(:request_headers))
|
53
|
+
request.request_headers[AUTHORIZATION] = credentials
|
54
|
+
elsif request.respond_to?(:headers)
|
55
|
+
request.headers[AUTHORIZATION] = credentials
|
56
|
+
else
|
57
|
+
fail ArgumentError, 'Incorrect request object was provided'
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,22 @@
|
|
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 that serves as a base for all authentications classes.
|
8
|
+
#
|
9
|
+
class ServiceClientCredentials
|
10
|
+
|
11
|
+
AUTHORIZATION = 'authorization'
|
12
|
+
|
13
|
+
#
|
14
|
+
# Base method for performing authentication of HTTP requests.
|
15
|
+
# @param request [Net::HTTPRequest] HTTP request to authenticate
|
16
|
+
#
|
17
|
+
# @return [Net::HTTPRequest] authenticated HTTP request
|
18
|
+
def sign_request(request)
|
19
|
+
fail ArgumentError, 'request is nil.' if request.nil?
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,41 @@
|
|
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 that provides access to authentication token.
|
8
|
+
#
|
9
|
+
class StringTokenProvider < TokenProvider
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
# @return [String] the access token.
|
14
|
+
attr_accessor :token
|
15
|
+
|
16
|
+
# @return [String] the token type.
|
17
|
+
attr_accessor :token_type
|
18
|
+
|
19
|
+
public
|
20
|
+
|
21
|
+
#
|
22
|
+
# Creates and initalizes a new instance of StringTokenProvider class.
|
23
|
+
|
24
|
+
# @param token [String] the access token.
|
25
|
+
# @param token_type [String] the token type.
|
26
|
+
#
|
27
|
+
def initialize(token, token_type = TokenCredentials::DEFAULT_SCHEME)
|
28
|
+
@token = token
|
29
|
+
@token_type = token_type
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Returns the string value which needs to be attached
|
34
|
+
# to HTTP request header in order to be authorized.
|
35
|
+
#
|
36
|
+
# @return [String] authentication headers.
|
37
|
+
def get_authentication_header
|
38
|
+
"#{token_type} #{token}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,61 @@
|
|
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 keeps functionality and date for performing OAuth (token based) authentication.
|
8
|
+
#
|
9
|
+
class TokenCredentials < ServiceClientCredentials
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
DEFAULT_SCHEME = 'Bearer'
|
14
|
+
|
15
|
+
# @return [String] the scheme for arranging token in the HTTP header.
|
16
|
+
attr_accessor :token_provider
|
17
|
+
|
18
|
+
public
|
19
|
+
|
20
|
+
#
|
21
|
+
# Creates and initialize new instance of the TokenCredentials class.
|
22
|
+
# @param token_provider [TokenProvider] the token provider.
|
23
|
+
# @param token [String] the token.
|
24
|
+
def initialize(*args)
|
25
|
+
if (args.size == 1)
|
26
|
+
if args[0].respond_to? :get_authentication_header
|
27
|
+
@token_provider = args[0]
|
28
|
+
elsif args[0].is_a? String
|
29
|
+
@token_provider = StringTokenProvider.new args[0], DEFAULT_SCHEME
|
30
|
+
else
|
31
|
+
fail ArgumentError, 'Invalid argument was passed, is can be either TokenProvider or token'
|
32
|
+
end
|
33
|
+
elsif (args.size == 2)
|
34
|
+
token = args[0]
|
35
|
+
token_type = args[1]
|
36
|
+
@token_provider = StringTokenProvider.new token, token_type
|
37
|
+
else
|
38
|
+
fail ArgumentError, 'Invalid number of parameters was passed to TokenCredentials constructor, valid number is 1 or 2'
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Attaches OAuth authentication header to the given HTTP request.
|
44
|
+
# @param request [Net::HTTPRequest] the request authentication header needs to be attached to.
|
45
|
+
#
|
46
|
+
# @return [Net::HTTPRequest] request with attached authentication header
|
47
|
+
def sign_request(request)
|
48
|
+
super(request)
|
49
|
+
header = @token_provider.get_authentication_header
|
50
|
+
|
51
|
+
if (request.respond_to?(:request_headers))
|
52
|
+
request.request_headers[AUTHORIZATION] = header
|
53
|
+
elsif request.respond_to?(:headers)
|
54
|
+
request.headers[AUTHORIZATION] = header
|
55
|
+
else
|
56
|
+
fail ArgumentError, 'Incorrect request object was provided'
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,19 @@
|
|
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 that provides access to authentication token.
|
8
|
+
#
|
9
|
+
class TokenProvider
|
10
|
+
|
11
|
+
#
|
12
|
+
# Returns the string value which needs to be attached
|
13
|
+
# to HTTP request header in order to be authorized.
|
14
|
+
#
|
15
|
+
# @return [String] authentication headers.
|
16
|
+
def get_authentication_header
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,43 @@
|
|
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 happening during deserialization of server response.
|
9
|
+
#
|
10
|
+
class DeserializationError < RestError
|
11
|
+
|
12
|
+
# @return [String] the inner exception message.
|
13
|
+
attr_accessor :exception_message
|
14
|
+
|
15
|
+
# @return [String] the inner exception stacktrace.
|
16
|
+
attr_accessor :exception_stacktrace
|
17
|
+
|
18
|
+
# @return [MsRest::HttpOperationResponse] server response which client was unable to parse.
|
19
|
+
attr_accessor :result
|
20
|
+
|
21
|
+
#
|
22
|
+
# Creates and initialize new instance of the DeserializationError class.
|
23
|
+
# @param [String] message message the human readable description of error.
|
24
|
+
# @param [String] exception_message the inner exception stacktrace.
|
25
|
+
# @param [String] exception_stacktrace the inner exception stacktrace.
|
26
|
+
# @param [MsRest::HttpOperationResponse] the request and response
|
27
|
+
def initialize(msg, exception_message, exception_stacktrace, result)
|
28
|
+
@msg = msg || self.class.name
|
29
|
+
@exception_message = exception_message
|
30
|
+
@exception_stacktrace = exception_stacktrace
|
31
|
+
@result = result
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_json(*a)
|
35
|
+
{exception_message: exception_message, message: @msg, stacktrace: exception_stacktrace, result: result}.to_json(*a)
|
36
|
+
end
|
37
|
+
|
38
|
+
def to_s
|
39
|
+
JSON.pretty_generate(self)
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +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
|
@@ -0,0 +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
|
@@ -0,0 +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
|
@@ -0,0 +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
|
@@ -0,0 +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
|
@@ -0,0 +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
|