Update app.py
Browse files
app.py
CHANGED
|
@@ -239,7 +239,7 @@ class PesticideDataFetcher:
|
|
| 239 |
return f"Substance inconnue ({substance_id})"
|
| 240 |
|
| 241 |
def get_product_list(self) -> List[Dict[str, Any]]:
|
| 242 |
-
"""Récupère la liste de tous les produits"""
|
| 243 |
if self._product_cache:
|
| 244 |
return list(self._product_cache.values())
|
| 245 |
|
|
@@ -247,6 +247,7 @@ class PesticideDataFetcher:
|
|
| 247 |
url = f"{self.BASE_URL}/pesticide_residues_products" # Corrected endpoint
|
| 248 |
params = {"format": "json", "language": "FR", "api-version": "v1.0"} # Use params dict
|
| 249 |
products_loaded = 0
|
|
|
|
| 250 |
|
| 251 |
while url:
|
| 252 |
data = self.fetch_data(url, params=params)
|
|
@@ -255,13 +256,23 @@ class PesticideDataFetcher:
|
|
| 255 |
break
|
| 256 |
|
| 257 |
if data:
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
product_id = product.get("productId")
|
| 260 |
if product_id:
|
| 261 |
self._product_cache[product_id] = product
|
| 262 |
products_loaded += 1
|
|
|
|
| 263 |
|
| 264 |
-
url = data.get("nextLink")
|
| 265 |
logger.info(f"Produits récupérés jusqu'à présent: {products_loaded}")
|
| 266 |
if url :
|
| 267 |
# No need to pass params again for nextLink
|
|
@@ -271,7 +282,8 @@ class PesticideDataFetcher:
|
|
| 271 |
self._save_caches()
|
| 272 |
|
| 273 |
logger.info(f"Total des produits récupérés: {len(self._product_cache)}")
|
| 274 |
-
return
|
|
|
|
| 275 |
|
| 276 |
def get_mrls(self, product_id: int) -> List[Dict[str, Any]]:
|
| 277 |
"""Récupère les LMR pour un produit spécifique"""
|
|
|
|
| 239 |
return f"Substance inconnue ({substance_id})"
|
| 240 |
|
| 241 |
def get_product_list(self) -> List[Dict[str, Any]]:
|
| 242 |
+
"""Récupère la liste de tous les produits, gérant les cas où la réponse est une liste."""
|
| 243 |
if self._product_cache:
|
| 244 |
return list(self._product_cache.values())
|
| 245 |
|
|
|
|
| 247 |
url = f"{self.BASE_URL}/pesticide_residues_products" # Corrected endpoint
|
| 248 |
params = {"format": "json", "language": "FR", "api-version": "v1.0"} # Use params dict
|
| 249 |
products_loaded = 0
|
| 250 |
+
all_products = [] # Accumulate products here
|
| 251 |
|
| 252 |
while url:
|
| 253 |
data = self.fetch_data(url, params=params)
|
|
|
|
| 256 |
break
|
| 257 |
|
| 258 |
if data:
|
| 259 |
+
# Check if data is a list or a dict
|
| 260 |
+
if isinstance(data, list):
|
| 261 |
+
products = data
|
| 262 |
+
elif isinstance(data, dict) and "value" in data:
|
| 263 |
+
products = data["value"]
|
| 264 |
+
else:
|
| 265 |
+
logger.error(f"Unexpected API response format: {data}")
|
| 266 |
+
break # Stop if the format is unexpected
|
| 267 |
+
|
| 268 |
+
for product in products:
|
| 269 |
product_id = product.get("productId")
|
| 270 |
if product_id:
|
| 271 |
self._product_cache[product_id] = product
|
| 272 |
products_loaded += 1
|
| 273 |
+
all_products.append(product) #Add to the list
|
| 274 |
|
| 275 |
+
url = data.get("nextLink") if isinstance(data,dict) else None # Get nextLink if data is dict
|
| 276 |
logger.info(f"Produits récupérés jusqu'à présent: {products_loaded}")
|
| 277 |
if url :
|
| 278 |
# No need to pass params again for nextLink
|
|
|
|
| 282 |
self._save_caches()
|
| 283 |
|
| 284 |
logger.info(f"Total des produits récupérés: {len(self._product_cache)}")
|
| 285 |
+
return all_products
|
| 286 |
+
|
| 287 |
|
| 288 |
def get_mrls(self, product_id: int) -> List[Dict[str, Any]]:
|
| 289 |
"""Récupère les LMR pour un produit spécifique"""
|