rohanshaw commited on
Commit
ef16e35
·
1 Parent(s): 88c91e0

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +45 -1
api.py CHANGED
@@ -1,13 +1,21 @@
1
  # Importing the required libraries
2
  import cv2
3
- from fastapi import FastAPI, File, UploadFile, HTTPException, Response, Form
4
  from fastapi.middleware.cors import CORSMiddleware
5
  import numpy as np
6
  import json
 
7
  import base64
 
 
8
 
9
  app = FastAPI()
10
 
 
 
 
 
 
11
  # Allowing CORS (Cross-Origin Resource Sharing)
12
  app.add_middleware(
13
  CORSMiddleware,
@@ -17,6 +25,13 @@ app.add_middleware(
17
  allow_headers=["*"],
18
  )
19
 
 
 
 
 
 
 
 
20
  # @app.on_event("startup")
21
  # def save_openapi_json():
22
  # openapi_data = app.openapi()
@@ -139,5 +154,34 @@ async def retrieve_message(image: UploadFile = File(...)):
139
  decoded_message = show_data(img)
140
 
141
  return {"decoded_message": decoded_message}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  except Exception as e:
143
  raise HTTPException(status_code=500, detail=str(e))
 
1
  # Importing the required libraries
2
  import cv2
3
+ from fastapi import FastAPI, File, UploadFile, HTTPException, Form, Depends
4
  from fastapi.middleware.cors import CORSMiddleware
5
  import numpy as np
6
  import json
7
+ import datetime
8
  import base64
9
+ from motor.motor_asyncio import AsyncIOMotorClient
10
+ from pymongo.server_api import ServerApi
11
 
12
  app = FastAPI()
13
 
14
+ # MongoDB connection settings
15
+ MONGODB_URL = "mongodb+srv://rohanshaw-1-authdata:[email protected]/?retryWrites=true&w=majority"
16
+ DATABASE_NAME = "AuthData"
17
+ COLLECTION_NAME = "inkognitoUsers"
18
+
19
  # Allowing CORS (Cross-Origin Resource Sharing)
20
  app.add_middleware(
21
  CORSMiddleware,
 
25
  allow_headers=["*"],
26
  )
27
 
28
+ # MongoDB client initialization
29
+ async def get_database_client():
30
+ client = AsyncIOMotorClient(MONGODB_URL)
31
+ database = client[DATABASE_NAME]
32
+ yield database
33
+ client.close()
34
+
35
  # @app.on_event("startup")
36
  # def save_openapi_json():
37
  # openapi_data = app.openapi()
 
154
  decoded_message = show_data(img)
155
 
156
  return {"decoded_message": decoded_message}
157
+ except Exception as e:
158
+ raise HTTPException(status_code=500, detail=str(e))
159
+
160
+ @app.post("/save_user_data")
161
+ async def save_user_data(name: str = Form(...), email: str = Form(...), database: AsyncIOMotorClient = Depends(get_database_client)):
162
+ try:
163
+ # Save user data to MongoDB
164
+ users_collection = database[COLLECTION_NAME]
165
+ user_data = {
166
+ "name": name,
167
+ "email": email,
168
+ "first_login": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
169
+ }
170
+ result = await users_collection.insert_one(user_data)
171
+
172
+ # Return success response
173
+ return {"message": "User data saved successfully"}
174
+ except Exception as e:
175
+ raise HTTPException(status_code=500, detail=str(e))
176
+
177
+ @app.get("/get_all_users")
178
+ async def get_all_users(database: AsyncIOMotorClient = Depends(get_database_client)):
179
+ try:
180
+ # Retrieve all users from MongoDB
181
+ users_collection = database[COLLECTION_NAME]
182
+ all_users = await users_collection.find({}, {"_id": 0}).to_list(length=None)
183
+
184
+ # Return the list of all users
185
+ return {"users": all_users}
186
  except Exception as e:
187
  raise HTTPException(status_code=500, detail=str(e))