<commit_before># -*- coding: utf-8 -*-

# FOGLAMP_BEGIN
# See: http://foglamp.readthedocs.io/
# FOGLAMP_END

"""Backup and Restore Rest API support"""

from aiohttp import web
# TODO: remove this and call actual class methods
from unittest.mock import MagicMock
Backup = MagicMock()

__author__ = "<NAME>"
__copyright__ = "Copyright (c) 2017 OSIsoft, LLC"
__license__ = "Apache 2.0"
__version__ = "${VERSION}"


_help = """
    -----------------------------------------------------------------------------------
    | GET, POST       | /foglamp/backup                                                |
    | GET             | /foglamp/backup/{backup-id}                                    |
    | DELETE          | /foglamp/backup/{backup-id}                                    |
    |                                                                                  |
    | PUT             | /foglamp/backup/{backup-id}/restore                            |
    -----------------------------------------------------------------------------------
"""

async def get_backups(request):
    """
    Returns a list of all backups

    :Example: curl -X GET  http://localhost:8082/foglamp/backup
    :Example: curl -X GET  http://localhost:8082/foglamp/backup?limit=2&skip=1&status=complete
    """
    try:
        limit = request.query['limit'] if 'limit' in request.query else None
        skip = request.query['skip'] if 'skip' in request.query else None
        status = request.query['status'] if 'status' in request.query else None

        if status and status not in ['complete', 'running', 'failed']:
            raise web.HTTPBadRequest(reason='Incorrect status')
        if limit:
            try:
                limit = int(limit)
            except ValueError:
                raise web.HTTPBadRequest(reason='Limit can be a positive integer only')
        if skip:
            try:
                skip = int(skip)
            except ValueError:
                raise web.HTTPBadRequest(reason='Skip can be a positive integer only')
        try:
            # TODO : Fix after actual implementation
            Backup.get_backup_list.return_value = [{'id': 28, 'date': '2017-08-30 04:05:10.382', 'status': 'running'},
                                                   {'id': 27, 'date': '2017-08-29 04:05:13.392', 'status': 'failed'},
                                                   {'id': 26, 'date': '2017-08-28 04:05:08.201', 'status': 'complete'}]

            # backup_json = [{"id": b[0], "date": b[1], "status": b[2]}
            #                for b in Backup.get_backup_list(limit=limit, skip=skip, status=status)]
            backup_json = Backup.get_backup_list(limit=limit, skip=skip, status=status)
        except Backup.DoesNotExist:
            raise web.HTTPNotFound(reason='No backups found for queried parameters')
        return web.json_response({"backups": backup_json})
    except Exception as ex:
        raise web.HTTPInternalServerError(reason='FogLAMP has encountered an internal error', text=str(ex))

async def create_backup(request):
    """
    Creates a backup

    :Example: curl -X POST http://localhost:8082/foglamp/backup
    """
    try:
        # TODO : Fix after actual implementation
        Backup.create_backup.return_value = "running"
        status = Backup.create_backup()
    except Exception as ex:
        raise web.HTTPInternalServerError(reason='FogLAMP has encountered an internal error', text=str(ex))
    return web.json_response({"status": status})

async def get_backup_details(request):
    """
    Returns the details of a backup

    :Example: curl -X GET  http://localhost:8082/foglamp/backup/1
    """
    try:
        backup_id = request.match_info.get('backup_id', None)
        if not backup_id:
            raise web.HTTPBadRequest(reason='Backup id is required')
        try:
            backup_id = int(backup_id)
        except ValueError:
            raise web.HTTPBadRequest(reason='Backup id can be a positive integer only')

        try:
            # TODO : Fix after actual implementation
            Backup.get_backup_details.return_value = \
                {"date": '2017-08-30 04:05:10.382', "status": "running"}
        except Backup.DoesNotExist:
            raise web.HTTPBadRequest(reason='Backup with {} does not exist'.format(backup_id))

        _resp = Backup.get_backup_details(id=backup_id)
        _resp["id"] = backup_id
        return web.json_response(_resp)

    except ValueError as ex:
        raise web.HTTPNotFound(reason=str(ex))
    except Exception as ex:
        raise web.HTTPInternalServerError(reason='FogLAMP has encountered an internal error', text=str(ex))

