-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
62 lines (47 loc) · 1.82 KB
/
example.py
File metadata and controls
62 lines (47 loc) · 1.82 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
#!/usr/bin/env python3
"""Example usage of the Oura API client."""
import os
from datetime import datetime, timedelta
import json
from oura_api_client.api.client import OuraClient
def main():
"""Run a demo of the Oura API client."""
# Get API token from environment variable
token = os.environ.get("OURA_API_TOKEN")
if not token:
print("Please set the OURA_API_TOKEN environment variable")
return
# Initialize the client
client = OuraClient(token)
# Get heart rate data for the last 7 days
end_date = datetime.now().strftime("%Y-%m-%d")
start_date = (datetime.now() - timedelta(days=7)).strftime("%Y-%m-%d")
try:
# Get heart rate data as a model
heart_rate = client.heartrate.get_heartrate(
start_date=start_date, end_date=end_date
)
print(f"Retrieved {len(heart_rate.data)} heart rate samples")
if heart_rate.data:
# Display the first 3 samples
for sample in heart_rate.data[:3]:
print(
f"Time: {sample.timestamp}, BPM: {sample.bpm}, Source: {sample.source}"
)
# Get heart rate data as raw JSON
heart_rate_raw = client.heartrate.get_heartrate(
start_date=start_date, end_date=end_date, return_model=False
)
# Save raw data to file
with open("heart_rate_data.json", "w") as f:
json.dump(heart_rate_raw, f, indent=2)
print("Saved raw heart rate data to heart_rate_data.json")
# Get personal info
personal_info = client.personal.get_personal_info()
print(f"\nUser ID: {personal_info.id}")
print(f"Email: {personal_info.email}")
print(f"Age: {personal_info.age}")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
main()