-
Notifications
You must be signed in to change notification settings - Fork 0
Add Resend email endpoint and utility #45
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| """NX AI - FastAPI, Python, Postgres, tsvector""" | ||
| """Python - FastAPI, Postgres, tsvector""" | ||
|
|
||
| # Current Version | ||
| __version__ = "2.0.5" | ||
| __version__ = "2.0.6" |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,11 +2,9 @@ | |
| from app import __version__ | ||
| import os | ||
| from app.utils.make_meta import make_meta | ||
|
|
||
| from fastapi import APIRouter, Query, Path, Body, HTTPException | ||
|
|
||
| from app.utils.db import get_db_connection | ||
|
|
||
| from .utils.send_email import send_email_resend | ||
|
|
||
| router = APIRouter() | ||
| base_url = os.getenv("BASE_URL", "http://localhost:8000") | ||
|
|
@@ -21,6 +19,35 @@ def root() -> dict: | |
| return {"meta": meta} | ||
| meta = make_meta("success", "Resend endpoint") | ||
| data = [ | ||
| {"action,": f"send email"}, | ||
| {"action": "send email"}, | ||
| ] | ||
| return {"meta": meta, "data": data} | ||
|
|
||
|
|
||
| # POST endpoint to send email | ||
| from fastapi import status | ||
| from pydantic import BaseModel, EmailStr | ||
|
|
||
| class EmailRequest(BaseModel): | ||
| to: EmailStr | ||
| subject: str | ||
| html: str | ||
| sender: EmailStr | ||
|
|
||
| @router.post("/resend", status_code=status.HTTP_202_ACCEPTED) | ||
| def send_email(request: EmailRequest): | ||
| """POST /resend endpoint to send email via Resend API.""" | ||
| if not RESEND_API_KEY: | ||
| meta = make_meta("error", "RESEND_API_KEY missing. Please set it in your .env file.") | ||
| return {"meta": meta} | ||
| result = send_email_resend( | ||
goldlabelapps marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| to=request.to, | ||
| subject=request.subject, | ||
| html=request.html, | ||
| sender=request.sender | ||
| ) | ||
|
Comment on lines
+37
to
+48
This comment was marked as resolved.
Sorry, something went wrong. |
||
| if "error" in result: | ||
| meta = make_meta("error", result["error"]) | ||
| return {"meta": meta} | ||
| meta = make_meta("success", "Email sent successfully.") | ||
|
Comment on lines
+37
to
+52
|
||
| return {"meta": meta, "data": result} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # Utility to send email using Resend API | ||
| import httpx | ||
| import os | ||
|
|
||
| RESEND_API_URL = "https://api.resend.com/emails" | ||
|
|
||
| def send_email_resend(to: str, subject: str, html: str, sender: str) -> dict: | ||
| resend_api_key = os.getenv("RESEND_API_KEY") | ||
| if not resend_api_key: | ||
| return {"error": "Missing RESEND_API_KEY"} | ||
| headers = { | ||
| "Authorization": f"Bearer {resend_api_key}", | ||
| "Content-Type": "application/json" | ||
| } | ||
| payload = { | ||
| "from": sender, | ||
| "to": [to], | ||
| "subject": subject, | ||
| "html": html | ||
| } | ||
| try: | ||
| response = httpx.post(RESEND_API_URL, headers=headers, json=payload, timeout=10) | ||
| response.raise_for_status() | ||
| return response.json() | ||
| except Exception as e: | ||
| return {"error": str(e)} | ||
|
Comment on lines
+21
to
+26
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ python-dotenv>=1.0.0 | |
| psycopg2-binary>=2.9.0 | ||
| python-multipart>=0.0.20 | ||
| Faker>=25.2.0 | ||
| pydantic[email]>=2.0.0 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are several unused imports/variables here (
__version__,Query,Path,Body,HTTPException,get_db_connection, andbase_url). Removing unused items will reduce confusion and avoid lint/type-check noise as this module evolves.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot apply changes based on this feedback