本文整理汇总了Python中amazonproduct.API._fetch方法的典型用法代码示例。如果您正苦于以下问题:Python API._fetch方法的具体用法?Python API._fetch怎么用?Python API._fetch使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类amazonproduct.API
的用法示例。
在下文中一共展示了API._fetch方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _fetch
# 需要导入模块: from amazonproduct import API [as 别名]
# 或者: from amazonproduct.API import _fetch [as 别名]
def _fetch(self, url):
"""
Uses XML response from (or stores in) local file.
"""
# subsequent calls of this API instance
# will be stored in different files
self.calls += 1
path = self.local_file
if self.calls > 1:
head, tail = os.path.splitext(self.local_file)
path = head + "-%i" % self.calls + tail
# If the XML response has not been previously fetched:
# retrieve it, obfuscate all sensible data and store it
# with the name of the TestCase using it
if not os.path.exists(path) or OVERWRITE_TESTS:
try:
fp = API._fetch(self, url)
except urllib2.HTTPError, e:
# HTTP errors 400 (Bad Request) and 410 (Gone) send a more
# detailed error message as body which can be parsed, too.
if e.code in (400, 410):
fp = e.fp
# otherwise re-raise
else:
raise
try:
tree = etree.parse(fp)
except AWSError:
pass
root = tree.getroot()
# overwrite sensible data
nspace = root.nsmap.get(None, "")
for arg in root.xpath("//aws:Arguments/aws:Argument", namespaces={"aws": nspace}):
if arg.get("Name") in "AWSAccessKeyId Signature":
arg.set("Value", "X" * 15)
xml = etree.tostring(root, pretty_print=True)
if AWS_KEY != "" and SECRET_KEY != "":
xml = xml.replace(AWS_KEY, "X" * 15)
xml = xml.replace(SECRET_KEY, "X" * 15)
local_dir = os.path.dirname(path)
if not os.path.exists(local_dir):
# print 'creating %s...' % local_dir
os.mkdir(local_dir)
fp = open(path, "wb")
# print 'storing response in %s...' % self.local_file
fp.write(xml)
fp.close()
return StringIO(xml)