async def delete_backup(request):
    """
    Delete a backup

    :Example: curl -X DELETE  http://localhost:8082/foglamp/backup/1
    """
    try:
        backup_id = request.match_info.get('backup_id', None)
        if not backup_id:
            raise web.HTTPBadRequest(reason='Backup id is required')
        try:
            backup_id = int(backup_id)
        except ValueError:
            raise web.HTTPBadRequest(reason='Backup id can be a positive integer only')

        try:
            # TODO : Fix after actual implementation
            Backup.delete_backup.return_value = "Backup deleted successfully"
        except Backup.DoesNotExist:
            raise web.HTTPBadRequest(reason='Backup with {} does not exist'.format(backup_id))

        _resp = Backup.delete_backup(id=backup_id)
        return web.json_response({'message': _resp})

    except ValueError as ex:
        raise web.HTTPNotFound(reason=str(ex))
    except Exception as ex:
        raise web.HTTPInternalServerError(reason='FogLAMP has encountered an internal error', text=str(ex))

async def restore_backup(request):
    """
    Restore from a backup

    :Example: curl -X PUT  http://localhost:8082/foglamp/backup/1/restore
    """
    try:
        backup_id = request.match_info.get('backup_id', None)
        if not backup_id:
            raise web.HTTPBadRequest(reason='Backup id is required')
        try:
            backup_id = int(backup_id)
        except ValueError:
            raise web.HTTPBadRequest(reason='Backup id can be a positive integer only')

        try:
            # TODO : Fix after actual implementation
            Backup.restore_backup.return_value = 1
        except Backup.DoesNotExist:
            raise web.HTTPBadRequest(reason='Backup with {} does not exist'.format(backup_id))

        _resp = Backup.restore_backup(id=backup_id)
        if _resp == 1:
            return web.json_response({'message': 'Restore backup with id {} started successfully'.format(backup_id)})
        else:
            return web.json_response({'message': 'Restore backup with id {} failed'.format(backup_id)})
    except ValueError as ex:
        raise web.HTTPNotFound(reason=str(ex))
    except Exception as ex:
        raise web.HTTPInternalServerError(reason='FogLAMP has encountered an internal error', text=str(ex))<commit_msg>fixed api error handling<commit_after># -*- coding: utf-8 -*-

# FOGLAMP_BEGIN
# See: http://foglamp.readthedocs.io/
# FOGLAMP_END

"""Backup and Restore Rest API support"""

from aiohttp import web
# TODO: remove this and call actual class methods
from unittest.mock import MagicMock
Backup = MagicMock()

__author__ = "<NAME>"
__copyright__ = "Copyright (c) 2017 OSIsoft, LLC"
__license__ = "Apache 2.0"
__version__ = "${VERSION}"


_help = """
    -----------------------------------------------------------------------------------
    | GET, POST       | /foglamp/backup                                                |
    | GET             | /foglamp/backup/{backup-id}                                    |
    | DELETE          | /foglamp/backup/{backup-id}                                    |
    |                                                                                  |
    | PUT             | /foglamp/backup/{backup-id}/restore                            |
    -----------------------------------------------------------------------------------
"""

async def get_backups(request):
    """
    Returns a list of all backups

    :Example: curl -X GET  http://localhost:8082/foglamp/backup
    :Example: curl -X GET  http://localhost:8082/foglamp/backup?limit=2&skip=1&status=complete
    """
    limit = request.query['limit'] if 'limit' in request.query else None
    skip = request.query['skip'] if 'skip' in request.query else None
    status = request.query['status'] if 'status' in request.query else None

    if status and status not in ['complete', 'running', 'failed']:
        raise web.HTTPBadRequest(reason='Incorrect status')
    if limit:
        try:
            limit = int(limit)
        except ValueError:
            raise web.HTTPBadRequest(reason='Limit can be a positive integer only')
    if skip:
        try:
            skip = int(skip)
        except ValueError:
            raise web.HTTPBadRequest(reason='Skip can be a positive integer only')
    try:
        # TODO : Fix after actual implementation
        Backup.get_backup_list.return_value = [{'id': 28, 'date': '2017-08-30 04:05:10.382', 'status': 'running'},
                                               {'id': 27, 'date': '2017-08-29 04:05:13.392', 'status': 'failed'},
                                               {'id': 26, 'date': '2017-08-28 04:05:08.201', 'status': 'complete'}]

        # backup_json = [{"id": b[0], "date": b[1], "status": b[2]}
        #                for b in Backup.get_backup_list(limit=limit, skip=skip, status=status)]
        backup_json = Backup.get_backup_list(limit=limit, skip=skip, status=status)
    except Backup.DoesNotExist:
        raise web.HTTPNotFound(reason='No backups found for queried parameters')
    return web.json_response({"backups": backup_json})

