Example #1
0
async def post_event(request,
                     event: Event,
                     operation: str = "merge",
                     insert: bool = False) -> Event:
    """
        merge or update an existing event with a new one
        :param request: the request object
        :param operation: takes either "merge" or "update":
           - merge will merge the existing event with the new event and
             raise an exception whenever merge isn't possible
           - update will update the values in the existing event with
             the new values given by the post request and add new values
        :return: the updated/merged event
    """
    if operation != "merge" and operation != "update":
        raise APIException(
            "only update and merge operation are supported, {0} passed".format(
                operation))

    try:
        existing_event = await request.app["db"].event.find_by_id(event.id)
        if operation == "merge":
            _merge(existing_event, event)
        else:
            _update(existing_event, event)
    except HTTPNotFound:
        existing_event = event
    await request.app["db"].event.update_by_id(event.id, existing_event,
                                               insert)
    return existing_event
Example #2
0
async def delete_event(request, event_id: str) -> bool:
    """
        deletes an existing event with event_id
        :param request: the request object
        :param event_id: the id corresponding to the source
        :return: the result of delete operation (msg)
    """
    result = await request.app["db"].event.delete_by_id(event_id)
    if not result:
        raise APIException(
            "Can not delete event with id: {0}".format(event_id))
    return {'message': 'Event: {0} deleted successfully'.format(event_id)}
Example #3
0
async def put_event(request, event: Event) -> Event:
    """
    stores a new event on the database
    :param request: the object representing the client request
    :param event: the event to be stored
    :return: the stored event
    """
    try:
        await request.app["db"].event.save(event)
    except DuplicateKeyError:
        raise APIException(message=f"Error: id '{event.id}' already exists")
    if request.app["config"].log_events:
        LOG.info(json.dumps(event.to_primitive()))
    return event
Example #4
0
async def api_exception(request) -> User:
    raise APIException("nope")