# Path: e2e-test-server/e2e_test_server/constants.py
# PROJECT_ID = os.environ["PROJECT_ID"]
# 
# PUSH_PORT = (
#     os.environ["PUSH_PORT"]
#     if SUBSCRIPTION_MODE is SubscriptionMode.PUSH
#     else None
# )
# 
# REQUEST_SUBSCRIPTION_NAME = os.environ["REQUEST_SUBSCRIPTION_NAME"]
# 
# RESPONSE_TOPIC_NAME = os.environ["RESPONSE_TOPIC_NAME"]
# 
# SCENARIO = "scenario"
# 
# STATUS_CODE = "status_code"
# 
# TEST_ID = "test_id"


import base64
import logging
from typing import Literal
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1.subscriber.message import Message
from google.rpc import code_pb2
from . import scenarios, types
from .constants import (
    PROJECT_ID,
    PUSH_PORT,
    REQUEST_SUBSCRIPTION_NAME,
    RESPONSE_TOPIC_NAME,
    SCENARIO,
    STATUS_CODE,
    TEST_ID,
)
    from flask import Flask, Response, request
    from waitress import serve

    logger.info(
        "Listening on subscription {} for pub/sub messages".format(
            REQUEST_SUBSCRIPTION_NAME
        )
    )
    with subscriber:
        streaming_pull_future.result()


def pubsub_push() -> None:
    """Start a web server listening for incoming pub/sub push message"""

    app = Flask(__name__)
    responder = _Responder()

    @app.route("/", methods=["POST"])
    def index() -> Response:
        if not request.is_json:
            return Response("Expected a JSON payload", status=400)
        payload = types.PubsubPushPayload(**request.json)

        ack_or_nack = handle_message(
            payload.message.to_pubsub_message(), responder
        )
        if ack_or_nack == "ack":
            return Response(status=200)
        else:
            return Response(status=400)


# Path: e2e-test-server/e2e_test_server/constants.py
# PROJECT_ID = os.environ["PROJECT_ID"]
# 
# PUSH_PORT = (
#     os.environ["PUSH_PORT"]
#     if SUBSCRIPTION_MODE is SubscriptionMode.PUSH
#     else None
# )
# 
# REQUEST_SUBSCRIPTION_NAME = os.environ["REQUEST_SUBSCRIPTION_NAME"]
# 
# RESPONSE_TOPIC_NAME = os.environ["RESPONSE_TOPIC_NAME"]
# 
# SCENARIO = "scenario"
# 
# STATUS_CODE = "status_code"
# 
# TEST_ID = "test_id"


# Path: e2e-test-server/e2e_test_server/server.py
import base64
import logging
from typing import Literal
from google.cloud import pubsub_v1
from google.cloud.pubsub_v1.subscriber.message import Message
from google.rpc import code_pb2
from . import scenarios, types
from .constants import (
    PROJECT_ID,
    PUSH_PORT,
    REQUEST_SUBSCRIPTION_NAME,
    RESPONSE_TOPIC_NAME,
    SCENARIO,
    STATUS_CODE,
    TEST_ID,
)
    from flask import Flask, Response, request
    from waitress import serve


    logger.info(
        "Listening on subscription {} for pub/sub messages".format(
            REQUEST_SUBSCRIPTION_NAME
        )
    )
    with subscriber:
        streaming_pull_future.result()


def pubsub_push() -> None:
    """Start a web server listening for incoming pub/sub push message"""

    app = Flask(__name__)
    responder = _Responder()

    @app.route("/", methods=["POST"])
    def index() -> Response:
        if not request.is_json:
            return Response("Expected a JSON payload", status=400)
        payload = types.PubsubPushPayload(**request.json)

        ack_or_nack = handle_message(
            payload.message.to_pubsub_message(), responder
        )
        if ack_or_nack == "ack":
            return Response(status=200)
        else:
            return Response(status=400)


    serve(app, port=PUSH_PORT)