KevanSoon commited on
Commit
6d282bc
·
1 Parent(s): bfd900e

testing endpoint

Browse files
Files changed (1) hide show
  1. app.py +102 -102
app.py CHANGED
@@ -48,7 +48,7 @@ from PIL import Image
48
 
49
  # pytesseract
50
  import pytesseract
51
- from auth.clerk import verify_clerk_jwt
52
 
53
  # --- MODIFIED: Replaced old tool imports with the new one ---
54
  from tools.tools import analyze_contract
@@ -125,117 +125,117 @@ async def analyze_contract_endpoint(file: UploadFile = File(...)):
125
  )
126
 
127
 
128
- @app.post("/upload")
129
- async def upload_file(authorization: str = Header(...), file: UploadFile = File(...)):
130
- if not authorization.startswith("Bearer "):
131
- raise HTTPException(status_code=401, detail="Missing Bearer token")
132
-
133
- token = authorization.split(" ")[1]
134
- claims = await verify_clerk_jwt(token)
135
-
136
- user_id = claims.get("sub") # Clerk user ID
137
- filename = f"{user_id}/{uuid.uuid4()}.png"
138
-
139
- # Upload to Supabase Storage
140
- async with httpx.AsyncClient() as client:
141
- upload_resp = await client.post(
142
- f"{SUPABASE_URL}/storage/v1/object/user-documents/{filename}",
143
- headers={
144
- "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
145
- "Content-Type": file.content_type,
146
- },
147
- content=await file.read(),
148
- )
149
 
150
- if upload_resp.status_code != 200:
151
- raise HTTPException(
152
- status_code=500, detail="Failed to upload to Supabase Storage"
153
- )
154
 
155
- file_url = f"user-documents/{filename}"
156
-
157
- # Insert metadata to `documents` table
158
- async with httpx.AsyncClient() as client:
159
- insert_resp = await client.post(
160
- f"{SUPABASE_URL}/rest/v1/documents",
161
- headers={
162
- "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
163
- "apikey": SUPABASE_SERVICE_ROLE_KEY,
164
- "Content-Type": "application/json",
165
- "Prefer": "return=representation",
166
- },
167
- json={
168
- "user_id": user_id,
169
- "filename": filename.split("/")[-1],
170
- "file_url": file_url,
171
- },
172
- )
173
 
174
- if insert_resp.status_code >= 300:
175
- raise HTTPException(
176
- status_code=500, detail="Failed to insert document metadata"
177
- )
 
 
 
 
 
 
178
 
179
- return {"message": f"File uploaded as {filename}"}
 
 
 
180
 
 
181
 
182
- @app.get("/api/documents")
183
- async def get_user_documents(
184
- credentials: HTTPAuthorizationCredentials = Depends(security),
185
- ):
186
- token = credentials.credentials
187
- claims = await verify_clerk_jwt(token)
188
- user_id = claims.get("sub")
189
- if not user_id:
190
- raise HTTPException(status_code=401, detail="Invalid user")
191
-
192
- # Step 1: Get documents from Supabase
193
- async with httpx.AsyncClient() as client:
194
- resp = await client.get(
195
- f"{SUPABASE_URL}/rest/v1/documents?user_id=eq.{user_id}",
196
- headers={
197
- "apikey": SUPABASE_SERVICE_ROLE_KEY,
198
- "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
199
- "Accept": "application/json",
200
- },
201
- )
202
 
203
- if resp.status_code != 200:
204
- raise HTTPException(status_code=500, detail="Failed to fetch documents")
205
-
206
- documents = resp.json()
207
-
208
- # Step 2: Get signed URLs for each file
209
- async with httpx.AsyncClient() as client:
210
- for doc in documents:
211
- file_path = doc["file_url"].split("user-documents/", 1)[-1]
212
- if not file_path:
213
- doc["signed_url"] = None
214
- continue
215
-
216
- signed_url_resp = await client.post(
217
- f"{SUPABASE_URL}/storage/v1/object/sign/user-documents/{file_path}",
218
- headers={
219
- "apikey": SUPABASE_SERVICE_ROLE_KEY,
220
- "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
221
- # "Content-Type": "application/json"
222
- },
223
- json={"expiresIn": 3600}, # 1 hour
224
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
225
 
226
- if signed_url_resp.status_code == 200:
227
- print(
228
- f"{SUPABASE_URL}/storage/v1{signed_url_resp.json().get('signedURL')}"
229
- )
230
- doc["signed_url"] = (
231
- f"{SUPABASE_URL}/storage/v1{signed_url_resp.json().get('signedURL')}"
232
- )
233
 
234
- else:
235
- doc["signed_url"] = None
236
- print(documents)
237
 
238
- return documents
239
 
240
 
241
  # --- END: NEW ENDPOINT FOR THE REFACTORED TOOL ---
 
48
 
49
  # pytesseract
50
  import pytesseract
51
+ # from auth.clerk import verify_clerk_jwt
52
 
53
  # --- MODIFIED: Replaced old tool imports with the new one ---
54
  from tools.tools import analyze_contract
 
125
  )
