This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reworked waiters to support v2 definitions.
Rewrote the internals to support the waiters v2 format. This new format allows for multi success and multiple failure states. It is also a much simpler format to work with.
- Loading branch information
1 parent
e2b7ec8
commit d53ba2f
Showing
12 changed files
with
428 additions
and
313 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
module Aws | ||
module Waiters | ||
|
||
# Polls a single API operation inspecting the response data and/or error | ||
# for states matching one of its acceptors. | ||
# @api private | ||
class Poller | ||
|
||
# @api private | ||
RAISE_HANDLER = Seahorse::Client::Plugins::RaiseResponseErrors::Handler | ||
|
||
# @api private | ||
def initialize(options = {}) | ||
@operation_name = underscore(options['operation']).to_sym | ||
@acceptors = options['acceptors'] || [] | ||
end | ||
|
||
# Makes an API call, returning the resultant state and the response. | ||
# | ||
# * `:success` - A success state has been matched. | ||
# * `:failure` - A terminate failure state has been matched. | ||
# * `:retry` - The waiter may be retried. | ||
# * `:error` - The waiter encountered an un-expected error. | ||
# | ||
# @example A trival (bad) example of a waiter that polls indefinetly. | ||
# | ||
# loop do | ||
# | ||
# state, resp = poller.call(client:client, params:{}) | ||
# | ||
# case state | ||
# when :success then return true | ||
# when :failure then return false | ||
# when :retry then next | ||
# when :error then raise 'oops' | ||
# end | ||
# | ||
# end | ||
# | ||
# @option options [required,Client] :client | ||
# @option options [required,Hash] :params | ||
# @return [Array<Symbol,Response>] | ||
def call(options = {}) | ||
response = send_request(options) | ||
@acceptors.each do |acceptor| | ||
if acceptor_matches?(acceptor, response) | ||
return [acceptor['state'].to_sym, response] | ||
end | ||
end | ||
[response.error ? :error : :retry, response] | ||
end | ||
|
||
private | ||
|
||
def send_request(options) | ||
req = options[:client].build_request(@operation_name, options[:params]) | ||
req.handlers.remove(RAISE_HANDLER) | ||
req.send_request | ||
end | ||
|
||
def acceptor_matches?(acceptor, response) | ||
send("matches_#{acceptor['matcher']}?", acceptor, response) | ||
end | ||
|
||
def matches_path?(acceptor, response) | ||
JMESPath.search(path(acceptor), response.data) == acceptor['expected'] | ||
end | ||
|
||
def matches_pathAll?(acceptor, response) | ||
values = JMESPath.search(path(acceptor), response.data) | ||
Array === values && | ||
values.count > 0 && | ||
values.all? { |value| value == acceptor['expected'] } | ||
end | ||
|
||
def matches_pathAny?(acceptor, response) | ||
values = JMESPath.search(path(acceptor), response.data) | ||
Array === values && | ||
values.count > 0 && | ||
values.any? { |value| value == acceptor['expected'] } | ||
end | ||
|
||
def matches_status?(acceptor, response) | ||
response.context.http_response.status_code == acceptor['expected'] | ||
end | ||
|
||
def matches_error?(acceptor, response) | ||
Aws::Errors::ServiceError === response.error && | ||
response.error.code == acceptor['expected'] | ||
end | ||
|
||
def path(acceptor) | ||
acceptor['argument'].gsub(/\w+/) { |s| Seahorse::Util.underscore(s) } | ||
end | ||
|
||
def underscore(str) | ||
Seahorse::Util.underscore(str) | ||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.
d53ba2f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎆