Download app.py from PD03/AgentKit: direct link, hf CLI and curl.
- Browser
- Download file 2.49 kB
-
https://huggingface.co/spaces/PD03/AgentKit/resolve/ea1bd981c4cdc143ee11df875e2f5869f6af5b13/app.py
- Command line
-
hf download hf://spaces/PD03/AgentKit@ea1bd981c4cdc143ee11df875e2f5869f6af5b13/app.py
-
curl -L -o app.py https://huggingface.co/spaces/PD03/AgentKit/resolve/ea1bd981c4cdc143ee11df875e2f5869f6af5b13/app.py
2.49 kB
| import os | |
| import httpx | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| app = FastAPI( | |
| title="SAP Purchase Order API Demo", | |
| description=( | |
| "Fetches purchase order data from SAP Sandbox API.\n\n" | |
| "Designed for integration with OpenAI Agentkit UI — " | |
| "Agentkit auto-discovers endpoints from `/openapi.json`." | |
| ), | |
| version="1.0.0", | |
| ) | |
| # Allow all origins for Agentkit / external calls | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Read secret from Hugging Face environment | |
| SAP_API_KEY = os.getenv("SAP_API_KEY") | |
| SAP_BASE_URL = ( | |
| "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata4/" | |
| "sap/api_purchaseorder_2/srvd_a2x/sap/purchaseorder/0001/PurchaseOrder?$top=50" | |
| ) | |
| # --- Models --- | |
| class PurchaseOrderItem(BaseModel): | |
| PurchaseOrder: str | None = None | |
| Supplier: str | None = None | |
| CreatedByUser: str | None = None | |
| CreatedDateTime: str | None = None | |
| class PurchaseOrderResponse(BaseModel): | |
| source: str | |
| count: int | |
| data: list[PurchaseOrderItem] | dict | |
| # --- Routes --- | |
| async def health_check(): | |
| """Simple endpoint to verify the backend is running.""" | |
| return {"status": "ok", "message": "SAP Purchase Order API backend is running"} | |
| async def get_purchase_orders(): | |
| """Fetches purchase orders from SAP Sandbox API""" | |
| if not SAP_API_KEY: | |
| raise HTTPException(status_code=500, detail="SAP_API_KEY not found in environment") | |
| headers = {"APIKey": SAP_API_KEY} | |
| try: | |
| async with httpx.AsyncClient(timeout=30.0) as client: | |
| resp = await client.get(SAP_BASE_URL, headers=headers) | |
| resp.raise_for_status() | |
| data = resp.json() | |
| po_items = data.get("value", []) | |
| return {"source": "SAP Sandbox", "count": len(po_items), "data": po_items} | |
| except httpx.HTTPStatusError as e: | |
| raise HTTPException(status_code=e.response.status_code, detail=f"SAP API error: {e.response.text}") | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |