Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1045 from disruptek/mws_call_iterator
Browse files Browse the repository at this point in the history
generators for abstracting MWS continuation calls
  • Loading branch information
garnaat committed Oct 16, 2012
2 parents 45c7a6e + cf4b239 commit e1c7979
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
35 changes: 35 additions & 0 deletions boto/mws/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion boto/mws/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class CancelReportRequestsResult(RequestReportResult):


class GetReportListResult(ResponseElement):
ReportInfo = Element()
ReportInfo = ElementList()


class GetReportListByNextTokenResult(GetReportListResult):
Expand Down

0 comments on commit e1c7979

Please sign in to comment.