def handler(self, event, context):
        """
        Request handler method for modify resource function.
        """
        if event is None or Constants.event_path_parameters() not in event:
            return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

        if Constants.event_path_parameter_identifier() not in event[Constants.event_path_parameters()]:
            return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

        try:
            body = json.loads(event[Constants.event_body()])
        except JSONDecodeError as e:
            return response(http.HTTPStatus.BAD_REQUEST, str(e))

        identifier = event[Constants.event_path_parameters()][Constants.event_path_parameter_identifier()]
        http_method = event[Constants.event_http_method()]

        if http_method == HttpConstants.http_method_put() and body is not None:
            try:
                ddb_response = self.modify_resource(body)
                ddb_response[Constants.event_identifier()] = identifier
                return response(http.HTTPStatus.OK, json.dumps(ddb_response))
            except ValueError as e:
                return response(http.HTTPStatus.BAD_REQUEST, str(e))

        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
Exemple #2
0
    def handler(self, event, context):
        """
        Request handler method for fetch resource function.
        """
        if event is None or Constants.event_path_parameters() not in event:
            return response(http.HTTPStatus.BAD_REQUEST,
                            Constants.error_insufficient_parameters())

        if Constants.event_path_parameter_identifier() not in event[
                Constants.event_path_parameters()]:
            return response(http.HTTPStatus.BAD_REQUEST,
                            Constants.error_insufficient_parameters())

        _identifier = event[Constants.event_path_parameters()][
            Constants.event_path_parameter_identifier()]
        _http_method = event[Constants.event_http_method()]

        if _http_method == HttpConstants.http_method_get() and _identifier:
            _ddb_response = self.__retrieve_resource(_identifier)
            if len(_ddb_response[
                    Constants.ddb_response_attribute_name_items()]) == 0:
                return response(http.HTTPStatus.NOT_FOUND,
                                json.dumps(_ddb_response))
            return response(http.HTTPStatus.OK, json.dumps(_ddb_response))
        return response(http.HTTPStatus.BAD_REQUEST,
                        Constants.error_insufficient_parameters())
    def handler(self, event, context):
        """
        Request handler method for insert resource function.
        """

        if event is None:
            return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

        try:
            body = event[Constants.event_body()]
            print(body)
            body_as_json = json.loads(body)
        except JSONDecodeError as e:
            return response(http.HTTPStatus.BAD_REQUEST, str(e))

        http_method = event[Constants.event_http_method()]

        if http_method == HttpConstants.http_method_post() and body_as_json is not None:
            ddb_response = self.insert_resource(body_as_json)
            return response(http.HTTPStatus.CREATED, json.dumps(ddb_response))

        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
Exemple #4
0
def handler(event, context):
    """
    Handler method for insert resource function.
    """
    if event is None:
        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
    if event is None or Constants.event_body() not in event or Constants.event_http_method() not in event:
        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())
    if event[Constants.event_body()] is None or len(event[Constants.event_body()]) is 0:
        return response(http.HTTPStatus.BAD_REQUEST, Constants.error_insufficient_parameters())

    global _dynamodb
    if _dynamodb is None:
        try:
            ddb = DynamoDB()
            _dynamodb = ddb.connect(os.environ[Constants.env_var_region()])
        except Exception as e:
            return response(http.HTTPStatus.INTERNAL_SERVER_ERROR, str(e))

    try:
        request_handler = RequestHandler(_dynamodb)
    except Exception as e:
        return response(http.HTTPStatus.INTERNAL_SERVER_ERROR, str(e))
    return request_handler.handler(event, context)