Skip to content

Commit e83fb5b

Browse files
fix: Returns a JSON response if desired result is not found (#234)
Returns - [] for all GET API responses if the desired output is not found ### Fixes bug(s) - #120 - #107
1 parent 5a6b6d1 commit e83fb5b

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

folksonomy/api.py

+34-10
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,12 @@ async def product_stats(response: Response,
291291
# )
292292
# out2 = await cur.fetchone()
293293
# import pdb;pdb.set_trace()
294-
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing":timing})
295294

295+
return JSONResponse(
296+
status_code=200,
297+
content=out[0] if out and out[0] is not None else [],
298+
headers={"x-pg-timing": timing}
299+
)
296300

297301
@app.get("/products", response_model=List[ProductList])
298302
async def product_list(response: Response,
@@ -320,8 +324,12 @@ async def product_list(response: Response,
320324
params
321325
)
322326
out = await cur.fetchone()
323-
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing":timing})
324327

328+
return JSONResponse(
329+
status_code=200,
330+
content=out[0] if out and out[0] is not None else [],
331+
headers={"x-pg-timing": timing}
332+
)
325333

326334
@app.get("/product/{product}", response_model=List[ProductTag])
327335
async def product_tags_list(response: Response,
@@ -340,9 +348,13 @@ async def product_tags_list(response: Response,
340348
(product, owner),
341349
)
342350
out = await cur.fetchone()
343-
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
344-
345351

352+
return JSONResponse(
353+
status_code=200,
354+
content=out[0] if out and out[0] is not None else [],
355+
headers={"x-pg-timing": timing}
356+
)
357+
346358
@app.get("/product/{product}/{k}", response_model=ProductTag)
347359
async def product_tag(response: Response,
348360
product: str, k: str, owner='',
@@ -379,10 +391,12 @@ async def product_tag(response: Response,
379391
(product, owner, key),
380392
)
381393
out = await cur.fetchone()
382-
if out:
383-
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
384-
else:
385-
return JSONResponse(status_code=404, content=None)
394+
395+
return JSONResponse(
396+
status_code=200,
397+
content=out[0] if out and out[0] is not None else [],
398+
headers={"x-pg-timing": timing}
399+
)
386400

387401

388402
@app.get("/product/{product}/{k}/versions", response_model=List[ProductTag])
@@ -407,7 +421,13 @@ async def product_tag_list_versions(response: Response,
407421
(product, owner, k),
408422
)
409423
out = await cur.fetchone()
410-
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
424+
425+
return JSONResponse(
426+
status_code=200,
427+
content=out[0] if out and out[0] is not None else [],
428+
headers={"x-pg-timing": timing}
429+
)
430+
411431

412432

413433
@app.post("/product")
@@ -569,8 +589,12 @@ async def keys_list(response: Response,
569589
(owner,)
570590
)
571591
out = await cur.fetchone()
572-
return JSONResponse(status_code=200, content=out[0], headers={"x-pg-timing": timing})
573592

593+
return JSONResponse(
594+
status_code=200,
595+
content=out[0] if out and out[0] is not None else [],
596+
headers={"x-pg-timing": timing}
597+
)
574598

575599
@app.get("/values/{k}", response_model=List[ValueCount])
576600
async def get_unique_values(response: Response,

tests/test_main.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,7 @@ def test_product_missing(with_sample):
297297
with TestClient(app) as client:
298298
response = client.get("/product/0000000000000")
299299
assert response.status_code == 200
300-
assert response.json() == None
301-
return response.json()
300+
assert response.json() == []
302301

303302

304303
def test_product_key(with_sample):
@@ -326,8 +325,8 @@ def test_key_stripped_on_get(with_sample):
326325
def test_product_key_missing(with_sample):
327326
with TestClient(app) as client:
328327
response = client.get(f"/product/{BARCODE_1}/not-existing")
329-
assert response.status_code == 404
330-
assert response.json() == None
328+
assert response.status_code == 200
329+
assert response.json() == []
331330

332331

333332
def test_product_key_versions(with_sample):
@@ -350,7 +349,7 @@ def test_product_key_versions_missing(with_sample):
350349
with TestClient(app) as client:
351350
response = client.get(f"/product/{BARCODE_3}/not-existing/versions")
352351
assert response.status_code == 200
353-
assert response.json() == None
352+
assert response.json() == []
354353

355354

356355
def test_products_stats_key(with_sample):

0 commit comments

Comments
 (0)