🔍 Code Extractor

function get_all_data_sections

Maturity: 50

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

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
4261 - 4273
Complexity:
simple

Purpose

This endpoint serves as a REST API route to fetch all data sections belonging to the authenticated user. It's used in web applications to display user-specific data sections, typically for dashboards or data management interfaces. The function handles authentication, retrieves data through a service layer, and provides error handling with appropriate HTTP status codes.

Source Code

def get_all_data_sections():
    """Get all data sections for the current user"""
    user_email = get_current_user()
    
    try:
        data_sections = data_section_service.get_user_data_sections(user_email)
        return jsonify({
            'success': True,
            'data_sections': clean_for_json([section.to_dict() for section in data_sections])
        })
    except Exception as e:
        logger.error(f"Error getting data sections: {e}")
        return jsonify({'error': str(e)}), 500

Return Value

Returns a Flask JSON response object. On success (HTTP 200), returns a dictionary with 'success': True and 'data_sections': a list of dictionaries representing each data section (cleaned for JSON serialization). On error (HTTP 500), returns a dictionary with 'error': error message string. The response is automatically serialized to JSON by Flask's jsonify function.

Dependencies

  • flask
  • logging

Required Imports

from flask import jsonify
import logging

Usage Example

# This is a Flask route handler, typically called via HTTP request
# Example HTTP request:
# GET /api/data-sections
# Headers: Authorization: Bearer <token>

# Example response on success:
# {
#   "success": true,
#   "data_sections": [
#     {
#       "id": "123",
#       "name": "Sales Data",
#       "user_email": "user@example.com",
#       "created_at": "2024-01-01T00:00:00"
#     }
#   ]
# }

# Example usage in client code (JavaScript):
# fetch('/api/data-sections', {
#   method: 'GET',
#   headers: {
#     'Authorization': 'Bearer ' + token
#   }
# })
# .then(response => response.json())
# .then(data => {
#   if (data.success) {
#     console.log('Data sections:', data.data_sections);
#   }
# });

Best Practices

  • Always ensure the require_auth decorator is applied to protect this endpoint from unauthorized access
  • The function properly handles exceptions and returns appropriate HTTP status codes (500 for server errors)
  • Uses a service layer (data_section_service) for business logic separation, following good architectural patterns
  • Logs errors for debugging and monitoring purposes
  • Uses clean_for_json() to ensure data is properly serialized, avoiding JSON serialization errors
  • Returns consistent response structure with 'success' flag for easy client-side handling
  • Consider adding pagination if users can have many data sections to avoid performance issues
  • Consider adding query parameters for filtering or sorting data sections
  • Ensure proper CORS configuration if this API is accessed from different domains
  • The get_current_user() function should be validated to ensure it returns a valid user email

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_user_data_sections 93.0% similar

    Flask API endpoint that retrieves all data sections associated with a specific user, enforcing access control to ensure users can only access their own data sections.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_data_section 82.7% similar

    Flask API endpoint that retrieves a specific data section by ID, ensuring the requesting user is the owner of the section.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_text_sections 79.5% similar

    Flask API endpoint that retrieves text sections for the authenticated user with optional filtering by type, search query, tags, and uniqueness.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function get_document_text_sections 78.6% similar

    Flask API endpoint that retrieves all text and data sections for a specific document, verifying user ownership and returning sections sorted by position.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function create_data_section 75.0% similar

    Flask API endpoint that creates a new data section for authenticated users, accepting title and description from JSON request body.

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