예제 #1
0
def get_user(user_id):
    logger.info(f'Getting user id {user_id}')
    resp = users_dynamodb.get_user(user_id)
    if not resp:
        logger.error(f'User with id {user_id} does not exist')
        return jsonify({'error':
                        'User does not exist'}), http.HTTPStatus.NOT_FOUND

    return jsonify(User.from_json(resp))
예제 #2
0
def mock():
    try:
        logger.info(
            f'Trying to return mock response for request {request.json}')
        mock_request = MockRequest.from_dict(request.json)
    except (KeyError, TypeError):
        logger.exception('Error creating mock request')
        return jsonify({'error':
                        'Invalid request'}), http.HTTPStatus.BAD_REQUEST

    user_json = users_dynamodb.get_user(mock_request.userId)
    if not user_json:
        logger.error(f'User id not found for request {mock_request.userId}')
        return jsonify({'error': f'User {mock_request.userId} not found'
                        }), http.HTTPStatus.NOT_FOUND

    user = User.from_json(user_json)
    if not user.isMock:
        logger.warning(f'User {mock_request.userId} is not mock available')
        return '', http.HTTPStatus.NO_CONTENT

    if not mock_request.httpStatus:
        mock_request.httpStatus = user.defaultResponse

    template_reduced = mock_service.search_template(mock_request)
    if not template_reduced:
        logger.error(
            f'Template id not found for path {mock_request.path} and http status response '
            f'{mock_request.httpStatus}')
        return jsonify({'error':
                        'Template id not found'}), http.HTTPStatus.NOT_FOUND

    template = Template.from_json(
        templates_dynamodb.get_template(template_reduced['id']))
    file = templates_s3.get_file(template)
    if not file:
        logger.error(
            f"File not found for path {mock_request.path} and template id {template_reduced['id']}"
        )
        return jsonify({'error': 'File not found'}), http.HTTPStatus.NOT_FOUND

    if template.responseType == 'json':
        return jsonify(
            mock_service.parse_file(mock_request, file, template,
                                    user.parameters))

    return '', http.HTTPStatus.NO_CONTENT