Skip to content

Commit

Permalink
get history data
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinebou12 authored Mar 6, 2024
1 parent 84d2984 commit 2390125
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,41 @@ async def get_measurements(self):
_LOGGER.error(f"Failed to fetch weight measurements: {e}")
return None


async def get_measurements_history(self):
"""
Fetch the most recent weight measurements_history for the user.
"""
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)

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 @@ -1016,6 +1051,17 @@ async def get_measurements(request: Request, renpho: RenphoWeight = Depends(get_
_LOGGER.error(f"Error fetching measurements: {e}")
return APIResponse(status="error", message=str(e))

@app.get("/measurements_history", response_model=APIResponse)
async def get_measurements_history(request: Request, renpho: RenphoWeight = Depends(get_current_user)):
try:
measurements_history = await renpho.get_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 2390125

Please sign in to comment.