-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
86 lines (64 loc) · 3.16 KB
/
example.py
File metadata and controls
86 lines (64 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Example usage of the Nutrition Tracker API Python SDK.
Before running:
1. Get your free API key from: https://rapidapi.com/anonymous617461746174/api/nutrition-tracker-api
2. Replace YOUR_RAPIDAPI_KEY below with your actual key
"""
from nutrition_api import NutritionAPI, NutritionAPIError
# Replace with your RapidAPI key
API_KEY = "YOUR_RAPIDAPI_KEY"
def main():
# Initialize the client
api = NutritionAPI(api_key=API_KEY)
print("=" * 60)
print("Nutrition Tracker API - Python SDK Example")
print("=" * 60)
# Example 1: Single food item
print("\n📍 Example 1: Single Food Item")
print("-" * 40)
try:
result = api.calculate("100g grilled chicken breast")
print(f"Query: 100g grilled chicken breast\n")
print("Key Nutrients:")
print(f" • Energy: {result.get('Energy', {}).get('value', 'N/A')} {result.get('Energy', {}).get('unit', '')}")
print(f" • Protein: {result.get('Protein', {}).get('value', 'N/A')} {result.get('Protein', {}).get('unit', '')}")
print(f" • Fat: {result.get('Fat', {}).get('value', 'N/A')} {result.get('Fat', {}).get('unit', '')}")
# Show fat breakdown if available
fat_data = result.get('Fat', {})
if 'breakdown' in fat_data:
print("\n Fat Breakdown:")
breakdown = fat_data['breakdown']
for fat_type, data in breakdown.items():
print(f" - {fat_type}: {data.get('value', 'N/A')} {data.get('unit', '')}")
except NutritionAPIError as e:
print(f"Error: {e.message}")
# Example 2: Multi-item meal
print("\n\n📍 Example 2: Multi-Item Meal")
print("-" * 40)
try:
result = api.calculate("2 eggs, 100g oatmeal, and 1 banana")
print(f"Query: 2 eggs, 100g oatmeal, and 1 banana\n")
print("Combined Nutrients:")
print(f" • Energy: {result.get('Energy', {}).get('value', 'N/A')} {result.get('Energy', {}).get('unit', '')}")
print(f" • Protein: {result.get('Protein', {}).get('value', 'N/A')} {result.get('Protein', {}).get('unit', '')}")
print(f" • Carbohydrates:{result.get('Carbohydrates', {}).get('value', 'N/A')} {result.get('Carbohydrates', {}).get('unit', '')}")
print(f" • Fiber: {result.get('Fiber', {}).get('value', 'N/A')} {result.get('Fiber', {}).get('unit', '')}")
except NutritionAPIError as e:
print(f"Error: {e.message}")
# Example 3: Full response with all nutrients
print("\n\n📍 Example 3: All 25+ Nutrients")
print("-" * 40)
try:
result = api.calculate("1 apple")
print(f"Query: 1 apple\n")
print("All Nutrients:")
for nutrient, data in result.items():
if isinstance(data, dict) and 'value' in data:
print(f" • {nutrient}: {data['value']} {data.get('unit', '')}")
except NutritionAPIError as e:
print(f"Error: {e.message}")
print("\n" + "=" * 60)
print("Get your API key: https://rapidapi.com/anonymous617461746174/api/nutrition-tracker-api")
print("=" * 60)
if __name__ == "__main__":
main()