1111load_dotenv ()
1212
1313POST_RECIPE_URL = os .getenv ("POST_RECIPE_URL" )
14+ BATCH_SIZE = 500
1415
1516
1617def add_recipes_from_file (file_path : str ) -> None :
@@ -22,14 +23,23 @@ def add_recipes_from_file(file_path: str) -> None:
2223 # Convert dict to list of recipes
2324 recipe_list = list (recipes .values ())
2425
25- # Send all recipes in a single batch request
26- resp = requests .post (POST_RECIPE_URL , json = recipe_list )
27- data = resp .json ()
28-
29- # Truncate the recipe IDs since there may be too many
30- if "recipe_ids" in data :
31- data ["recipe_ids" ] = data ["recipe_ids" ][:10 ]
32- print (json .dumps (data , indent = 4 ))
26+ # Send all recipes in batches
27+ print (
28+ f"Sending { len (recipe_list )} recipes to API using { len (recipe_list ) // BATCH_SIZE + 1 } batches"
29+ )
30+ for i in range (0 , len (recipe_list ), BATCH_SIZE ):
31+ current_batch = i // BATCH_SIZE + 1
32+ batch = recipe_list [i : i + BATCH_SIZE ]
33+ resp = requests .post (POST_RECIPE_URL , json = batch )
34+ data = resp .json ()
35+
36+ # Truncate response to first 5 recipe IDs
37+ if "recipe_ids" in data :
38+ data ["recipe_ids" ] = data ["recipe_ids" ][:5 ]
39+
40+ print (f"Response from batch { current_batch } :" )
41+ print (json .dumps (data , indent = 4 ))
42+ print ()
3343
3444
3545if __name__ == "__main__" :
0 commit comments