Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

methods for iteration over calls and their continuations

  • Loading branch information...
commit cf4b239f725de0d1663ac8592e0f7ef745d29fcc 1 parent 293ae43
@disruptek authored
Showing with 35 additions and 0 deletions.
  1. +35 −0 boto/mws/connection.py
View
35 boto/mws/connection.py
@@ -21,6 +21,7 @@
import xml.sax
import hashlib
import base64
+import string
from boto.connection import AWSQueryConnection
from boto.mws.exception import ResponseErrorFactory
from boto.mws.response import ResponseFactory, ResponseElement
@@ -219,6 +220,7 @@ def decorator(func, quota=int(quota), restore=float(restore)):
response = getattr(boto.mws.response, action + 'Response')
else:
response = ResponseFactory(action)
+ response._action = action
def wrapper(self, *args, **kw):
kw.setdefault(accesskey, getattr(self, accesskey, None))
@@ -278,6 +280,39 @@ def post_request(self, path, params, cls, body='', headers={}, isXML=True):
xml.sax.parseString(body, h)
return obj
+ def method_for(self, name):
+ """Return the MWS API method referred to in the argument.
+ The named method can be in CamelCase or underlined_lower_case.
+ This is the complement to MWSConnection.any_call.action
+ """
+ # this looks ridiculous but it should be better than regex
+ action = '_' in name and string.capwords(name, '_') or name
+ attribs = [getattr(self, m) for m in dir(self)]
+ ismethod = lambda m: type(m) is type(self.method_for)
+ ismatch = lambda m: getattr(m, 'action', None) == action
+ method = filter(ismatch, filter(ismethod, attribs))
+ return method and method[0] or None
+
+ def iter_call(self, call, *args, **kw):
+ """Pass a call name as the first argument and a generator
+ is returned for the initial response and any continuation
+ call responses made using the NextToken.
+ """
+ method = self.method_for(call)
+ assert method, 'No call named "{0}"'.format(call)
+ return self.iter_response(method(*args, **kw))
+
+ def iter_response(self, response):
+ """Pass a call's response as the initial argument and a
+ generator is returned for the initial response and any
+ continuation call responses made using the NextToken.
+ """
+ yield response
+ more = self.method_for(response._action + 'ByNextToken')
+ while more and response._result.HasNext == 'true':
+ response = more(NextToken=response._result.NextToken)
+ yield response
+
@boolean_arguments('PurgeAndReplace')
@http_body('FeedContent')
@structured_lists('MarketplaceIdList.Id')
Please sign in to comment.
Something went wrong with that request. Please try again.