From 26fd5be24a56a333f3c200d21ff5aee5094052b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E4=B8=BA=E4=B9=8B?= <560397@gree.com.cn> Date: Tue, 21 Nov 2023 11:47:54 +0800 Subject: [PATCH] Update project --- application/views/log/__init__.py | 12 +++++++++++- application/views/log/log.py | 21 +++++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/application/views/log/__init__.py b/application/views/log/__init__.py index b17f4bc..fe0b835 100644 --- a/application/views/log/__init__.py +++ b/application/views/log/__init__.py @@ -7,12 +7,22 @@ # @Email : StudentCWZ@outlook.com # @Date : 2023/11/19 16:05 # @File : __init__.py -# @Description : +# @Description : Registers the logging views with the Flask application. """ from flask import Flask + from .log import log_api def register_log_views(app: Flask): + """ + Registers the log API blueprint with the Flask application. + + Args: + app (Flask): The Flask application. + + Returns: + None + """ app.register_blueprint(log_api) diff --git a/application/views/log/log.py b/application/views/log/log.py index 6fe5c29..750575e 100644 --- a/application/views/log/log.py +++ b/application/views/log/log.py @@ -7,10 +7,10 @@ # @Email : StudentCWZ@outlook.com # @Date : 2023/11/19 16:05 # @File : log.py -# @Description : +# @Description : Defines the log API endpoints. """ -from flask import Blueprint, current_app, request, jsonify +from flask import Blueprint, jsonify, request from flask_restful import Api from loguru import logger from pydantic import ValidationError @@ -19,24 +19,33 @@ from application.schemas import ParseLogRequestItem from application.services import LogService +# 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 jsonify({"message": "No input data provided"}), 400 try: + # Validate input data item = ParseLogRequestItem(**json_data) except ValidationError as e: + # If validation fails, return error message return jsonify({"message": "Invalid input data", "errors": e.errors()}), 400 + # If validation passes, parse logs and return total number of logs parsed total = LogService.parse(item) - return jsonify( - search_total=total - ), 200 - + return jsonify(search_total=total), 200 -- GitLab