當前位置: 首頁>>代碼示例>>Python>>正文


Python API._fetch方法代碼示例

本文整理匯總了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)
開發者ID:mutaku,項目名稱:python-amazon-product-api,代碼行數:55,代碼來源:utils.py


注:本文中的amazonproduct.API._fetch方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。