Skip to content

Commit

Permalink
improve access to member_id/uuid and correct names
Browse files Browse the repository at this point in the history
  • Loading branch information
NodeJSmith committed Jan 3, 2025
1 parent 0abd89e commit 642eef4
Showing 1 changed file with 27 additions and 28 deletions.
55 changes: 27 additions & 28 deletions src/otf_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@


class Otf:
member: models.MemberDetail
home_studio_uuid: str
user: OtfUser
_session: httpx.Client

Expand Down Expand Up @@ -56,9 +58,6 @@ def __init__(
user (OtfUser, optional): A user object. Default is None.
"""

self.member: models.MemberDetail
self.home_studio_uuid: str

if user:
self.user = user
elif (username and password) or (access_token and id_token):
Expand All @@ -73,15 +72,15 @@ def __init__(
else:
raise ValueError("No valid authentication method provided")

# simplify access to member_id and member_uuid
self._member_id = self.user.member_id
self._member_uuid = self.user.member_uuid
self.member = self.get_member_detail()
self.home_studio_uuid = self.member.home_studio.studio_uuid

self.member_uuid = self.member.member_uuid
self._cognito_id = self.member.cognito_id
self._perf_api_headers = {
"koji-member-id": self._member_id,
"koji-member-id": self.member_uuid,
"koji-member-email": self.user.id_claims_data.email,
}
self.member = self.get_member_detail()
self.home_studio_uuid = self.member.home_studio.studio_uuid

@property
def headers(self) -> dict[str, str]:
Expand Down Expand Up @@ -263,7 +262,7 @@ def get_booking(self, booking_uuid: str) -> models.Booking:
if not booking_uuid:
raise ValueError("booking_uuid is required")

data = self._default_request("GET", f"/member/members/{self._member_id}/bookings/{booking_uuid}")
data = self._default_request("GET", f"/member/members/{self.member_uuid}/bookings/{booking_uuid}")
return models.Booking(**data["data"])

def get_booking_from_class(self, class_: str | models.OtfClass) -> models.Booking:
Expand Down Expand Up @@ -321,7 +320,7 @@ def book_class(self, class_: str | models.OtfClass) -> models.Booking:

body = {"classUUId": class_uuid, "confirmed": False, "waitlist": False}

resp = self._default_request("PUT", f"/member/members/{self._member_id}/bookings", json=body)
resp = self._default_request("PUT", f"/member/members/{self.member_uuid}/bookings", json=body)

if resp["code"] == "ERROR":
if resp["data"]["errorCode"] == "603":
Expand Down Expand Up @@ -363,7 +362,7 @@ def cancel_booking(self, booking: str | models.Booking):

params = {"confirmed": "true"}
resp = self._default_request(
"DELETE", f"/member/members/{self._member_id}/bookings/{booking_uuid}", params=params
"DELETE", f"/member/members/{self.member_uuid}/bookings/{booking_uuid}", params=params
)
if resp["code"] == "NOT_AUTHORIZED" and resp["message"].startswith("This class booking has"):
raise BookingAlreadyCancelledError(
Expand Down Expand Up @@ -433,7 +432,7 @@ def get_bookings(

params = {"startDate": start_date, "endDate": end_date, "statuses": status_value}

res = self._default_request("GET", f"/member/members/{self._member_id}/bookings", params=params)
res = self._default_request("GET", f"/member/members/{self.member_uuid}/bookings", params=params)

bookings = res["data"][:limit] if limit else res["data"]

Expand Down Expand Up @@ -500,7 +499,7 @@ def _get_bookings_old(self, status: models.BookingStatus | None = None) -> model
status_value = status.value if status else None

res = self._default_request(
"GET", f"/member/members/{self._member_id}/bookings", params={"status": status_value}
"GET", f"/member/members/{self.member_uuid}/bookings", params={"status": status_value}
)

return models.BookingList(bookings=res["data"])
Expand Down Expand Up @@ -542,7 +541,7 @@ def get_member_detail(

params = {"include": ",".join(include)} if include else None

data = self._default_request("GET", f"/member/members/{self._member_id}", params=params)
data = self._default_request("GET", f"/member/members/{self.user.member_id}", params=params)
return models.MemberDetail(**data["data"])

def get_member_membership(self) -> models.MemberMembership:
Expand All @@ -552,7 +551,7 @@ def get_member_membership(self) -> models.MemberMembership:
MemberMembership: The member's membership details.
"""

data = self._default_request("GET", f"/member/members/{self._member_id}/memberships")
data = self._default_request("GET", f"/member/members/{self.member_uuid}/memberships")
return models.MemberMembership(**data["data"])

def get_member_purchases(self) -> models.MemberPurchaseList:
Expand All @@ -561,7 +560,7 @@ def get_member_purchases(self) -> models.MemberPurchaseList:
Returns:
MemberPurchaseList: The member's purchases.
"""
data = self._default_request("GET", f"/member/members/{self._member_id}/purchases")
data = self._default_request("GET", f"/member/members/{self.member_uuid}/purchases")
return models.MemberPurchaseList(data=data["data"])

def get_member_lifetime_stats(
Expand All @@ -581,7 +580,7 @@ def get_member_lifetime_stats(
Any: The member's lifetime stats.
"""

data = self._default_request("GET", f"/performance/v2/{self._member_id}/over-time/{select_time.value}")
data = self._default_request("GET", f"/performance/v2/{self.member_uuid}/over-time/{select_time.value}")

stats = models.StatsResponse(**data["data"])
return stats
Expand All @@ -606,7 +605,7 @@ def get_out_of_studio_workout_history(self) -> models.OutOfStudioWorkoutHistoryL
Returns:
OutOfStudioWorkoutHistoryList: The member's out of studio workout history.
"""
data = self._default_request("GET", f"/member/members/{self._member_id}/out-of-studio-workout")
data = self._default_request("GET", f"/member/members/{self.member_uuid}/out-of-studio-workout")

return models.OutOfStudioWorkoutHistoryList(workouts=data["data"])

Expand All @@ -616,7 +615,7 @@ def get_favorite_studios(self) -> models.FavoriteStudioList:
Returns:
FavoriteStudioList: The member's favorite studios.
"""
data = self._default_request("GET", f"/member/members/{self._member_id}/favorite-studios")
data = self._default_request("GET", f"/member/members/{self.member_uuid}/favorite-studios")

return models.FavoriteStudioList(studios=data["data"])

Expand Down Expand Up @@ -778,7 +777,7 @@ def get_body_composition_list(self) -> models.BodyCompositionList:
Returns:
Any: The member's body composition list.
"""
data = self._default_request("GET", f"/member/members/{self._member_uuid}/body-composition")
data = self._default_request("GET", f"/member/members/{self._cognito_id}/body-composition")

return models.BodyCompositionList(data=data["data"])

Expand All @@ -788,7 +787,7 @@ def get_challenge_tracker_content(self) -> models.ChallengeTrackerContent:
Returns:
ChallengeTrackerContent: The member's challenge tracker content.
"""
data = self._default_request("GET", f"/challenges/v3.1/member/{self._member_id}")
data = self._default_request("GET", f"/challenges/v3.1/member/{self.member_uuid}")
return models.ChallengeTrackerContent(**data["Dto"])

def get_challenge_tracker_detail(
Expand Down Expand Up @@ -818,7 +817,7 @@ def get_challenge_tracker_detail(
"challengeSubTypeId": challenge_sub_type_id,
}

data = self._default_request("GET", f"/challenges/v3/member/{self._member_id}/benchmarks", params=params)
data = self._default_request("GET", f"/challenges/v3/member/{self.member_uuid}/benchmarks", params=params)

return models.ChallengeTrackerDetailList(details=data["Dto"])

Expand All @@ -840,7 +839,7 @@ def get_challenge_tracker_participation(self, challenge_type_id: models.Challeng

data = self._default_request(
"GET",
f"/challenges/v1/member/{self._member_id}/participation",
f"/challenges/v1/member/{self.member_uuid}/participation",
params={"challengeTypeId": challenge_type_id.value},
)
return data
Expand Down Expand Up @@ -894,7 +893,7 @@ def get_hr_history(self) -> models.TelemetryHrHistory:
"""
path = "/v1/physVars/maxHr/history"

params = {"memberUuid": self._member_id}
params = {"memberUuid": self.member_uuid}
res = self._telemetry_request("GET", path, params=params)
return models.TelemetryHrHistory(**res)

Expand All @@ -908,7 +907,7 @@ def get_max_hr(self) -> models.TelemetryMaxHr:
"""
path = "/v1/physVars/maxHr"

params = {"memberUuid": self._member_id}
params = {"memberUuid": self.member_uuid}

res = self._telemetry_request("GET", path, params=params)
return models.TelemetryMaxHr(**res)
Expand Down Expand Up @@ -946,7 +945,7 @@ def _get_member_services(self, active_only: bool = True) -> Any:
."""
active_only_str = "true" if active_only else "false"
data = self._default_request(
"GET", f"/member/members/{self._member_id}/services", params={"activeOnly": active_only_str}
"GET", f"/member/members/{self.member_uuid}/services", params={"activeOnly": active_only_str}
)
return data

Expand All @@ -964,5 +963,5 @@ def _get_aspire_data(self, datetime: str | None = None, unit: str | None = None)
"""
params = {"datetime": datetime, "unit": unit}

data = self._default_request("GET", f"/member/wearables/{self._member_id}/wearable-daily", params=params)
data = self._default_request("GET", f"/member/wearables/{self.member_uuid}/wearable-daily", params=params)
return data

0 comments on commit 642eef4

Please sign in to comment.