126
 
127
 
128
+ # @app.post("/upload")
129
+ # async def upload_file(authorization: str = Header(...), file: UploadFile = File(...)):
130
+ # if not authorization.startswith("Bearer "):
131
+ # raise HTTPException(status_code=401, detail="Missing Bearer token")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
+ # token = authorization.split(" ")[1]
134
+ # claims = await verify_clerk_jwt(token)
 
 
135
 
136
+ # user_id = claims.get("sub") # Clerk user ID
137
+ # filename = f"{user_id}/{uuid.uuid4()}.png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
139
+ # # Upload to Supabase Storage
140
+ # async with httpx.AsyncClient() as client:
141
+ # upload_resp = await client.post(
142
+ # f"{SUPABASE_URL}/storage/v1/object/user-documents/{filename}",
143
+ # headers={
144
+ # "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
145
+ # "Content-Type": file.content_type,
146
+ # },
147
+ # content=await file.read(),
148
+ # )
149
 
150
+ # if upload_resp.status_code != 200:
151
+ # raise HTTPException(
152
+ # status_code=500, detail="Failed to upload to Supabase Storage"
153
+ # )
154
 
155
+ # file_url = f"user-documents/{filename}"
156
 
157
+ # # Insert metadata to `documents` table
158
+ # async with httpx.AsyncClient() as client:
159
+ # insert_resp = await client.post(
160
+ # f"{SUPABASE_URL}/rest/v1/documents",
161
+ # headers={
162
+ # "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
163
+ # "apikey": SUPABASE_SERVICE_ROLE_KEY,
164
+ # "Content-Type": "application/json",
165
+ # "Prefer": "return=representation",
166
+ # },
167
+ # json={
168
+ # "user_id": user_id,
169
+ # "filename": filename.split("/")[-1],
170
+ # "file_url": file_url,
171
+ # },
172
+ # )
 
 
 
 
173
 
174
+ # if insert_resp.status_code >= 300:
175
+ # raise HTTPException(
176
+ # status_code=500, detail="Failed to insert document metadata"
177
+ # )
178
+
179
+ # return {"message": f"File uploaded as {filename}"}
180
+
181
+
182
+ # @app.get("/api/documents")
183
+ # async def get_user_documents(
184
+ # credentials: HTTPAuthorizationCredentials = Depends(security),
185
+ # ):
186
+ # token = credentials.credentials
187
+ # claims = await verify_clerk_jwt(token)
188
+ # user_id = claims.get("sub")
189
+ # if not user_id:
190
+ # raise HTTPException(status_code=401, detail="Invalid user")
191
+
192
+ # # Step 1: Get documents from Supabase
193
+ # async with httpx.AsyncClient() as client:
194
+ # resp = await client.get(
195
+ # f"{SUPABASE_URL}/rest/v1/documents?user_id=eq.{user_id}",
196
+ # headers={
197
+ # "apikey": SUPABASE_SERVICE_ROLE_KEY,
198
+ # "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
199
+ # "Accept": "application/json",
200
+ # },
201
+ # )
202
+
203
+ # if resp.status_code != 200:
204
+ # raise HTTPException(status_code=500, detail="Failed to fetch documents")
205
+
206
+ # documents = resp.json()
207
+
208
+ # # Step 2: Get signed URLs for each file
209
+ # async with httpx.AsyncClient() as client:
210
+ # for doc in documents:
211
+ # file_path = doc["file_url"].split("user-documents/", 1)[-1]
212
+ # if not file_path:
213
+ # doc["signed_url"] = None
214
+ # continue
215
+
216
+ # signed_url_resp = await client.post(
217
+ # f"{SUPABASE_URL}/storage/v1/object/sign/user-documents/{file_path}",
218
+ # headers={
219
+ # "apikey": SUPABASE_SERVICE_ROLE_KEY,
220
+ # "Authorization": f"Bearer {SUPABASE_SERVICE_ROLE_KEY}",
221
+ # # "Content-Type": "application/json"
222
+ # },
223
+ # json={"expiresIn": 3600}, # 1 hour
224
+ # )
225
 
226
+ # if signed_url_resp.status_code == 200:
227
+ # print(
228
+ # f"{SUPABASE_URL}/storage/v1{signed_url_resp.json().get('signedURL')}"
229
+ # )
230
+ # doc["signed_url"] = (
231
+ # f"{SUPABASE_URL}/storage/v1{signed_url_resp.json().get('signedURL')}"
232
+ # )
233
 
234
+ # else:
235
+ # doc["signed_url"] = None
236
+ # print(documents)
237
 
238
+ # return documents
239
 
240
 
241
  # --- END: NEW ENDPOINT FOR THE REFACTORED TOOL ---