Skip to content

Commit 485fbcb

Browse files
Fetch content post api added
1 parent 1bab811 commit 485fbcb

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/app.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,51 @@ async def get_document(filepath):
13621362
span.set_status(Status(StatusCode.ERROR, str(e)))
13631363
return jsonify({"error": str(e)}), 500
13641364

1365+
@bp.route("/fetch-azure-search-content", methods=["POST"])
1366+
async def fetch_azure_search_content():
1367+
try:
1368+
print("Fetching content from Azure Search")
1369+
request_json = await request.get_json()
1370+
url = request_json.get("url")
1371+
1372+
if not url:
1373+
track_event_if_configured("FetchAzureSearchContentFailed", {"error": "URL is required"})
1374+
return jsonify({"error": "URL is required"}), 400
1375+
1376+
#Get Azure AD token
1377+
credential = DefaultAzureCredentialSync()
1378+
token = credential.get_token("https://search.azure.com/.default")
1379+
access_token = token.token
1380+
1381+
def fetch_content():
1382+
try:
1383+
response = requests.get(
1384+
url,
1385+
headers={
1386+
"Authorization": f"Bearer {access_token}",
1387+
"Content-Type": "application/json"
1388+
},
1389+
timeout=10
1390+
)
1391+
if response.status_code == 200:
1392+
data = response.json()
1393+
content = data.get("content", "")
1394+
return content
1395+
else:
1396+
return
1397+
except Exception as e:
1398+
logging.exception("Error fetching content from Azure Search")
1399+
print(f"Error fetching content from Azure Search: {str(e)}")
1400+
return f"Error: {str(e)}"
1401+
1402+
content = await asyncio.to_thread(fetch_content)
1403+
return jsonify({"content": content}), 200
1404+
1405+
except Exception as e:
1406+
logging.exception("Exception in /fetch-azure-search-content")
1407+
return jsonify({"error": str(e)}), 500
1408+
1409+
13651410
async def generate_title(conversation_messages):
13661411
# make sure the messages are sorted by _ts descending
13671412
title_prompt = app_settings.azure_openai.title_prompt

0 commit comments

Comments
 (0)