Skip to content

Commit

Permalink
Added Device get_question_by_id, and always fetch template before dat…
Browse files Browse the repository at this point in the history
…a to ensure we do not get None
  • Loading branch information
mikelgg93 committed Jul 8, 2024
1 parent df6aafa commit 2e27d2d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/pupil_labs/realtime_api/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import logging
import types
import typing as T
from uuid import UUID

import aiohttp
import numpy as np
import websockets

import pupil_labs # noqa: F401
import websockets

from .base import DeviceBase
from .models import (
Expand Down Expand Up @@ -182,6 +182,8 @@ async def get_template_data(self, format: TemplateDataFormat = "simple"):
format in TemplateDataFormat.__args__
), f"format should be one of {TemplateDataFormat}"

self.template_definition = await self.get_template()

async with self.session.get(self.api_url(APIPath.TEMPLATE_DATA)) as response:
confirmation = await response.json()
if response.status != 200:
Expand All @@ -193,8 +195,9 @@ async def get_template_data(self, format: TemplateDataFormat = "simple"):
if format == "api":
return result
elif format == "simple":
template = await self.get_template()
return template.convert_from_api_to_simple_format(result)
return self.template_definition.convert_from_api_to_simple_format(
result
)

async def post_template_data(
self,
Expand Down Expand Up @@ -248,6 +251,14 @@ async def post_template_data(
logger.debug(f"[{self}.get_template_data] Send data's template: {result}")
return result

async def get_question_by_id(self, question_id: T.Union[str, UUID]):
self.template_definition = await self.get_template()

for item in self.template_definition.items:
if str(item.id) == str(question_id):
return item
return None

async def close(self):
await self.session.close()
self.session = None
Expand Down

0 comments on commit 2e27d2d

Please sign in to comment.