Skip to content

Commit

Permalink
Introduce RemoteFitbitRepository.
Browse files Browse the repository at this point in the history
Use this repository interface from use cases, instead of calling the services directly.
  • Loading branch information
caarmen committed Mar 16, 2024
1 parent 14c4f67 commit 604b6d4
Show file tree
Hide file tree
Showing 20 changed files with 287 additions and 156 deletions.

This file was deleted.

38 changes: 0 additions & 38 deletions slackhealthbot/domain/modelmappers/remoteservicetodomain/sleep.py

This file was deleted.

26 changes: 26 additions & 0 deletions slackhealthbot/domain/remoterepository/remotefitbitrepository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import datetime
from abc import ABC

from slackhealthbot.core.models import OAuthFields
from slackhealthbot.domain.models.activity import ActivityData
from slackhealthbot.domain.models.sleep import SleepData


class RemoteFitbitRepository(ABC):
async def subscribe(
self,
oauth_fields: OAuthFields,
):
pass

async def get_activity(
self, oauth_fields: OAuthFields, when: datetime.datetime
) -> tuple[str, ActivityData] | None:
pass

async def get_sleep(
self,
oauth_fields: OAuthFields,
when: datetime.date,
) -> SleepData | None:
pass
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
LocalFitbitRepository,
User,
)
from slackhealthbot.domain.modelmappers.remoteservicetodomain.activity import (
remote_service_activity_to_domain_activity,
)
from slackhealthbot.domain.models.activity import ActivityData
from slackhealthbot.remoteservices.api.fitbit import activityapi
from slackhealthbot.domain.remoterepository.remotefitbitrepository import (
RemoteFitbitRepository,
)


