-
Notifications
You must be signed in to change notification settings - Fork 5
feat: enable fetching activity & logging diapers for babies #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| from python_snoo.containers import BabyData | ||
| from datetime import datetime | ||
|
|
||
| from python_snoo.containers import Activity, BabyData, BreastfeedingActivity, DiaperActivity, DiaperTypes | ||
| from python_snoo.exceptions import SnooBabyError | ||
| from python_snoo.snoo import Snoo | ||
|
|
||
|
|
@@ -8,6 +10,7 @@ def __init__(self, baby_id: str, snoo: Snoo): | |
| self.baby_id = baby_id | ||
| self.snoo = snoo | ||
| self.baby_url = f"https://api-us-east-1-prod.happiestbaby.com/us/me/v10/babies/{self.baby_id}" | ||
| self.activity_base_url = "https://api-us-east-1-prod.happiestbaby.com/cs/me/v11" | ||
|
|
||
| @property | ||
| def session(self): | ||
|
|
@@ -21,3 +24,93 @@ async def get_status(self) -> BabyData: | |
| except Exception as ex: | ||
| raise SnooBabyError from ex | ||
| return BabyData.from_dict(resp) | ||
|
|
||
| async def get_activity_data(self, from_date: datetime, to_date: datetime) -> list[Activity]: | ||
| """Get activity data for this baby including feeding and diaper changes | ||
|
|
||
| Args: | ||
| from_date: Start date for activity range | ||
| to_date: End date for activity range | ||
|
|
||
| Returns: | ||
| List of typed Activity objects (DiaperActivity or BreastfeedingActivity) | ||
| """ | ||
| hdrs = self.snoo.generate_snoo_auth_headers(self.snoo.tokens.aws_id) | ||
|
|
||
| url = f"{self.activity_base_url}/babies/{self.baby_id}/journals/grouped-tracking" | ||
|
|
||
| params = { | ||
| "group": "activity", | ||
| "fromDateTime": from_date.astimezone().isoformat(timespec="milliseconds"), | ||
| "toDateTime": to_date.astimezone().isoformat(timespec="milliseconds"), | ||
| } | ||
|
|
||
| try: | ||
| r = await self.session.get(url, headers=hdrs, params=params) | ||
| resp = await r.json() | ||
| if r.status < 200 or r.status >= 300: | ||
| raise SnooBabyError(f"Failed to get activity data: {r.status}: {resp}. Payload: {params}") | ||
|
|
||
| activities: list[Activity] = [] | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. other thing I tried out was an |
||
| if isinstance(resp, list): | ||
| for activity in resp: | ||
| activity_type = activity.get("type", "").lower() | ||
|
|
||
| if activity_type == "diaper": | ||
| activities.append(DiaperActivity.from_dict(activity)) | ||
| elif activity_type == "breastfeeding": | ||
| activities.append(BreastfeedingActivity.from_dict(activity)) | ||
| else: | ||
| # Other activity types exist but aren't supported yet | ||
| raise SnooBabyError(f"Unknown activity type: {activity_type}") | ||
| else: | ||
| raise SnooBabyError(f"Unexpected response format: {type(resp)}") | ||
|
|
||
| return activities | ||
|
|
||
| except Exception as ex: | ||
| raise SnooBabyError from ex | ||
|
|
||
| async def log_diaper_change( | ||
| self, | ||
| diaper_types: list[DiaperTypes], | ||
| note: str | None = None, | ||
| start_time: datetime | None = None, | ||
| ) -> DiaperActivity: | ||
| """Log a diaper change for this baby | ||
|
|
||
| Args: | ||
| diaper_types (list): List of diaper types. e.g. ['pee'], ['poo'], or ['pee', 'poo'] | ||
| note (str, optional): Optional note about the diaper change | ||
| start_time (datetime, optional): Diaper change timestamp, doesn't allow length. | ||
| Defaults to current local time if not provided. | ||
| """ | ||
|
|
||
| if not start_time: | ||
| start_time = datetime.now() | ||
|
|
||
| # Always include the timezone indicator in the ISO string - seems to be required by the API | ||
| if start_time.tzinfo is None: | ||
| start_time = start_time.astimezone() | ||
|
|
||
| hdrs = self.snoo.generate_snoo_auth_headers(self.snoo.tokens.aws_id) | ||
| url = f"{self.activity_base_url}/journals" | ||
|
|
||
| payload = { | ||
| "babyId": self.baby_id, | ||
| "data": {"types": [dt.value for dt in diaper_types]}, | ||
| "type": "diaper", | ||
| "startTime": start_time.isoformat(timespec="milliseconds"), | ||
| } | ||
|
|
||
| if note: | ||
| payload["note"] = note | ||
|
|
||
| try: | ||
| r = await self.session.post(url, headers=hdrs, json=payload) | ||
| resp = await r.json() | ||
| if r.status < 200 or r.status >= 300: | ||
| raise SnooBabyError(f"Failed to log diaper change: {r.status}: {resp}. Payload: {payload}") | ||
| return DiaperActivity.from_dict(resp) | ||
| except Exception as ex: | ||
| raise SnooBabyError from ex | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.