File size: 2,155 Bytes
f4e3196
 
21011da
f4e3196
21011da
f4e3196
de093bc
feca41c
f4e3196
21011da
 
 
 
 
 
 
 
f4e3196
 
 
 
 
 
0700888
f4e3196
de093bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21011da
f4e3196
 
 
 
 
21011da
f4e3196
676e2a2
f4e3196
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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.
@app.on_event("startup")
async def startup_event():
    # Start the continuous function as a background task.
    asyncio.create_task(your_continuous_function())

@app.get("/test_api")
async def test_api():
    return "kpi_result"