async def create_backup(request):
    """
    Creates a backup

    :Example: curl -X POST http://localhost:8082/foglamp/backup
    """
    # TODO : Fix after actual implementation
    Backup.create_backup.return_value = "running"
    status = Backup.create_backup()
    return web.json_response({"status": status})

async def get_backup_details(request):
    """
    Returns the details of a backup

    :Example: curl -X GET  http://localhost:8082/foglamp/backup/1
    """
    try:
        backup_id = request.match_info.get('backup_id', None)
        if not backup_id:
            raise web.HTTPBadRequest(reason='Backup id is required')
        try:
            backup_id = int(backup_id)
        except ValueError:
            raise web.HTTPBadRequest(reason='Backup id can be a positive integer only')

        try:
            # TODO : Fix after actual implementation
            Backup.get_backup_details.return_value = \
                {"date": '2017-08-30 04:05:10.382', "status": "running"}
        except Backup.DoesNotExist:
            raise web.HTTPNotFound(reason='Backup with {} does not exist'.format(backup_id))

        _resp = Backup.get_backup_details(id=backup_id)
        _resp["id"] = backup_id
        return web.json_response(_resp)
    except ValueError as ex:
        raise web.HTTPNotFound(reason=str(ex))

async def delete_backup(request):
    """
    Delete a backup

    :Example: curl -X DELETE  http://localhost:8082/foglamp/backup/1
    """
    try:
        backup_id = request.match_info.get('backup_id', None)
        if not backup_id:
            raise web.HTTPBadRequest(reason='Backup id is required')
        try:
            backup_id = int(backup_id)
        except ValueError:
            raise web.HTTPBadRequest(reason='Backup id can be a positive integer only')

        try:
            # TODO : Fix after actual implementation
            Backup.delete_backup.return_value = "Backup deleted successfully"
        except Backup.DoesNotExist:
            raise web.HTTPNotFound(reason='Backup with {} does not exist'.format(backup_id))

        _resp = Backup.delete_backup(id=backup_id)
        return web.json_response({'message': _resp})
    except ValueError as ex:
        raise web.HTTPNotFound(reason=str(ex))

async def restore_backup(request):
    """
    Restore from a backup

    :Example: curl -X PUT  http://localhost:8082/foglamp/backup/1/restore
    """
    try:
        backup_id = request.match_info.get('backup_id', None)
        if not backup_id:
            raise web.HTTPBadRequest(reason='Backup id is required')
        try:
            backup_id = int(backup_id)
        except ValueError:
            raise web.HTTPBadRequest(reason='Backup id can be a positive integer only')

        try:
            # TODO : Fix after actual implementation
            Backup.restore_backup.return_value = 1
        except Backup.DoesNotExist:
            raise web.HTTPNotFound(reason='Backup with {} does not exist'.format(backup_id))

        _resp = Backup.restore_backup(id=backup_id)
        if _resp == 1:
            return web.json_response({'message': 'Restore backup with id {} started successfully'.format(backup_id)})
        else:
            return web.json_response({'message': 'Restore backup with id {} failed'.format(backup_id)})
    except ValueError as ex:
        raise web.HTTPNotFound(reason=str(ex))
{'fraction_non_alphanumeric': 0.1310991754293063, 'fraction_numerical': 0.01739919812391255, 'mean_word_length': 2.0594769729229343, 'pattern_counts': {'":': 14, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2358605', 'n_tokens_mistral': 3766, 'n_tokens_neox': 3593, 'n_words': 940}