function get_documents_v1
Flask API endpoint that retrieves all documents belonging to the currently authenticated user and returns them as JSON.
/tf/active/vicechatdev/vice_ai/new_app.py
932 - 942
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
flasklogging
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function api_list_documents 84.7% similar
-
function api_list_documents_v2 81.5% similar
-
function api_list_documents_v1 81.2% similar
-
function api_get_document 78.6% similar
-
function get_all_data_sections 74.5% similar