sathish2352 commited on
Commit
cbd1739
·
verified ·
1 Parent(s): 4ebf134

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -15
app.py CHANGED
@@ -1,21 +1,48 @@
1
  # app.py
2
- from fastapi import FastAPI, Request
3
- from fastapi.responses import JSONResponse
 
 
4
 
5
  app = FastAPI()
6
 
 
 
 
 
 
7
  @app.post("/echo")
8
  async def echo(request: Request):
9
- data = await request.json()
10
- if len(data)==0:
11
- return JSONResponse(content={"you_sent": "F you"})
12
- lst=""
13
- c=0
14
- for i in data:
15
- if c%2==0:
16
- lst+=i.upper()
17
- else:
18
- lst+=i.lower()
19
- c+=1
20
-
21
- return JSONResponse(content={"you_sent": lst})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # app.py
2
+ from fastapi import FastAPI, Request, HTTPException
3
+ from fastapi.responses import StreamingResponse
4
+ import qrcode
5
+ import io
6
 
7
  app = FastAPI()
8
 
9
+ def stylize_text(text: str) -> str:
10
+ """Return the text with alternating upper/lower‑case characters (old behaviour)."""
11
+ return "".join(ch.upper() if idx % 2 == 0 else ch.lower() for idx, ch in enumerate(text))
12
+
13
+
14
  @app.post("/echo")
15
  async def echo(request: Request):
16
+ """Accept JSON payload and respond with a QR‑code PNG encoding the stylised text."""
17
+ try:
18
+ payload = await request.json()
19
+ except Exception:
20
+ raise HTTPException(status_code=400, detail="Expected a JSON body")
21
+
22
+ if not payload:
23
+ raise HTTPException(status_code=400, detail="Payload cannot be empty")
24
+
25
+ # Normalise input
26
+ if isinstance(payload, str):
27
+ raw_text = payload
28
+ elif isinstance(payload, dict) and "text" in payload:
29
+ raw_text = str(payload["text"])
30
+ else:
31
+ # Fallback: convert entire JSON object to string
32
+ raw_text = str(payload)
33
+
34
+ # Apply old alternating‑case transformation
35
+ qr_text = stylize_text(raw_text)
36
+
37
+ # Build QR‑code
38
+ qr = qrcode.QRCode(box_size=10, border=4)
39
+ qr.add_data(qr_text)
40
+ qr.make(fit=True)
41
+ img = qr.make_image(fill_color="black", back_color="white")
42
+
43
+ # Send back PNG
44
+ buffer = io.BytesIO()
45
+ img.save(buffer, format="PNG")
46
+ buffer.seek(0)
47
+
48
+ return StreamingResponse(buffer, media_type="image/png")