RAG_AI_BOT / app.py
willco-afk's picture
Update app.py
19da2df verified
raw
history blame
2.82 kB
from fastapi import FastAPI, HTTPException
import requests
import oci
import json
from datetime import datetime
app = FastAPI()
# Oracle Cloud Configurations (Ensure your OCI config file is set up correctly)
config = oci.config.from_file("~/.oci/config", "DEFAULT")
signer = oci.auth.signers.get_resource_principals_signer()
# Endpoint URL for Oracle AI Service (Replace region appropriately)
oci_endpoint = "https://anomalydetection.aiservice.{{region}}.oci.oraclecloud.com/20210101/aiPrivateEndpoints"
# Request headers including authentication (ensure the correct signing/authentication process)
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {access_token}" # Use the appropriate method to authenticate
}
@app.post("/create-private-endpoint")
async def create_private_endpoint(compartment_id: str, subnet_id: str, display_name: str, dns_zones: list):
"""Creates a private reverse connection endpoint in Oracle Cloud"""
# Prepare the request body
request_body = {
"compartmentId": compartment_id,
"subnetId": subnet_id,
"displayName": display_name,
"dnsZones": dns_zones,
"definedTags": {},
"freeformTags": {}
}
response = requests.post(
oci_endpoint,
headers=headers,
data=json.dumps(request_body)
)
if response.status_code == 202:
return {"message": "Private endpoint created successfully", "status": response.status_code}
else:
raise HTTPException(status_code=response.status_code, detail=response.json())
@app.put("/update-private-endpoint/{endpoint_id}")
async def update_private_endpoint(endpoint_id: str, display_name: str, dns_zones: list):
"""Updates a private reverse connection endpoint"""
update_url = f"{oci_endpoint}/{endpoint_id}"
request_body = {
"displayName": display_name,
"dnsZones": dns_zones,
"definedTags": {},
"freeformTags": {}
}
response = requests.put(
update_url,
headers=headers,
data=json.dumps(request_body)
)
if response.status_code == 202:
return {"message": "Private endpoint updated successfully", "status": response.status_code}
else:
raise HTTPException(status_code=response.status_code, detail=response.json())
@app.get("/get-private-endpoint/{endpoint_id}")
async def get_private_endpoint(endpoint_id: str):
"""Retrieves a private reverse connection endpoint by its ID"""
get_url = f"{oci_endpoint}/{endpoint_id}"
response = requests.get(
get_url,
headers=headers
)
if response.status_code == 200:
return response.json()
else:
raise HTTPException(status_code=response.status_code, detail=response.json())