xyz69 commited on
Commit
6eaf9a7
1 Parent(s): a9e96e1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -3
app.py CHANGED
@@ -4,6 +4,7 @@ from fastapi.responses import JSONResponse
4
  import httpx
5
  import random
6
  import string
 
7
 
8
  app = FastAPI()
9
 
@@ -30,13 +31,23 @@ async def jsongen(url):
30
  @app.middleware("http")
31
  async def add_custom_fields(request: Request, call_next):
32
  response = await call_next(request)
 
 
33
  if response.status_code == 200 and isinstance(response, JSONResponse):
34
- response_data = response.body.decode("utf-8")
 
 
 
 
 
 
35
  modified_data = {
36
- "creator": "EYEPATCH",
37
- **response.json(),
 
38
  }
39
  return JSONResponse(modified_data)
 
40
  return response
41
 
42
  # Route to fetch trending videos
 
4
  import httpx
5
  import random
6
  import string
7
+ import json
8
 
9
  app = FastAPI()
10
 
 
31
  @app.middleware("http")
32
  async def add_custom_fields(request: Request, call_next):
33
  response = await call_next(request)
34
+
35
+ # Check if the response is JSON and has a body
36
  if response.status_code == 200 and isinstance(response, JSONResponse):
37
+ original_body = await response.body()
38
+ try:
39
+ response_data = json.loads(original_body.decode("utf-8"))
40
+ except Exception:
41
+ return response # If the body is not JSON, return the original response
42
+
43
+ # Add custom fields to the response
44
  modified_data = {
45
+ "creator": "my name",
46
+ "api_version": "1.0",
47
+ **response_data,
48
  }
49
  return JSONResponse(modified_data)
50
+
51
  return response
52
 
53
  # Route to fetch trending videos