Spaces:
Sleeping
Sleeping
import asyncio | |
from fastapi import FastAPI | |
from fastapi.middleware.cors import CORSMiddleware | |
import requests | |
import pandas as pd | |
import json | |
import httpx | |
app = FastAPI() | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
global page | |
page = 1 | |
# Declare the continuous function as an async function. | |
async def your_continuous_function(): | |
import pandas as pd | |
import requests | |
import json,httpx | |
global page | |
async with httpx.AsyncClient() as client: | |
while True: | |
print("data fetcher running.....") | |
# Initialize an empty DataFrame to store the combined data | |
combined_df = pd.DataFrame() | |
# Update the payload for each page | |
url = "https://dev3.api.curfox.parallaxtec.com/api/ml/order-list?sort=id&paginate=500&page="+str(page) | |
payload = {} | |
headers = { | |
'Accept': 'application/json', | |
'X-Tenant': 'royalexpress' | |
} | |
response = requests.request("GET", url, headers=headers, data=payload) | |
# Sample JSON response | |
json_response = response.json() | |
# Extracting 'data' for conversion | |
data = json_response['data'] | |
df = pd.json_normalize(data) | |
# Concatenate the current page's DataFrame with the combined DataFrame | |
combined_df = pd.concat([combined_df, df], ignore_index=True) | |
data = combined_df[combined_df['status.name'].isin(['RETURN TO CLIENT', 'DELIVERED'])] | |
print("data collected from page : "+str(page)) | |
page+=1 | |
await asyncio.sleep(60) # Adjust the sleep interval as needed | |
# Create a startup event. | |
async def startup_event(): | |
# Start the continuous function as a background task. | |
asyncio.create_task(your_continuous_function()) | |
async def test_api(): | |
return "kpi_result" |