async def do(
repo: LocalFitbitRepository,
local_repo: LocalFitbitRepository,
remote_repo: RemoteFitbitRepository,
fitbit_userid: str,
when: datetime.datetime,
) -> tuple[str, ActivityData] | None:
user: User = await repo.get_user_by_fitbit_userid(
user: User = await local_repo.get_user_by_fitbit_userid(
fitbit_userid=fitbit_userid,
)
last_activities: activityapi.FitbitActivities = await activityapi.get_activity(
oauth_token=user.oauth_data,
return await remote_repo.get_activity(
oauth_fields=user.oauth_data,
when=when,
)
return remote_service_activity_to_domain_activity(last_activities)
17 changes: 8 additions & 9 deletions slackhealthbot/domain/usecases/fitbit/usecase_get_last_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@
from slackhealthbot.domain.localrepository.localfitbitrepository import (
LocalFitbitRepository,
)
from slackhealthbot.domain.modelmappers.remoteservicetodomain.sleep import (
remote_service_sleep_to_domain_sleep,
)
from slackhealthbot.domain.models.sleep import SleepData
from slackhealthbot.remoteservices.api.fitbit import sleepapi
from slackhealthbot.domain.remoterepository.remotefitbitrepository import (
RemoteFitbitRepository,
)


async def do(
repo: LocalFitbitRepository,
local_repo: LocalFitbitRepository,
remote_repo: RemoteFitbitRepository,
fitbit_userid: str,
when: datetime.date,
) -> SleepData | None:
oauth_data: OAuthFields = await repo.get_oauth_data_by_fitbit_userid(
oauth_data: OAuthFields = await local_repo.get_oauth_data_by_fitbit_userid(
fitbit_userid=fitbit_userid,
)
last_sleep: sleepapi.FitbitSleep = await sleepapi.get_sleep(
oauth_token=oauth_data,
return await remote_repo.get_sleep(
oauth_fields=oauth_data,
when=when,
)
return remote_service_sleep_to_domain_sleep(last_sleep)
13 changes: 7 additions & 6 deletions slackhealthbot/domain/usecases/fitbit/usecase_login_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
UserIdentity,
)
from slackhealthbot.domain.modelmappers.remoteservicetocore import oauth
from slackhealthbot.remoteservices.api.fitbit import subscribeapi
from slackhealthbot.domain.remoterepository.remotefitbitrepository import (
RemoteFitbitRepository,
)


async def do(
repo: LocalFitbitRepository,
local_repo: LocalFitbitRepository,
remote_repo: RemoteFitbitRepository,
slack_alias: str,
token: dict[str, Any],
):
user: User = await _upsert_user(repo, slack_alias, token)
await subscribeapi.subscribe(
fitbit_userid=user.identity.fitbit_userid, oauth_token=user.oauth_data
)
user: User = await _upsert_user(local_repo, slack_alias, token)
await remote_repo.subscribe(oauth_fields=user.oauth_data)


async def _upsert_user(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
ActivityHistory,
TopActivityStats,
)
from slackhealthbot.domain.remoterepository.remotefitbitrepository import (
RemoteFitbitRepository,
)
from slackhealthbot.domain.remoterepository.remoteslackrepository import (
RemoteSlackRepository,
)
Expand All @@ -18,16 +21,20 @@


async def do(
fitbit_repo: LocalFitbitRepository,
local_fitbit_repo: LocalFitbitRepository,
remote_fitbit_repo: RemoteFitbitRepository,
slack_repo: RemoteSlackRepository,
fitbit_userid: str,
when: datetime.datetime,
) -> ActivityData | None:
user_identity: UserIdentity = await fitbit_repo.get_user_identity_by_fitbit_userid(
fitbit_userid=fitbit_userid,
user_identity: UserIdentity = (
await local_fitbit_repo.get_user_identity_by_fitbit_userid(
fitbit_userid=fitbit_userid,
)
)
new_activity = await usecase_get_last_activity.do(
repo=fitbit_repo,
local_repo=local_fitbit_repo,
remote_repo=remote_fitbit_repo,
fitbit_userid=fitbit_userid,
when=when,
)
Expand All @@ -37,32 +44,32 @@ async def do(
activity_name, new_activity_data = new_activity

if not await _is_new_valid_activity(
fitbit_repo,
local_fitbit_repo,
fitbit_userid=fitbit_userid,
type_id=new_activity_data.type_id,
log_id=new_activity_data.log_id,
):
return None

last_activity_data: ActivityData = (
await fitbit_repo.get_latest_activity_by_user_and_type(
await local_fitbit_repo.get_latest_activity_by_user_and_type(
fitbit_userid=fitbit_userid,
type_id=new_activity_data.type_id,
)
)

await fitbit_repo.create_activity_for_user(
await local_fitbit_repo.create_activity_for_user(
fitbit_userid=fitbit_userid,
activity=new_activity_data,
)
all_time_top_activity_stats: TopActivityStats = (
await fitbit_repo.get_top_activity_stats_by_user_and_activity_type(
await local_fitbit_repo.get_top_activity_stats_by_user_and_activity_type(
fitbit_userid=fitbit_userid,
type_id=new_activity_data.type_id,
)
)
recent_top_activity_stats: TopActivityStats = (
await fitbit_repo.get_top_activity_stats_by_user_and_activity_type(
await local_fitbit_repo.get_top_activity_stats_by_user_and_activity_type(
fitbit_userid=fitbit_userid,
type_id=new_activity_data.type_id,
since=datetime.datetime.now(datetime.timezone.utc)
Expand Down
19 changes: 13 additions & 6 deletions slackhealthbot/domain/usecases/fitbit/usecase_process_new_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
UserIdentity,
)
from slackhealthbot.domain.models.sleep import SleepData
from slackhealthbot.domain.remoterepository.remotefitbitrepository import (
RemoteFitbitRepository,
)
from slackhealthbot.domain.remoterepository.remoteslackrepository import (
RemoteSlackRepository,
)
Expand All @@ -13,25 +16,29 @@


async def do(
fitbit_repo: LocalFitbitRepository,
local_fitbit_repo: LocalFitbitRepository,
remote_fitbit_repo: RemoteFitbitRepository,
slack_repo: RemoteSlackRepository,
fitbit_userid: str,
when: datetime.date,
) -> SleepData | None:
user_identity: UserIdentity = await fitbit_repo.get_user_identity_by_fitbit_userid(
fitbit_userid=fitbit_userid,
user_identity: UserIdentity = (
await local_fitbit_repo.get_user_identity_by_fitbit_userid(
fitbit_userid=fitbit_userid,
)
)
last_sleep_data: SleepData = await fitbit_repo.get_sleep_by_fitbit_userid(
last_sleep_data: SleepData = await local_fitbit_repo.get_sleep_by_fitbit_userid(
fitbit_userid=fitbit_userid,
)
new_sleep_data: SleepData = await usecase_get_last_sleep.do(
repo=fitbit_repo,
local_repo=local_fitbit_repo,
remote_repo=remote_fitbit_repo,
fitbit_userid=fitbit_userid,
when=when,
)
if not new_sleep_data:
return None
await fitbit_repo.update_sleep_for_user(
await local_fitbit_repo.update_sleep_for_user(
fitbit_userid=fitbit_userid,
sleep=new_sleep_data,
)
Expand Down
4 changes: 3 additions & 1 deletion slackhealthbot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from slackhealthbot.oauth import withingsconfig as oauth_withings
from slackhealthbot.routers.dependencies import (
fitbit_repository_factory,
get_remote_fitbit_repository,
get_slack_repository,
request_context_fitbit_repository,
request_context_withings_repository,
Expand All @@ -39,7 +40,8 @@ async def lifespan(_app: FastAPI):
schedule_task = None
if settings.fitbit_poll_enabled:
schedule_task = await fitbitpoll.schedule_fitbit_poll(
fitbit_repo_factory=fitbit_repository_factory(),
local_fitbit_repo_factory=fitbit_repository_factory(),
remote_fitbit_repo=get_remote_fitbit_repository(),
slack_repo=get_slack_repository(),
initial_delay_s=10,
)
Expand Down
3 changes: 1 addition & 2 deletions slackhealthbot/remoteservices/api/fitbit/subscribeapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@


async def subscribe(
fitbit_userid: str,
oauth_token: OAuthFields,
):
for collectionPath in ["sleep", "activities"]:
response = await requests.post(
provider=settings.name,
token=oauth_token,
url=f"{settings.base_url}1/user/-/{collectionPath}/apiSubscriptions/{fitbit_userid}-{collectionPath}.json",
url=f"{settings.base_url}1/user/-/{collectionPath}/apiSubscriptions/{oauth_token.oauth_userid}-{collectionPath}.json",
)
logging.info(
f"Fitbit {collectionPath} subscription response: {response.json()}"
Expand Down
Loading

0 comments on commit 604b6d4

Please sign in to comment.