当前位置: 首页>>代码示例>>Python>>正文


Python IngestApiError.apiRequestError方法代码示例

本文整理汇总了Python中superdesk.errors.IngestApiError.apiRequestError方法的典型用法代码示例。如果您正苦于以下问题:Python IngestApiError.apiRequestError方法的具体用法?Python IngestApiError.apiRequestError怎么用?Python IngestApiError.apiRequestError使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在superdesk.errors.IngestApiError的用法示例。


在下文中一共展示了IngestApiError.apiRequestError方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _update

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
    def _update(self, provider, update):
        try:
            config = provider['config']
            user = config['username']
            password = config['password']
            id_list = config['idList']
            # before "products" was hardcoded as value for "idListType"
            id_list_type = config.get('idListType', 'products')
            if not user.strip() or not password.strip() or not id_list.strip():
                raise KeyError
        except KeyError:
            raise SuperdeskIngestError.notConfiguredError(Exception('username, password and idList are needed'))

        # we remove spaces and empty values from id_list to do a clean list
        id_list = ','.join([id_.strip() for id_ in id_list.split(',') if id_.strip()])

        params = {'idList': id_list,
                  'idListType': id_list_type,
                  'format': '5',
                  'maxItems': '25',
                  'sortOrder': 'chronological'}
        try:
            min_date_time = provider['private']['min_date_time']
            sequence_number = provider['private']['sequence_number']
        except KeyError:
            pass
        else:
            params['minDateTime'] = min_date_time
            params['sequenceNumber'] = sequence_number

        try:
            r = requests.get(URL, auth=(user, password), params=params)
        except Exception:
            raise IngestApiError.apiRequestError(Exception('error while doing the request'))

        try:
            root_elt = etree.fromstring(r.content)
        except Exception:
            raise IngestApiError.apiRequestError(Exception('error while doing the request'))

        parser = self.get_feed_parser(provider)
        items = parser.parse(root_elt, provider)

        try:
            min_date_time = root_elt.xpath('//iptc:timestamp[@role="minDateTime"]/text()', namespaces=NS)[0].strip()
            sequence_number = root_elt.xpath('//iptc:transmitId/text()', namespaces=NS)[0].strip()
        except IndexError:
            raise IngestApiError.apiRequestError(Exception('missing minDateTime or transmitId'))
        else:
            update.setdefault('private', {})
            update['private']['min_date_time'] = min_date_time
            update['private']['sequence_number'] = sequence_number

        return [items]
开发者ID:jerome-poisson,项目名称:superdesk-core,代码行数:56,代码来源:ap.py

示例2: _get_tree

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
    def _get_tree(self, endpoint, payload=None):
        """Get xml response for given API endpoint and payload.

        :param: endpoint
        :type endpoint: str
        :param: payload
        :type payload: str
        """

        if payload is None:
            payload = {}

        payload['token'] = self._get_auth_token(self.provider, update=True)
        url = self._get_absolute_url(endpoint)

        if not self.session:
            self.session = requests.Session()

        retries = 0
        while True:
            try:
                response = self.session.get(url, params=payload, timeout=(30, 15))
            except requests.exceptions.Timeout as ex:
                if retries < 3:
                    logger.warn('Reuters API timeout retrying, retries {}'.format(retries))
                    retries += 1
                    continue
                raise IngestApiError.apiTimeoutError(ex, self.provider)
            except requests.exceptions.TooManyRedirects as ex:
                # Tell the user their URL was bad and try a different one
                raise IngestApiError.apiRedirectError(ex, self.provider)
            except requests.exceptions.RequestException as ex:
                # catastrophic error. bail.
                raise IngestApiError.apiRequestError(ex, self.provider)
            except Exception as error:
                traceback.print_exc()
                raise IngestApiError.apiGeneralError(error, self.provider)

            if response.status_code == 404:
                raise LookupError('Not found %s' % payload)

            break

        try:
            return etree.fromstring(response.content)  # workaround for http mock lib
        except UnicodeEncodeError as error:
            traceback.print_exc()
            raise IngestApiError.apiUnicodeError(error, self.provider)
        except ParseError as error:
            traceback.print_exc()
            raise IngestApiError.apiParseError(error, self.provider)
        except Exception as error:
            traceback.print_exc()
            raise IngestApiError.apiGeneralError(error, self.provider)
开发者ID:jerome-poisson,项目名称:superdesk-core,代码行数:56,代码来源:reuters.py

