#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ # @Version : Python 3.11.4 # @Software : Sublime Text 4 # @Author : StudentCWZ # @Email : StudentCWZ@outlook.com # @Date : 2023/11/19 16:05 # @File : log.py # @Description : Defines the log API endpoints. """ from flask import Blueprint, jsonify, request from flask_restful import Api from loguru import logger from pydantic import ValidationError from application.schemas import ParseLogRequestItem from application.services import LogService from application.libs import JsonResponse from application.libs import ServerError # Define the log API blueprint log_api = Blueprint('log_api', __name__) api_logs = Api(log_api) @log_api.route('/logs', methods=['POST']) def parse(): """ Parses the logs based on the request. Returns: The total number of logs parsed or an error message if the input data is invalid. """ json_data = request.get_json(force=True) # If no data provided, return error message if not json_data: logger.error('No input data provided') return JsonResponse.error(message='No input data provided', status_code=400) try: # Validate input data item = ParseLogRequestItem(**json_data) except ValidationError as e: # If validation fails, return error message logger.error(f'Invalid input data, error: {e}') return JsonResponse.error(message='Invalid input data', status_code=400) # If validation passes, parse logs and return total number of logs parsed try: total = LogService.parse(item) except ServerError as e: logger.error('Internal server error') return JsonResponse.error(message='Internal server error', status_code=500) else: return JsonResponse.success(message='Successfully parsed', data=total)