Example #1
0
    def request(self, method, url, **kwargs):
        try:
            url = urljoin(self._host_address, url)
            json = kwargs.get(u"json")

            if json is not None:
                kwargs[u"data"] = json_lib.dumps(_filter_out_none(json))
            if u"json" in kwargs:
                del kwargs[u"json"]

            self._renew_authentication(use_cache=True)

            tries = 0
            max_tries = 2
            while tries < max_tries:
                response, unauthorized = self._try_make_request(method, url, **kwargs)
                tries += 1

                if unauthorized and tries < max_tries:
                    self._renew_authentication()
                    continue

                if response.status_code >= 400:
                    response.raise_for_status()

                if not kwargs.get(u"stream"):
                    response.encoding = u"utf-8"  # setting this manually speeds up read times

                return Py42Response(response)
        except requests.HTTPError as err:
            raise_py42_error(err)
Example #2
0
    def test_raise_py42_error_raises_py42_http_error(self, error_response):
        error_response.response.status_code = 600
        with pytest.raises(Py42HTTPError):
            raise_py42_error(error_response)

        error_response.response.status_code = 999
        with pytest.raises(Py42HTTPError):
            raise_py42_error(error_response)
Example #3
0
    def get_security_plan_storage_info_list(self, user_uid):
        locations = None
        try:
            response = self._security_client.get_security_event_locations(
                user_uid)
            locations = response[u"securityPlanLocationsByDestination"]
        except HTTPError as err:
            if err.response.status_code == 404:
                pass
            else:
                raise_py42_error(err)

        if locations:
            plan_destination_map = _get_plan_destination_map(locations)
            selected_plan_infos = self._get_plan_storage_infos(
                plan_destination_map)
            if not selected_plan_infos:
                raise Py42SecurityPlanConnectionError(
                    u"Could not establish a connection to retrieve security events for user {0}"
                    .format(user_uid))

            return selected_plan_infos
Example #4
0
    def test_raise_py42_error_raises_internal_server_error(
            self, error_response):
        error_response.response.status_code = 500
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)

        error_response.response.status_code = 501
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)

        error_response.response.status_code = 599
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)

        error_response.response.status_code = 550
        with pytest.raises(Py42InternalServerError):
            raise_py42_error(error_response)
Example #5
0
 def test_raise_py42_error_raises_not_found_error(self, error_response):
     error_response.response.status_code = 404
     with pytest.raises(Py42NotFoundError):
         raise_py42_error(error_response)
Example #6
0
 def test_raise_py42_error_raises_forbidden_error(self, error_response):
     error_response.response.status_code = 403
     with pytest.raises(Py42ForbiddenError):
         raise_py42_error(error_response)
Example #7
0
 def test_raise_py42_error_raises_unauthorized_error(self, error_response):
     error_response.response.status_code = 401
     with pytest.raises(Py42UnauthorizedError):
         raise_py42_error(error_response)
Example #8
0
 def test_raise_py42_error_raises_bad_request_error(self, error_response):
     error_response.response.status_code = 400
     with pytest.raises(Py42BadRequestError):
         raise_py42_error(error_response)