示例3: test_raise_apiRequestError

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
 def test_raise_apiRequestError(self):
     with assert_raises(IngestApiError) as error_context:
         ex = Exception("Testing apiRequestError")
         raise IngestApiError.apiRequestError(ex, self.provider)
     exception = error_context.exception
     self.assertTrue(exception.code == 4003)
     self.assertTrue(exception.message == "API ingest has request error")
     self.assertIsNotNone(exception.system_exception)
     self.assertEqual(exception.system_exception.args[0], "Testing apiRequestError")
     self.assertEqual(len(self.mock_logger_handler.messages['error']), 1)
     self.assertEqual(self.mock_logger_handler.messages['error'][0],
                      "IngestApiError Error 4003 - API ingest has request error: "
                      "Testing apiRequestError on channel TestProvider")
开发者ID:ancafarcas,项目名称:superdesk-core,代码行数:15,代码来源:errors_test.py

示例4: _get_tree

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
    def _get_tree(self, endpoint, payload=None):
        """
        Get xml response for given API endpoint and payload.
        :param: endpoint
        :type endpoint: str
        :param: payload
        :type payload: str
        """

        if payload is None:
            payload = {}

        payload['token'] = self._get_auth_token(self.provider, update=True)
        url = self._get_absolute_url(endpoint)

        try:
            response = requests.get(url, params=payload, timeout=15)
        except requests.exceptions.Timeout as ex:
            # Maybe set up for a retry, or continue in a retry loop
            raise IngestApiError.apiTimeoutError(ex, self.provider)
        except requests.exceptions.TooManyRedirects as ex:
            # Tell the user their URL was bad and try a different one
            raise IngestApiError.apiRedirectError(ex, self.provider)
        except requests.exceptions.RequestException as ex:
            # catastrophic error. bail.
            raise IngestApiError.apiRequestError(ex, self.provider)
        except Exception as error:
            traceback.print_exc()
            raise IngestApiError.apiGeneralError(error, self.provider)

        if response.status_code == 404:
            raise LookupError('Not found %s' % payload)

        try:
            return etree.fromstring(response.content)  # workaround for http mock lib
        except UnicodeEncodeError as error:
            traceback.print_exc()
            raise IngestApiError.apiUnicodeError(error, self.provider)
        except ParseError as error:
            traceback.print_exc()
            raise IngestApiError.apiParseError(error, self.provider)
        except Exception as error:
            traceback.print_exc()
            raise IngestApiError.apiGeneralError(error, self.provider)
开发者ID:actionless,项目名称:superdesk,代码行数:46,代码来源:reuters.py

示例5: _update

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
    def _update(self, provider, update):
        try:
            config = provider['config']
            user = config['username']
            password = config['password']
        except KeyError:
            SuperdeskIngestError.notConfiguredError(Exception('username and password are needed'))

        url_override = config.get('url', '').strip()
        if not url_override.startswith('http'):
            SuperdeskIngestError.notConfiguredError(Exception('if URL is set, it must be a valid http link'))

        if url_override:
            params = {'user': user, 'password': password, 'maksAntal': 50}
        else:
            params = {'user': user, 'password': password, 'maksAntal': 50, 'waitAcknowledge': 'true'}

        try:
            r = requests.get(url_override or URL, params=params)
        except Exception:
            raise IngestApiError.apiRequestError(Exception('error while doing the request'))

        try:
            root_elt = etree.fromstring(r.text)
        except Exception:
            raise IngestApiError.apiRequestError(Exception('error while parsing the request answer'))

        try:
            if root_elt.xpath('(//error/text())[1]')[0] != '0':
                err_msg = root_elt.xpath('(//errormsg/text())[1]')[0]
                raise IngestApiError.apiRequestError(Exception('error code returned by API: {msg}'.format(msg=err_msg)))
        except IndexError:
            raise IngestApiError.apiRequestError(Exception('Invalid XML, <error> element not found'))

        parser = self.get_feed_parser(provider)
        items = []
        for elt in root_elt.xpath('//RBNews'):
            item = parser.parse(elt, provider)
            items.append(item)
            if not url_override:
                try:
                    queue_id = elt.xpath('.//ServiceQueueId/text()')[0]
                except IndexError:
                    raise IngestApiError.apiRequestError(Exception('missing ServiceQueueId element'))
                ack_params = {'user': user, 'password': password, 'servicequeueid': queue_id}
                try:
                    requests.get(URL_ACK, params=ack_params)
                except Exception:
                    raise IngestApiError.apiRequestError(Exception('error while doing the request'))

        return [items]
开发者ID:sjunaid,项目名称:superdesk-core,代码行数:53,代码来源:ritzau.py

