Esempio n. 1
0
def create_ticket():
    """Create a new ticket - ticket data is posted as json

    Example request ::

        {
            "id": "the_odyssey",
            "title": "The Odyssey"
        }


    The response contains the new product ID in a json document ::

        {"id": "1234"}

    """

    schema = TicketSchema()

    try:
        # load input data through a schema (for validation)
        # Note - this may raise `ValueError` for invalid json,
        # or `ValidationError` if data is invalid.
        ticket_data = schema.loads(request.get_data(as_text=True))
    except ValueError as exc:
        raise BadRequest("Invalid json: {}".format(exc))

    # Create the product
    with ClusterRpcProxy(CONFIG) as rpc:
        rpc.ticketService.create(ticket_data)
    return Response(json.dumps({'id': ticket_data['ticket_id']}),
                    mimetype='application/json')
Esempio n. 2
0
    def create_product(self, request):
        """Create a new product - product data is posted as json

        Example request ::

            {
                "id": "the_odyssey",
                "title": "The Odyssey",
                "passenger_capacity": 101,
                "maximum_speed": 5,
                "in_stock": 10
            }


        The response contains the new product ID in a json document ::

            {"id": "the_odyssey"}

        """

        schema = ProductSchema(strict=True)

        try:
            # load input data through a schema (for validation)
            # Note - this may raise `ValueError` for invalid json,
            # or `ValidationError` if data is invalid.
            product_data = schema.loads(request.get_data(as_text=True)).data
        except ValueError as exc:
            raise BadRequest("Invalid json: {}".format(exc))

        # Create the product
        self.products_rpc.create(product_data)
        return Response(json.dumps({'id': product_data['id']}),
                        mimetype='application/json')
Esempio n. 3
0
def add_account():
    schema = CreateAccountSchema()
    try:
        # load input data through a schema (for validation)
        # Note - this may raise `ValueError` for invalid json,
        # or `ValidationError` if data is invalid.
        account_data = schema.loads(request.get_data(as_text=True))
    except ValueError as exc:
        raise BadRequest("Invalid json: {}".format(exc))

    with ClusterRpcProxy(CONFIG) as rpc:
        result = rpc.userService.add_account(account_data)
    return Response(json.dumps({'id': result['account_id']}),
                    mimetype='application/json')
Esempio n. 4
0
    def post_match(self, request):
        schema = MatchSchema(strict=True)
        try:
            match_data = schema.loads(request.get_data(as_text=True)).data
        except ValueError as exc:
            raise BadRequest("Invalid json: {}".format(exc))

        username1 = match_data['username1']
        username2 = match_data['username2']
        score1 = match_data['scorePlayer1']
        score2 = match_data['scorePlayer2']

        if self.create_match(username1, username2, score1, score2):
            return 200, ""
        return 500, ""
Esempio n. 5
0
    def signup(self, request):
        schema = LoginSchema(strict=True)

        try:
            login_data = schema.loads(request.get_data(as_text=True)).data
        except ValueError as exc:
            raise BadRequest("Invalid json: {}".format(exc))

        username = login_data["username"]
        password = login_data["password"]

        try:
            self.auth.signup(username, password)
        except RemoteError as exc:
            return 500, exc.value

        return 200, "Successful sign-up"
Esempio n. 6
0
    def login(self, request):
        schema = LoginSchema(strict=True)

        try:
            login_data = schema.loads(request.get_data(as_text=True)).data
        except ValueError as exc:
            raise BadRequest("Invalid json: {}".format(exc))

        username = login_data["username"]
        password = login_data["password"]

        try:
            user_data = self.auth.login(username, password)
        except RemoteError:
            return 400, "Invalid Credentials"

        return str(user_data)
Esempio n. 7
0
    def signup(self, request):
        schema = SignUpSchema(strict=True)

        try:
            signup_data = schema.loads(request.get_data(as_text=True)).data
        except ValueError as exc:
            raise BadRequest("Invalid json: {}".format(exc))

        username = signup_data["username"]
        password = signup_data["password"]
        country = signup_data["country"]

        if self.auth.signup(username, password, country):
            user_data = self.auth.login(username, password)
            if user_data:
                return 200, user_data
            return 500, "Something went wrong"
        else:
            return 500, "User already exists"
Esempio n. 8
0
    def create_order(self, request):
        """Create a new order - order data is posted as json

        Example request ::

            {
                "order_details": [
                    {
                        "product_id": "the_odyssey",
                        "price": "99.99",
                        "quantity": 1
                    },
                    {
                        "price": "5.99",
                        "product_id": "the_enigma",
                        "quantity": 2
                    },
                ]
            }


        The response contains the new order ID in a json document ::

            {"id": 1234}

        """

        schema = CreateOrderSchema(strict=True)

        try:
            # load input data through a schema (for validation)
            # Note - this may raise `ValueError` for invalid json,
            # or `ValidationError` if data is invalid.
            order_data = schema.loads(request.get_data(as_text=True)).data
        except ValueError as exc:
            raise BadRequest("Invalid json: {}".format(exc))

        # Create the order
        # Note - this may raise `ProductNotFound`
        id_ = self._create_order(order_data)
        return Response(json.dumps({'id': id_}), mimetype='application/json')
Esempio n. 9
0
def get_json(request):
    """Returns request payload as json. In case of malformed json or
    no body, an appropriate http error gets returned.

    Args:
        req (werkzeug.Request): Incoming nameko web request.

    Returns:
        payload (dict):  Parsed request body as json.

    Raises:
        HttpBadRequest: If client has sent empty request body.
        HttpError: Status code 753. If client has sent a malformed request data.
    """
    body = request.get_data(as_text=True)
    if not body:
        raise BadRequest('Empty request body')

    try:
        return json.loads(body)
    except ValueError:
        raise HttpMalformedJSON('Malformed JSON. Could not decode the request body.')