Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
from typing import List, Dict, Optional
|
2 |
from datetime import datetime, timedelta
|
3 |
-
from fastapi import FastAPI, HTTPException, Query, Body
|
4 |
from pydantic import BaseModel, validator
|
5 |
import json
|
6 |
import os
|
@@ -78,6 +78,41 @@ def clean_old_data():
|
|
78 |
load_data()
|
79 |
|
80 |
# --- API Endpoints ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
|
82 |
@app.post("/entry/", response_model=UserEntry, status_code=201)
|
83 |
async def create_user_entry(entry_data: UserEntry):
|
|
|
1 |
from typing import List, Dict, Optional
|
2 |
from datetime import datetime, timedelta
|
3 |
+
from fastapi import FastAPI, HTTPException, Query, Body, Request
|
4 |
from pydantic import BaseModel, validator
|
5 |
import json
|
6 |
import os
|
|
|
78 |
load_data()
|
79 |
|
80 |
# --- API Endpoints ---
|
81 |
+
@app.post("/auto_entry/", response_model=UserEntry, status_code=201)
|
82 |
+
async def create_auto_user_entry(
|
83 |
+
request: Request,
|
84 |
+
device_type: str = Body(...),
|
85 |
+
timestamp: Optional[datetime] = Body(None)
|
86 |
+
):
|
87 |
+
"""
|
88 |
+
Endpoint to automatically record user entry by extracting the IP address
|
89 |
+
from the request and taking device_type and optional timestamp as input.
|
90 |
+
"""
|
91 |
+
try:
|
92 |
+
# Automatically extract the client's IP address
|
93 |
+
ip_address = request.client.host
|
94 |
+
|
95 |
+
# If timestamp is not provided, use the current time
|
96 |
+
if not timestamp:
|
97 |
+
timestamp = datetime.now()
|
98 |
+
|
99 |
+
# Create a UserEntry object
|
100 |
+
entry_data = UserEntry(
|
101 |
+
ip_address=ip_address,
|
102 |
+
device_type=device_type,
|
103 |
+
timestamp=timestamp
|
104 |
+
)
|
105 |
+
|
106 |
+
# Save the entry
|
107 |
+
user_data[ip_address] = entry_data.dict()
|
108 |
+
save_data()
|
109 |
+
|
110 |
+
return entry_data
|
111 |
+
|
112 |
+
except ValueError as ve:
|
113 |
+
raise HTTPException(status_code=400, detail=str(ve))
|
114 |
+
except Exception as e:
|
115 |
+
raise HTTPException(status_code=500, detail=f"Internal server error: {e}")
|
116 |
|
117 |
@app.post("/entry/", response_model=UserEntry, status_code=201)
|
118 |
async def create_user_entry(entry_data: UserEntry):
|