Skip to content

Commit

Permalink
Update app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Apr 26, 2024
1 parent 2fbd7ea commit 6383b51
Showing 1 changed file with 45 additions and 4 deletions.
49 changes: 45 additions & 4 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,6 @@ async def get_measurements(self):
"""
Fetch the most recent weight measurements for the user.
"""
_LOGGER.error(f"test1")
_LOGGER.error(f"{self.get_timestamp()}")
url = f"{API_MEASUREMENTS_URL}?user_id={self.user_id}&last_at={self.get_timestamp()}&locale=en&app_id=Renpho&terminal_user_session_key={self.token}"
try:
parsed = await self._request("GET", url, skip_auth=True)
Expand All @@ -651,8 +649,6 @@ async def get_measurements(self):
_LOGGER.error("No weight measurements found in the response.")
return
if measurements := parsed["last_ary"]:
_LOGGER.error(f"test2")
_LOGGER.error(f"{measurements}")
self.weight_history = [MeasurementDetail(**measurement) for measurement in measurements]
self.weight_info = self.weight_history[0] if self.weight_history else None
self.weight = self.weight_info.weight if self.weight_info else None
Expand Down Expand Up @@ -708,6 +704,40 @@ async def get_measurements_history(self):
_LOGGER.error(f"Failed to fetch weight measurements: {e}")
return None

async def get_all_users_measurements_history(self):
"""
Fetch the most recent weight measurements_history for the user.
"""
url = f"{API_MEASUREMENTS_URL}?last_at={self.get_timestamp()}&locale=en&app_id=Renpho&terminal_user_session_key={self.token}"
try:
parsed = await self._request("GET", url, skip_auth=True)

if not parsed:
_LOGGER.error("Failed to fetch weight measurements.")
return

if "status_code" in parsed and parsed["status_code"] == "20000":
if "last_ary" not in parsed:
_LOGGER.error("No weight measurements found in the response.")
return
if measurements := parsed["last_ary"]:
self.weight_history = [MeasurementDetail(**measurement) for measurement in measurements]
return self.weight_history
else:
_LOGGER.error("No weight measurements found in the response.")
return None
else:
# Handling different error scenarios
if "status_code" not in parsed:
_LOGGER.error("Invalid response format received from weight measurements endpoint.")
else:
_LOGGER.error(f"Error fetching weight measurements: Status Code {parsed.get('status_code')} - {parsed.get('status_message')}")
return None

except Exception as e:
_LOGGER.error(f"Failed to fetch weight measurements: {e}")
return None

async def get_weight(self):
if self.weight and self.weight_info:
return self.weight, self.weight_info
Expand Down Expand Up @@ -1212,6 +1242,17 @@ async def get_measurements_history(request: Request, renpho: RenphoWeight = Depe
_LOGGER.error(f"Error fetching measurements_history: {e}")
return APIResponse(status="error", message=str(e))

@app.get("/all_users_measurements_history", response_model=APIResponse)
async def get_measurements_history(request: Request, renpho: RenphoWeight = Depends(get_current_user)):
try:
measurements_history = await renpho.get_all_users_measurements_history()
if measurements_history:
return APIResponse(status="success", message="Fetched measurements_history.", data={"measurements_history": measurements_history})
raise HTTPException(status_code=404, detail="Measurements not found")
except Exception as e:
_LOGGER.error(f"Error fetching measurements_history: {e}")
return APIResponse(status="error", message=str(e))

@app.get("/weight", response_model=APIResponse)
async def get_weight(request: Request, renpho: RenphoWeight = Depends(get_current_user)):
try:
Expand Down

0 comments on commit 6383b51

Please sign in to comment.