Skip to content

pushed fast api version #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 25 additions & 57 deletions _example.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,44 @@
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods
from django.views.decorators.csrf import csrf_exempt
from .client import supabase
import logging

logger = logging.getLogger(" apps.supabase_home")
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse

from .client import supabase

app = FastAPI()

logger = logging.getLogger("apps.supabase_home")

@csrf_exempt
@require_http_methods(["GET"])
def example_supabase_view(request):

@app.get("/api/supabase/example/")
async def example_supabase_view():
"""
Example view demonstrating how to use the Supabase client in Django.
Example endpoint demonstrating how to use the Supabase client in FastAPI.

This view shows how to:
This endpoint shows how to:
1. Use the Supabase database service to fetch data
2. Handle errors properly
3. Return JSON responses

URL: /api/supabase/example/
Method: GET

Returns:
JsonResponse: JSON response with data or error message
JSONResponse: JSON response with data or error message
"""
try:
# Get the database service from the Supabase client
db_service = supabase.get_database_service()

# Example: Fetch data from a table (replace 'your_table' with an actual table name)
# For demonstration purposes only
table_name = "example_table" # Replace with your actual table name

try:
# Try to fetch data from the table
data = db_service.fetch_data(table=table_name, limit=10)
return JsonResponse(
return JSONResponse(
{
"success": True,
"data": data,
"message": f"Successfully fetched data from {table_name}",
}
)
except Exception as e:
# If the table doesn't exist or there's another error, log it
logger.warning(f"Error fetching data: {str(e)}")

# Return a fallback response showing the Supabase connection is working
return JsonResponse(
return JSONResponse(
{
"success": True,
"data": None,
Expand All @@ -57,57 +48,34 @@ def example_supabase_view(request):
)

except Exception as e:
# Log any unexpected errors
logger.exception(f"Unexpected error in example_supabase_view: {str(e)}")

# Return an error response
return JsonResponse(
{
"success": False,
"error": str(e),
"message": "An error occurred while connecting to Supabase.",
},
status=500,
raise HTTPException(
status_code=500, detail="An error occurred while connecting to Supabase."
)


@csrf_exempt
@require_http_methods(["GET"])
def supabase_health_check(request):
@app.get("/api/supabase/health/")
async def supabase_health_check():
"""
Health check endpoint for Supabase connection.

This view attempts to initialize the Supabase client and reports
This endpoint attempts to initialize the Supabase client and reports
whether the connection is working properly.

URL: /api/supabase/health/
Method: GET

Returns:
JsonResponse: JSON response with connection status
JSONResponse: JSON response with connection status
"""
try:
# Get the raw Supabase client to check if it's initialized
raw_client = supabase.get_raw_client()

# If we get here, the client was initialized successfully
return JsonResponse(
return JSONResponse(
{
"status": "ok",
"message": "Supabase client is configured correctly",
"supabase_url": raw_client.supabase_url, # Safe to show the URL
"supabase_url": raw_client.supabase_url,
}
)
except Exception as e:
# Log the error
logger.exception(f"Supabase health check failed: {str(e)}")

# Return an error response
return JsonResponse(
{
"status": "error",
"message": "Supabase client configuration error",
"error": str(e),
},
status=500,
raise HTTPException(
status_code=500, detail="Supabase client configuration error"
)
Loading