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


Python responses.PlainTextResponse方法代碼示例

本文整理匯總了Python中starlette.responses.PlainTextResponse方法的典型用法代碼示例。如果您正苦於以下問題:Python responses.PlainTextResponse方法的具體用法?Python responses.PlainTextResponse怎麽用?Python responses.PlainTextResponse使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在starlette.responses的用法示例。


在下文中一共展示了responses.PlainTextResponse方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: app

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def app(self):
        app_ = Starlette()
        app_.add_middleware(PrometheusMiddleware)
        app_.add_route("/metrics/", metrics)

        @app_.route("/foo/")
        def foo(request):
            return PlainTextResponse("Foo")

        @app_.route("/bar/")
        def bar(request):
            raise ValueError("bar")

        @app_.route("/foo/{bar}/")
        def foobar(request):
            return PlainTextResponse(f"Foo: {request.path_params['bar']}")

        return app_ 
開發者ID:perdy,項目名稱:starlette-prometheus,代碼行數:20,代碼來源:test_end_to_end.py

示例2: app

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def app():
    app = Starlette()

    @app.route("/sync-message")
    def hi(request):
        capture_message("hi", level="error")
        return PlainTextResponse("ok")

    @app.route("/async-message")
    async def hi2(request):
        capture_message("hi", level="error")
        return PlainTextResponse("ok")

    app.add_middleware(SentryAsgiMiddleware)

    return app 
開發者ID:getsentry,項目名稱:sentry-python,代碼行數:18,代碼來源:test_asgi.py

示例3: graphql_http_server

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def graphql_http_server(self, request: Request) -> Response:
        try:
            data = await self.extract_data_from_request(request)
        except HttpError as error:
            return PlainTextResponse(error.message or error.status, status_code=400)

        context_value = await self.get_context_for_request(request)
        extensions = await self.get_extensions_for_request(request, context_value)
        middleware = await self.get_middleware_for_request(request, context_value)

        success, response = await graphql(
            self.schema,
            data,
            context_value=context_value,
            root_value=self.root_value,
            validation_rules=self.validation_rules,
            debug=self.debug,
            introspection=self.introspection,
            logger=self.logger,
            error_formatter=self.error_formatter,
            extensions=extensions,
            middleware=middleware,
        )
        status_code = 200 if success else 400
        return JSONResponse(response, status_code=status_code) 
開發者ID:mirumee,項目名稱:ariadne,代碼行數:27,代碼來源:asgi.py

示例4: get

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def get(self, request: Request) -> Response:

        # validate state
        state = request.query_params["state"]
        _state = request.session.pop("state", "unknown")
        if state != _state:
            return PlainTextResponse("Invalid state", status_code=403)

        # retrieve tokens
        code = request.query_params["code"]
        tokens = self.kc.callback(code)
        # request.session["tokens"] = json.dumps(tokens)

        # retrieve user info
        access_token = tokens["access_token"]
        user = self.kc.fetch_userinfo(access_token)
        request.session["user"] = json.dumps(user)

        return RedirectResponse(self.redirect_uri) 
開發者ID:chunky-monkeys,項目名稱:keycloak-client,代碼行數:21,代碼來源:starlette.py

示例5: app

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def app(elasticapm_client):
    app = Starlette()

    @app.route("/", methods=["GET", "POST"])
    async def hi(request):
        with async_capture_span("test"):
            pass
        return PlainTextResponse("ok")

    @app.route("/raise-exception", methods=["GET", "POST"])
    async def raise_exception(request):
        raise ValueError()

    app.add_middleware(ElasticAPM, client=elasticapm_client)

    return app 
開發者ID:elastic,項目名稱:apm-agent-python,代碼行數:18,代碼來源:starlette_tests.py

示例6: post

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def post(self, request: Request) -> Response:
        content_type = request.headers.get("Content-Type", "")

        if "application/json" in content_type:
            try:
                data = await request.json()
            except json.JSONDecodeError:
                return JSONResponse({"error": "Invalid JSON."}, 400)
        elif "application/graphql" in content_type:
            body = await request.body()
            data = {"query": body.decode()}
        elif "query" in request.query_params:
            data = request.query_params
        else:
            return PlainTextResponse("Unsupported Media Type", 415)

        return await self._get_response(request, data=data) 
