Spaces:
Running
Running
Prathamesh Sable
commited on
Commit
·
6192fe2
1
Parent(s):
91990bf
fixed not found issue
Browse files- main.py +1 -1
- routers/product.py +1 -1
- utils/fetch_data.py +5 -2
main.py
CHANGED
|
@@ -33,7 +33,7 @@ async def log_requests(request: Request, call_next):
|
|
| 33 |
request._body = body_content
|
| 34 |
response = await call_next(request)
|
| 35 |
print(f"Request: {request.method} {request.url}")
|
| 36 |
-
print(f"Data: {body_content}")
|
| 37 |
print(f"Headers: {request.headers}")
|
| 38 |
return response
|
| 39 |
|
|
|
|
| 33 |
request._body = body_content
|
| 34 |
response = await call_next(request)
|
| 35 |
print(f"Request: {request.method} {request.url}")
|
| 36 |
+
# print(f"Data: {body_content}")
|
| 37 |
print(f"Headers: {request.headers}")
|
| 38 |
return response
|
| 39 |
|
routers/product.py
CHANGED
|
@@ -195,7 +195,7 @@ async def create_product(
|
|
| 195 |
return JSONResponse({"error": str(e)}, status_code=500)
|
| 196 |
|
| 197 |
|
| 198 |
-
@router.get("/find_barcode
|
| 199 |
async def find_product_by_barcode(barcode_number: str):
|
| 200 |
"""Endpoint to find product data using a barcode number."""
|
| 201 |
log_info(f"Find product by barcode endpoint called for barcode: {barcode_number}")
|
|
|
|
| 195 |
return JSONResponse({"error": str(e)}, status_code=500)
|
| 196 |
|
| 197 |
|
| 198 |
+
@router.get("/find_barcode")
|
| 199 |
async def find_product_by_barcode(barcode_number: str):
|
| 200 |
"""Endpoint to find product data using a barcode number."""
|
| 201 |
log_info(f"Find product by barcode endpoint called for barcode: {barcode_number}")
|
utils/fetch_data.py
CHANGED
|
@@ -5,7 +5,7 @@ async def fetch_product_data_from_api(barcode):
|
|
| 5 |
url = f"https://india.openfoodfacts.org/api/v2/product/{barcode}.json"
|
| 6 |
response = requests.get(url)
|
| 7 |
if response.status_code != 200:
|
| 8 |
-
|
| 9 |
return response.json()
|
| 10 |
|
| 11 |
def extract_product_info(product_data: dict):
|
|
@@ -13,9 +13,12 @@ def extract_product_info(product_data: dict):
|
|
| 13 |
Extracts product information (found status, name, ingredients) from OpenFoodFacts API response.
|
| 14 |
"""
|
| 15 |
found = product_data.get('status') == 1
|
|
|
|
|
|
|
|
|
|
| 16 |
product = product_data.get('product')
|
| 17 |
|
| 18 |
-
if not
|
| 19 |
return False, None, []
|
| 20 |
|
| 21 |
name = product.get('product_name')
|
|
|
|
| 5 |
url = f"https://india.openfoodfacts.org/api/v2/product/{barcode}.json"
|
| 6 |
response = requests.get(url)
|
| 7 |
if response.status_code != 200:
|
| 8 |
+
return {"error": f"Failed to fetch data for barcode {barcode}",'status':0}
|
| 9 |
return response.json()
|
| 10 |
|
| 11 |
def extract_product_info(product_data: dict):
|
|
|
|
| 13 |
Extracts product information (found status, name, ingredients) from OpenFoodFacts API response.
|
| 14 |
"""
|
| 15 |
found = product_data.get('status') == 1
|
| 16 |
+
if not found:
|
| 17 |
+
return False, None, []
|
| 18 |
+
|
| 19 |
product = product_data.get('product')
|
| 20 |
|
| 21 |
+
if not product:
|
| 22 |
return False, None, []
|
| 23 |
|
| 24 |
name = product.get('product_name')
|