示例#1
0
 def _decode_response(self, response):
     """Return a native-Python representation of a response's JSON blob."""
     try:
         json_response = response.json()
     except JSONDecodeError:
         raise InvalidJsonResponseError(response)
     return json_response
示例#2
0
 def _decode_response(self, response):
     try:
         json_response = json.loads(response.content,
                                    object_pairs_hook=OrderedDict)
     except ValueError:
         raise InvalidJsonResponseError(response)
     return json_response
示例#3
0
    def send_request(self,
                     method,
                     path_components,
                     body='',
                     query_params=None):
        """
        Send an HTTP request to ES, and return the JSON-decoded response.

        This is mostly an internal method, but it also comes in handy if you
        need to use a brand new ES API that isn't yet explicitly supported by
        pyelasticsearch, while still taking advantage of our connection pooling
        and retrying.

        Retry the request on different servers if the first one is down and
        the ``max_retries`` constructor arg was > 0.

        On failure, raise an
        :class:`~pyelasticsearch.exceptions.ElasticHttpError`, a
        :class:`~pyelasticsearch.exceptions.ConnectionError`, or a
        :class:`~pyelasticsearch.exceptions.Timeout`.

        :arg method: An HTTP method, like "GET"
        :arg path_components: An iterable of path components, to be joined by
            "/"
        :arg body: A map of key/value pairs to be sent as the JSON request
            body. Alternatively, a string to be sent verbatim, without further
            JSON encoding.
        :arg query_params: A map of querystring param names to values or
            ``None``
        """
        path = self._join_path(path_components)

        # We wrap to use pyelasticsearch's exception hierarchy for backward
        # compatibility:
        try:
            # This implicitly converts dicts to JSON. Strings are left alone:
            _, prepped_response = self._transport.perform_request(
                method,
                path,
                params=dict((k, self._utf8(self._to_query(v)))
                            for k, v in iteritems(query_params)) if query_params else None,
                body=body)
        except SerializationError as exc:
            raise InvalidJsonResponseError(exc.args[0])
        except (ConnectionError, ConnectionTimeout) as exc:
            # Pull the urllib3-native exception out, and raise it:
            raise exc.info
        except TransportError as exc:
            status = exc.args[0]
            error_message = exc.args[1]
            self._raise_exception(status, error_message)

        return prepped_response
示例#4
0
    def test_get_with_errors(self, mocked_es):
        # Test missing argument.
        assert_raises(
            BadArgumentError,
            self.api.get,
            query='hello!',
        )

        # Test invalid JSON argument.
        assert_raises(
            MissingArgumentError,
            self.api.get,
        )

        # Test missing index in elasticsearch.
        mocked_connection = mock.Mock()
        mocked_es.ElasticSearch.return_value = mocked_connection

        mocked_connection.search.side_effect = ElasticHttpNotFoundError(
            404, '[[socorro_201801] missing]')
        assert_raises(
            ResourceNotFound,
            self.api.get,
            query='{}',
        )

        # Test invalid JSON response from elasticsearch.
        mocked_connection.search.side_effect = InvalidJsonResponseError('aaa')
        assert_raises(
            DatabaseError,
            self.api.get,
            query='{}',
        )

        # Test HTTP error from elasticsearch.
        mocked_connection.search.side_effect = ElasticHttpError('aaa')
        assert_raises(
            DatabaseError,
            self.api.get,
            query='{}',
        )
示例#5
0
 def create_exception_generator(self):
     yield ConnectionError('ConnectionError')
     yield ElasticHttpNotFoundError('ElasticHttpNotFoundError')
     yield InvalidJsonResponseError('InvalidJsonResponseError')
示例#6
0
 def _decode_response(self, response):
     """Return a native-Python representation of a response's JSON blob."""
     json_response = response.json
     if json_response is None:
         raise InvalidJsonResponseError(response)
     return json_response