🔍 Code Extractor

function api_documents

Maturity: 48

Flask API endpoint that retrieves statistics and metadata about indexed documents from a document indexer service.

File:
/tf/active/vicechatdev/docchat/app.py
Lines:
1270 - 1281
Complexity:
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

  • flask
  • logging

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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function api_list_documents_v1 70.7% similar

    Flask API endpoint that retrieves and returns a list of all documents uploaded by the currently authenticated user, including metadata such as filename, size, and creation date.

    From: /tf/active/vicechatdev/vice_ai/app.py
  • function api_list_documents 70.4% similar

    Flask API endpoint that retrieves and returns a list of all documents belonging to the authenticated user, including metadata like title, author, section count, and timestamps.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function api_list_documents_v2 69.7% similar

    Flask API endpoint that retrieves and returns a list of all documents uploaded by the currently authenticated user from their session storage.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function api_get_document 68.7% similar

    Flask API endpoint that retrieves a specific document by ID, validates user access permissions, and returns the document data as JSON.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
  • function get_stats 68.6% similar

    Flask API endpoint that retrieves and returns statistics about a document collection from a RAG (Retrieval-Augmented Generation) system.

    From: /tf/active/vicechatdev/docchat/blueprint.py
← Back to Browse