示例6: _request

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
    def _request(self, url):
        try:
            response = requests.get(url, params={}, timeout=120)
        except requests.exceptions.Timeout as ex:
            # Maybe set up for a retry, or continue in a retry loop
            raise IngestApiError.apiTimeoutError(ex, self.provider)
        except requests.exceptions.TooManyRedirects as ex:
            # Tell the user their URL was bad and try a different one
            raise IngestApiError.apiRedirectError(ex, self.provider)
        except requests.exceptions.RequestException as ex:
            # catastrophic error. bail.
            raise IngestApiError.apiRequestError(ex, self.provider)
        except Exception as error:
            traceback.print_exc()
            raise IngestApiError.apiGeneralError(error, self.provider)

        if response.status_code == 404:
            raise LookupError('Not found')

        return response.content
开发者ID:mdhaman,项目名称:superdesk-aap,代码行数:22,代码来源:aap_sports_service.py

示例7: _update

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
 def _update(self, provider, update):
     user = provider['config']['wufoo_username']
     wufoo_data = {
         "url": WUFOO_URL.format(subdomain=user),
         "user": user,
         "api_key": provider['config']['wufoo_api_key'],
         "form_query_entries_tpl": WUFOO_QUERY_FORM + WUFOO_QUERY_ENTRIES,
         "update": update}
     try:
         parser = self.get_feed_parser(provider, None)
     except requests.exceptions.Timeout as ex:
         raise IngestApiError.apiTimeoutError(ex, provider)
     except requests.exceptions.TooManyRedirects as ex:
         raise IngestApiError.apiRedirectError(ex, provider)
     except requests.exceptions.RequestException as ex:
         raise IngestApiError.apiRequestError(ex, provider)
     except Exception as error:
         traceback.print_exc()
         raise IngestApiError.apiGeneralError(error, self.provider)
     items = parser.parse(wufoo_data, provider)
     return [items]
开发者ID:sjunaid,项目名称:superdesk-core,代码行数:23,代码来源:wufoo.py

示例8: get_tree

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
    def get_tree(self, endpoint, payload=None):
        """Get xml response for given API endpoint and payload."""
        if payload is None:
            payload = {}
        payload['token'] = self.get_token()
        url = self.get_url(endpoint)

        try:
            response = requests.get(url, params=payload, timeout=21.0)
        except requests.exceptions.Timeout as ex:
            # Maybe set up for a retry, or continue in a retry loop
            raise IngestApiError.apiTimeoutError(ex, self.provider)
        except requests.exceptions.TooManyRedirects as ex:
            # Tell the user their URL was bad and try a different one
            raise IngestApiError.apiRedirectError(ex, self.provider)
        except requests.exceptions.RequestException as ex:
            # catastrophic error. bail.
            raise IngestApiError.apiRequestError(ex, self.provider)
        except Exception as error:
            traceback.print_exc()
            raise IngestApiError(error, self.provider)

        if response.status_code == 404:
            raise IngestApiError.apiNotFoundError(LookupError('Not found %s' % payload), self.provider)

        try:
            # workaround for httmock lib
            # return etree.fromstring(response.text.encode('utf-8'))
            return etree.fromstring(response.content)
        except UnicodeEncodeError as error:
            traceback.print_exc()
            raise IngestApiError.apiUnicodeError(error, self.provider)
        except ParseError as error:
            traceback.print_exc()
            raise IngestApiError.apiParseError(error, self.provider)
        except Exception as error:
            traceback.print_exc()
            raise IngestApiError(error, self.provider)
开发者ID:akintolga,项目名称:superdesk-server,代码行数:40,代码来源:reuters.py

示例9: ReutersIngestService

# 需要导入模块: from superdesk.errors import IngestApiError [as 别名]
# 或者: from superdesk.errors.IngestApiError import apiRequestError [as 别名]
from superdesk.io.ingest_service import IngestService

from superdesk.utc import utcnow
from superdesk.etree import etree, ParseError
from superdesk.io import register_provider
from .newsml_2_0 import NewsMLTwoParser
from .reuters_token import get_token
from superdesk.errors import IngestApiError
from flask import current_app as app


PROVIDER = 'reuters'
errors = [IngestApiError.apiTimeoutError().get_error_description(),
          IngestApiError.apiRedirectError().get_error_description(),
          IngestApiError.apiRequestError().get_error_description(),
          IngestApiError.apiUnicodeError().get_error_description(),
          IngestApiError.apiParseError().get_error_description(),
          IngestApiError.apiGeneralError().get_error_description()]


class ReutersIngestService(IngestService):
    """Reuters ingest service."""

    DATE_FORMAT = '%Y.%m.%d.%H.%M'
    URL = 'http://rmb.reuters.com/rmd/rest/xml'
    token = None

    def __init__(self):
        self.parser = NewsMLTwoParser()
开发者ID:Flowdeeps,项目名称:superdesk-1,代码行数:31,代码来源:reuters.py


注:本文中的superdesk.errors.IngestApiError.apiRequestError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。