Пример #1
0
 def valid_file_log_helper(self, tmpdir, level):
     output_file = tmpdir.join("tests.log")
     os.environ["LOG_FILE"] = output_file.strpath
     os.environ["LOG_LEVEL"] = level.upper()
     configure_loggers()
     log(self.messages[level], level=level)
     file_content = output_file.read()
     assert "[[asymmetric]]" in file_content
     assert level in file_content
Пример #2
0
 def __prepare_and_validate_finders(
         callback: Union[Dict[str, Any], bool]) -> Dict[str, str]:
     """
     Collects and returns the callback data finders and validates
     that they are correct on decoration-time.
     """
     try:
         validate_callback_data(callback)
         return get_header_finders(callback)
     except InvalidCallbackObjectError as error:
         log(str(error), level="critical")
         raise InvalidCallbackObjectError(error) from error
Пример #3
0
        def decorator(function: Callable[..., Any]) -> Callable[..., Any]:
            """
            Function decorator. Receives the main function and wraps it as a
            starlette endpoint. Returns the original unwrapped function.
            """

            if callback:
                callback_client = CallbackClient(function, callback)

            @self.__app.route(route, methods=methods)
            async def wrapper(request: Request) -> JSONResponse:
                asyncio.ensure_future(log_request(request, route, function))

                try:
                    # Get the body
                    body = await get_body(request)

                    # Get params and headers
                    params = filter_params(function, body)
                    headers = request.headers

                    if not callback:
                        # Process and return the result
                        return JSONResponse(
                            await generic_call(function, params),
                            status_code=response_code,
                        )

                    return callback_client.handle_callback(headers, params)
                except Exception as error:
                    return handle_error(error)

            # Save Endpoint
            try:
                self.__endpoints.add_endpoints(
                    route,
                    methods,
                    function,  # Save unchanged function
                    wrapper,  # Save starlette decorated function
                    callback=callback,
                    response_code=response_code,
                )
            except DuplicatedEndpointError as error:
                log(f"DuplicatedEndpointError: {error}", level="critical")
                raise DuplicatedEndpointError(error) from error

            return function
Пример #4
0
    async def __callback_call(self) -> None:
        """
        Executes the function and makes the request to the callback endpoint.
        """
        try:
            response = await generic_call(self.__function, self.__params)
            if self.custom_key is not None:
                response = {self.custom_key: response}

            async with httpx.AsyncClient() as client:
                await client.request(
                    self.http_method,
                    self.url,
                    json=response,
                )
        except Exception as error:
            message = "Error while executing the delegated method: "
            message += str(error)
            log(message, level="warn")
Пример #5
0
 def log_helper(self, caplog, level, expected_level):
     expected_level = getattr(logging, expected_level.upper())
     with caplog.at_level(expected_level):
         log(self.messages[level], level=level)
     return caplog.text