🔍 Code Extractor

function get_documents_v1

Maturity: 50

Flask API endpoint that retrieves all documents belonging to the currently authenticated user and returns them as JSON.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
932 - 942
Complexity:
simple

Purpose

This endpoint serves as a REST API route to fetch all documents associated with the authenticated user. It uses authentication middleware to identify the user, queries the document service to retrieve their documents, logs the operation, and returns the documents in a JSON format suitable for frontend consumption.

Source Code

def get_documents():
    """Get all documents for current user"""
    user_email = get_current_user()
    logger.info(f"Getting documents for user: {user_email}")
    
    documents = document_service.get_user_documents(user_email)
    logger.info(f"Found {len(documents)} documents")
    
    return jsonify({
        'documents': [doc.to_dict() for doc in documents]
    })

Return Value

Returns a Flask JSON response containing a dictionary with a 'documents' key. The value is a list of document dictionaries, where each document is converted to a dictionary representation via its to_dict() method. HTTP status code is 200 (OK) by default.

Dependencies

  • flask
  • logging

Required Imports

from flask import jsonify
import logging

Usage Example

# Client-side usage (JavaScript fetch example):
// GET request to retrieve user's documents
fetch('/api/documents', {
  method: 'GET',
  headers: {
    'Authorization': 'Bearer <token>',
    'Content-Type': 'application/json'
  }
})
.then(response => response.json())
.then(data => {
  console.log('User documents:', data.documents);
  // data.documents is an array of document objects
  data.documents.forEach(doc => {
    console.log(`Document ID: ${doc.id}, Title: ${doc.title}`);
  });
})
.catch(error => console.error('Error:', error));

Best Practices

  • Ensure the require_auth decorator is properly implemented to prevent unauthorized access
  • The get_current_user() function should handle cases where no user is authenticated
  • Document.to_dict() method should sanitize sensitive data before returning
  • Consider implementing pagination for users with large numbers of documents
  • Add error handling for cases where document_service.get_user_documents() fails
  • Consider adding query parameters for filtering, sorting, or pagination
  • Implement rate limiting to prevent abuse of this endpoint
  • Log user_email carefully to comply with privacy regulations
  • Consider caching results for frequently accessed user documents
  • Ensure proper CORS configuration if accessed from different domains

Related Versions

Other versions of this component:

  • get_documents_v1

    From: /tf/active/vicechatdev/document_controller_backup.py | Maturity: N/A

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function api_list_documents 84.7% 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 81.5% 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_list_documents_v1 81.2% 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_get_document 78.6% 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_all_data_sections 74.5% similar

    Flask API endpoint that retrieves all data sections associated with the currently authenticated user and returns them as JSON.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
← Back to Browse