Skip to content
This repository

generators for abstracting MWS continuation calls #1045

Merged
merged 2 commits into from over 1 year ago

2 participants

Andy Davidoff Mitch Garnaat
Andy Davidoff

Plus a bonus response parsing bugfix.

Mitch Garnaat garnaat merged commit e1c7979 into from October 15, 2012
Mitch Garnaat garnaat closed this October 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
35  boto/mws/connection.py
@@ -21,6 +21,7 @@
21 21
 import xml.sax
22 22
 import hashlib
23 23
 import base64
  24
+import string
24 25
 from boto.connection import AWSQueryConnection
25 26
 from boto.mws.exception import ResponseErrorFactory
26 27
 from boto.mws.response import ResponseFactory, ResponseElement
@@ -219,6 +220,7 @@ def decorator(func, quota=int(quota), restore=float(restore)):
219 220
             response = getattr(boto.mws.response, action + 'Response')
220 221
         else:
221 222
             response = ResponseFactory(action)
  223
+        response._action = action
222 224
 
223 225
         def wrapper(self, *args, **kw):
224 226
             kw.setdefault(accesskey, getattr(self, accesskey, None))
@@ -278,6 +280,39 @@ def post_request(self, path, params, cls, body='', headers={}, isXML=True):
278 280
         xml.sax.parseString(body, h)
279 281
         return obj
280 282
 
  283
+    def method_for(self, name):
  284
+        """Return the MWS API method referred to in the argument.
  285
+           The named method can be in CamelCase or underlined_lower_case.
  286
+           This is the complement to MWSConnection.any_call.action
  287
+        """
  288
+        # this looks ridiculous but it should be better than regex
  289
+        action = '_' in name and string.capwords(name, '_') or name
  290
+        attribs = [getattr(self, m) for m in dir(self)]
  291
+        ismethod = lambda m: type(m) is type(self.method_for)
  292
+        ismatch = lambda m: getattr(m, 'action', None) == action
  293
+        method = filter(ismatch, filter(ismethod, attribs))
  294
+        return method and method[0] or None
  295
+
  296
+    def iter_call(self, call, *args, **kw):
  297
+        """Pass a call name as the first argument and a generator
  298
+           is returned for the initial response and any continuation
  299
+           call responses made using the NextToken.
  300
+        """
  301
+        method = self.method_for(call)
  302
+        assert method, 'No call named "{0}"'.format(call)
  303
+        return self.iter_response(method(*args, **kw))
  304
+
  305
+    def iter_response(self, response):
  306
+        """Pass a call's response as the initial argument and a
  307
+           generator is returned for the initial response and any
  308
+           continuation call responses made using the NextToken.
  309
+        """
  310
+        yield response
  311
+        more = self.method_for(response._action + 'ByNextToken')
  312
+        while more and response._result.HasNext == 'true':
  313
+            response = more(NextToken=response._result.NextToken)
  314
+            yield response
  315
+
281 316
     @boolean_arguments('PurgeAndReplace')
282 317
     @http_body('FeedContent')
283 318
     @structured_lists('MarketplaceIdList.Id')
2  boto/mws/response.py
@@ -287,7 +287,7 @@ class CancelReportRequestsResult(RequestReportResult):
287 287
 
288 288
 
289 289
 class GetReportListResult(ResponseElement):
290  
-    ReportInfo = Element()
  290
+    ReportInfo = ElementList()
291 291
 
292 292
 
293 293
 class GetReportListByNextTokenResult(GetReportListResult):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.