function api_documents
Flask API endpoint that retrieves statistics and metadata about indexed documents from a document indexer service.
/tf/active/vicechatdev/docchat/app.py
1270 - 1281
simple
Purpose
This endpoint serves as a REST API to query the document indexer for collection statistics, providing information about documents that have been indexed in the system. It's used to monitor and display the current state of the document collection, including counts, metadata, and other relevant statistics. The endpoint handles errors gracefully and returns appropriate HTTP status codes.
Source Code
def api_documents():
"""Get list of indexed documents"""
try:
if not document_indexer:
return jsonify({'error': 'Document indexer not initialized'}), 500
stats = document_indexer.get_collection_stats()
return jsonify(stats)
except Exception as e:
logger.error(f"Error getting documents: {e}")
return jsonify({'error': str(e)}), 500
Return Value
Returns a Flask JSON response object. On success (HTTP 200), returns a JSON object containing collection statistics from document_indexer.get_collection_stats(). On error, returns a JSON object with an 'error' key containing the error message, with HTTP status code 500. The exact structure of the stats object depends on the DocumentIndexer implementation.
Dependencies
flasklogging
Required Imports
from flask import Flask
from flask import jsonify
import logging
Usage Example
# Assuming Flask app setup and document_indexer initialization
from flask import Flask, jsonify
import logging
app = Flask(__name__)
logger = logging.getLogger(__name__)
# Initialize document_indexer (example)
from document_indexer import DocumentIndexer
document_indexer = DocumentIndexer()
@app.route('/api/documents', methods=['GET'])
def api_documents():
"""Get list of indexed documents"""
try:
if not document_indexer:
return jsonify({'error': 'Document indexer not initialized'}), 500
stats = document_indexer.get_collection_stats()
return jsonify(stats)
except Exception as e:
logger.error(f"Error getting documents: {e}")
return jsonify({'error': str(e)}), 500
# Client usage:
# GET http://localhost:5000/api/documents
# Response: {"total_documents": 42, "collections": [...], ...}
Best Practices
- Ensure document_indexer is properly initialized before the Flask app starts accepting requests
- The function relies on a global document_indexer variable - consider dependency injection for better testability
- Implement proper authentication/authorization before exposing this endpoint in production
- The error handling catches all exceptions - consider more specific exception handling for different error types
- Add rate limiting to prevent abuse of the API endpoint
- Consider adding pagination if get_collection_stats() returns large datasets
- Add request logging for audit trails and debugging
- Consider caching the stats response if the data doesn't change frequently
- Validate that document_indexer.get_collection_stats() returns serializable data for JSON response
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_list_documents_v1 70.7% similar
-
function api_list_documents 70.4% similar
-
function api_list_documents_v2 69.7% similar
-
function api_get_document 68.7% similar
-
function get_stats 68.6% similar