Update app.py
Browse files
app.py
CHANGED
|
@@ -4,17 +4,20 @@ from fastapi import FastAPI, HTTPException
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
app = FastAPI(
|
| 8 |
title="SAP Purchase Order API Demo",
|
| 9 |
description=(
|
| 10 |
-
"
|
| 11 |
"Designed for integration with OpenAI Agentkit UI — "
|
| 12 |
-
"Agentkit
|
| 13 |
),
|
| 14 |
version="1.0.0",
|
| 15 |
)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
app.add_middleware(
|
| 19 |
CORSMiddleware,
|
| 20 |
allow_origins=["*"],
|
|
@@ -23,15 +26,18 @@ app.add_middleware(
|
|
| 23 |
allow_headers=["*"],
|
| 24 |
)
|
| 25 |
|
| 26 |
-
#
|
|
|
|
|
|
|
| 27 |
SAP_API_KEY = os.getenv("SAP_API_KEY")
|
| 28 |
SAP_BASE_URL = (
|
| 29 |
"https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata4/"
|
| 30 |
"sap/api_purchaseorder_2/srvd_a2x/sap/purchaseorder/0001/PurchaseOrder?$top=50"
|
| 31 |
)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
|
|
|
| 35 |
class PurchaseOrderItem(BaseModel):
|
| 36 |
PurchaseOrder: str | None = None
|
| 37 |
Supplier: str | None = None
|
|
@@ -45,11 +51,30 @@ class PurchaseOrderResponse(BaseModel):
|
|
| 45 |
data: list[PurchaseOrderItem] | dict
|
| 46 |
|
| 47 |
|
| 48 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
@app.get("/health", summary="Health check", tags=["System"])
|
| 51 |
async def health_check():
|
| 52 |
-
"""
|
| 53 |
return {"status": "ok", "message": "SAP Purchase Order API backend is running"}
|
| 54 |
|
| 55 |
|
|
@@ -58,10 +83,10 @@ async def health_check():
|
|
| 58 |
response_model=PurchaseOrderResponse,
|
| 59 |
summary="Get Purchase Orders",
|
| 60 |
tags=["Purchase Orders"],
|
| 61 |
-
description="
|
| 62 |
)
|
| 63 |
async def get_purchase_orders():
|
| 64 |
-
"""
|
| 65 |
if not SAP_API_KEY:
|
| 66 |
raise HTTPException(status_code=500, detail="SAP_API_KEY not found in environment")
|
| 67 |
|
|
|
|
| 4 |
from fastapi.middleware.cors import CORSMiddleware
|
| 5 |
from pydantic import BaseModel
|
| 6 |
|
| 7 |
+
# -------------------------------------------------
|
| 8 |
+
# FastAPI App Configuration
|
| 9 |
+
# -------------------------------------------------
|
| 10 |
app = FastAPI(
|
| 11 |
title="SAP Purchase Order API Demo",
|
| 12 |
description=(
|
| 13 |
+
"Backend to fetch purchase order data from SAP Sandbox API.\n\n"
|
| 14 |
"Designed for integration with OpenAI Agentkit UI — "
|
| 15 |
+
"Agentkit automatically discovers endpoints from `/openapi.json`."
|
| 16 |
),
|
| 17 |
version="1.0.0",
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# Enable CORS for Agentkit and browser testing
|
| 21 |
app.add_middleware(
|
| 22 |
CORSMiddleware,
|
| 23 |
allow_origins=["*"],
|
|
|
|
| 26 |
allow_headers=["*"],
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# -------------------------------------------------
|
| 30 |
+
# Environment Variables
|
| 31 |
+
# -------------------------------------------------
|
| 32 |
SAP_API_KEY = os.getenv("SAP_API_KEY")
|
| 33 |
SAP_BASE_URL = (
|
| 34 |
"https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata4/"
|
| 35 |
"sap/api_purchaseorder_2/srvd_a2x/sap/purchaseorder/0001/PurchaseOrder?$top=50"
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# -------------------------------------------------
|
| 39 |
+
# Pydantic Models (for OpenAPI schema)
|
| 40 |
+
# -------------------------------------------------
|
| 41 |
class PurchaseOrderItem(BaseModel):
|
| 42 |
PurchaseOrder: str | None = None
|
| 43 |
Supplier: str | None = None
|
|
|
|
| 51 |
data: list[PurchaseOrderItem] | dict
|
| 52 |
|
| 53 |
|
| 54 |
+
# -------------------------------------------------
|
| 55 |
+
# Routes
|
| 56 |
+
# -------------------------------------------------
|
| 57 |
+
|
| 58 |
+
@app.get("/", tags=["Root"])
|
| 59 |
+
async def root():
|
| 60 |
+
"""Friendly welcome message for the root URL."""
|
| 61 |
+
return {
|
| 62 |
+
"message": "👋 Welcome to the SAP Purchase Order API demo!",
|
| 63 |
+
"available_endpoints": {
|
| 64 |
+
"health": "/health",
|
| 65 |
+
"purchase_orders": "/purchase-orders",
|
| 66 |
+
"docs": "/docs"
|
| 67 |
+
},
|
| 68 |
+
"instructions": (
|
| 69 |
+
"Use /purchase-orders to fetch SAP Sandbox data, "
|
| 70 |
+
"or open /docs for the interactive Swagger UI."
|
| 71 |
+
),
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
|
| 75 |
@app.get("/health", summary="Health check", tags=["System"])
|
| 76 |
async def health_check():
|
| 77 |
+
"""Check if the API is running."""
|
| 78 |
return {"status": "ok", "message": "SAP Purchase Order API backend is running"}
|
| 79 |
|
| 80 |
|
|
|
|
| 83 |
response_model=PurchaseOrderResponse,
|
| 84 |
summary="Get Purchase Orders",
|
| 85 |
tags=["Purchase Orders"],
|
| 86 |
+
description="Fetches the top 50 purchase orders from the SAP Sandbox API.",
|
| 87 |
)
|
| 88 |
async def get_purchase_orders():
|
| 89 |
+
"""Fetch purchase order data from SAP Sandbox API."""
|
| 90 |
if not SAP_API_KEY:
|
| 91 |
raise HTTPException(status_code=500, detail="SAP_API_KEY not found in environment")
|
| 92 |
|