File size: 1,294 Bytes
9f26e80
 
ea9f7e5
a27cd03
9f26e80
 
 
 
d0443bf
 
 
9f26e80
 
ea9f7e5
 
a27cd03
ea9f7e5
9f26e80
 
 
ea9f7e5
 
 
 
9f26e80
 
ea9f7e5
a27cd03
9f26e80
ea9f7e5
 
9f26e80
ea9f7e5
 
a27cd03
9f26e80
a27cd03
 
 
9f26e80
 
 
a27cd03
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import asyncio
import aiohttp
from fastapi import FastAPI
from sse_starlette.sse import EventSourceResponse
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI()

API_URL = "https://b917-160-179-178-105.ngrok-free.app/test-ai-call"

call_count = 0

async def event_generator():
    global call_count
    async with aiohttp.ClientSession() as session:
        while True:
            try:
                call_count += 1
                payload = {"count": call_count}

                async with session.post(API_URL, json=payload) as response:
                    if response.status == 200:
                        data = await response.json()
                        logger.info(f"Call {call_count}: API Response: {data}")
                        yield data
                    else:
                        logger.error(f"Call {call_count}: API call failed with status code: {response.status}")

            except Exception as e:
                logger.error(f"Call {call_count}: Error: {e}")

            await asyncio.sleep(10)

@app.get("/stream")
async def stream(request):
    return EventSourceResponse(event_generator())

@app.get("/")
async def root():
    return {"message": "API caller is running", "calls_made": call_count}