開發者ID:tartiflette,項目名稱:tartiflette-asgi,代碼行數:19,代碼來源:_endpoints.py

示例7: _get_response

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def _get_response(self, request: Request, data: QueryParams) -> Response:
        try:
            query = data["query"]
        except KeyError:
            return PlainTextResponse("No GraphQL query found in the request", 400)

        config = get_graphql_config(request)
        background = BackgroundTasks()
        context = {"req": request, "background": background, **config.context}

        engine: Engine = config.engine
        result: dict = await engine.execute(
            query,
            context=context,
            variables=data.get("variables"),
            operation_name=data.get("operationName"),
        )

        content = {"data": result["data"]}
        has_errors = "errors" in result
        if has_errors:
            content["errors"] = format_errors(result["errors"])
        status = 400 if has_errors else 200

        return JSONResponse(content=content, status_code=status, background=background) 
開發者ID:tartiflette,項目名稱:tartiflette-asgi,代碼行數:27,代碼來源:_endpoints.py

示例8: test_tartiflette_app_as_sub_starlette_app

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def test_tartiflette_app_as_sub_starlette_app(engine: Engine) -> None:
    async def home(_request: Request) -> PlainTextResponse:
        return PlainTextResponse("Hello, world!")

    graphql = TartifletteApp(engine=engine)
    routes = [
        Route("/", endpoint=home),
        Mount("/graphql", app=graphql, name="tartiflette-asgi"),
    ]
    app = Starlette(routes=routes, on_startup=[graphql.startup])

    async with get_client(app) as client:
        response = await client.get("/")
        assert response.status_code == 200
        assert response.text == "Hello, world!"
        response = await client.get("/graphql?query={ hello }")
        assert response.status_code == 200
        assert response.json() == {"data": {"hello": "Hello stranger"}} 
開發者ID:tartiflette,項目名稱:tartiflette-asgi,代碼行數:20,代碼來源:test_mount.py

示例9: hello_webpage

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def hello_webpage(request):
    """
    returns PlainTextResponse callback for hello world webpage
    """
    return PlainTextResponse("Hello, world!") 
開發者ID:abhiTronix,項目名稱:vidgear,代碼行數:7,代碼來源:test_webgear.py

示例10: homepage

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def homepage(request) -> Response:
    return PlainTextResponse("Homepage") 
開發者ID:dmfigol,項目名稱:network-programmability-stream,代碼行數:4,代碼來源:app.py

示例11: about

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def about(request) -> Response:
    return PlainTextResponse("About") 
開發者ID:dmfigol,項目名稱:network-programmability-stream,代碼行數:4,代碼來源:app.py

示例12: config_validator

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def config_validator(
    data: fastapi.UploadFile = fastapi.File(...),
):  # pragma: no cover
    try:
        rules.UserConfigurationSchema(await data.read())
    except Exception as e:
        status = 400
        message = str(e)
    else:
        status = 200
        message = "The configuration is valid"

    return responses.PlainTextResponse(message, status_code=status) 
開發者ID:Mergifyio,項目名稱:mergify-engine,代碼行數:15,代碼來源:web.py

示例13: howdy

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def howdy(request):
    user = request.session["user"]
    return PlainTextResponse(f"Howdy {user}!") 
開發者ID:chunky-monkeys,項目名稱:keycloak-client,代碼行數:5,代碼來源:app.py

示例14: logout

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def logout(request):
    return PlainTextResponse("User logged out successfully") 
開發者ID:chunky-monkeys,項目名稱:keycloak-client,代碼行數:4,代碼來源:app.py

示例15: howdy

# 需要導入模塊: from starlette import responses [as 別名]
# 或者: from starlette.responses import PlainTextResponse [as 別名]
def howdy(request):
    return PlainTextResponse("Howdy!") 
開發者ID:chunky-monkeys,項目名稱:keycloak-client,代碼行數:4,代碼來源:starlette.py


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