def route(self, http_request: Type[HttpRequest]) -> HttpReponse:
        """Method to call use case"""

        response = None
        if http_request.body:
            body_params = http_request.body.keys()
            if ("name" in body_params and "specie" in body_params
                    and "user_info" in body_params):
                user_info_params = http_request.body["user_info"].keys()

                if "user_id" in user_info_params or "user_name" in user_info_params:
                    name = http_request.body["name"]
                    specie = http_request.body["specie"]
                    user_info = http_request.body["user_info"]

                    if "age" in body_params:
                        age = http_request.body["age"]
                    else:
                        age = None
                    response = self.register_pet_use_case.register(
                        name=name, specie=specie, user_info=user_info, age=age)
                else:
                    response = {"Success": False, "Data": None}
            else:
                response = {"Success": False, "Data": None}

            if response["Success"] is False:
                http_error = HttpErrors.error_422()
                return HttpReponse(status_code=http_error["status_code"],
                                   body=http_error["body"])
            return HttpReponse(status_code=200, body=response["Data"])
        http_error = HttpErrors.error_400()
        return HttpReponse(status_code=http_error["status_code"],
                           body=http_error["body"])
Exemple #2
0
    def route(self, http_request: Type[HttpRequest]) -> HttpResponse:
        """ Method to call use case """

        response = None

        if http_request.body:
            # if body in htp_request

            body_params = http_request.body.keys()

            if "name" in body_params and "password" in body_params:
                name = http_request.body["name"]
                password = http_request.body["password"]
                response = self.register_user_use_case.register(
                    name=name, password=password)

            else:
                response = {"Success": False, "Data": None}

            if response["Success"] is False:
                https_error = HttpErrors.error_422()
                return HttpResponse(status_code=https_error["status_code"],
                                    body=https_error["body"])

            return HttpResponse(status_code=200, body=response["Data"])

        # If no body in http_request
        https_error = HttpErrors.error_400()
        return HttpResponse(status_code=https_error["status_code"],
                            body=https_error["body"])
    def route(self, http_request: Type[HttpRequest]) -> HttpResponse:
        """ Method to call use case """

        response = None

        if not http_request.body:
            # if body in htp_request
            # If no body in http_request
            https_error = HttpErrors.error_400()
            return HttpResponse(
                status_code=https_error["status_code"], body=https_error["body"]
            )

        body_params = http_request.body.keys()

        if (
            "name" in body_params
            and "specie" in body_params
            and "user_information" in body_params
        ):
            # if body param contain correct items

            user_information_params = http_request.body["user_information"].keys()
            if (
                "user_id" in user_information_params
                or "user_name" in user_information_params
            ):
                # if user_information contain correct items

                name = http_request.body["name"]
                specie = http_request.body["specie"]
                user_information = http_request.body["user_information"]

                if "age" in body_params:
                    age = http_request.body["age"]
                else:
                    age = None

                response = self.register_pet_use_case.registry(
                    name=name,
                    specie=specie,
                    user_information=user_information,
                    age=age,
                )

            else:
                response = {"Success": False, "Data": None}

        else:
            response = {"Success": False, "Data": None}

        if response["Success"] is False:
            https_error = HttpErrors.error_422()
            return HttpResponse(
                status_code=https_error["status_code"], body=https_error["body"]
            )

        return HttpResponse(status_code=200, body=response["Data"])
Exemple #4
0
    def route(self, http_request: Type[HttpRequest]) -> HttpRequest:
        """ Method to call use case """

        response = None

        if http_request.query:
            # if query in http_request

            query_string_params = http_request.query.keys()

            if "pet_id" in query_string_params and "user_id" in query_string_params:
                pet_id = http_request.query["pet_id"]
                user_id = http_request.query["user_id"]
                response = self.find_pet_use_case.by_pet_id_and_user_id(
                    pet_id=pet_id, user_id=user_id
                )

            elif (
                "pet_id" in query_string_params and "user_id" not in query_string_params
            ):
                pet_id = http_request.query["pet_id"]
                response = self.find_pet_use_case.by_pet_id(pet_id=pet_id)

            elif (
                "user_id" in query_string_params and "pet_id" not in query_string_params
            ):
                user_id = http_request.query["user_id"]
                response = self.find_pet_use_case.by_user_id(user_id=user_id)

            else:
                response = {"Success": False, "Data": None}

            if response["Success"] is False:
                https_error = HttpErrors.error_422()
                return HttpResponse(
                    status_code=https_error["status_code"], body=https_error["body"]
                )

            return HttpResponse(status_code=200, body=response["Data"])

        # If no query in http_request
        https_error = HttpErrors.error_400()
        return HttpResponse(
            status_code=https_error["status_code"], body=https_error["body"]
        )
def flask_adapter(request: any, api_route: Type[Route]) -> any:
    """Adapter pattern to Flask
    :param - Flask Request
    :api_route: Composite Routes
    """

    try:
        query_string_params = request.args.to_dict()

        if "pet_id" in query_string_params.keys():
            query_string_params["pet_id"] = int(query_string_params["pet_id"])

        if "user_id" in query_string_params.keys():
            query_string_params["user_id"] = int(
                query_string_params["user_id"])
    except:
        http_error = HttpErrors.error_400()
        return HttpResponse(status_code=http_error["status_code"],
                            body=http_error["body"])

    http_request = HttpRequest(header=request.headers,
                               body=request.json,
                               query=query_string_params)

    try:
        response = api_route.route(http_request)
    except IntegrityError:
        http_error = HttpErrors.error_409()
        return HttpResponse(status_code=http_error["status_code"],
                            body=http_error["body"])
    except Exception as exc:
        print(exc)
        http_error = HttpErrors.error_500()
        return HttpResponse(status_code=http_error["status_code"],
                            body=http_error["body"])

    return response