Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Device get_question_by_id, and always fetch template before data to ensure we do not get None #60

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/pupil_labs/realtime_api/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import types
import typing as T
from uuid import UUID

import aiohttp
import numpy as np
Expand Down Expand Up @@ -182,6 +183,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 +196,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 +252,28 @@ 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]):
item = (
self.template_definition.get_question_by_id(question_id)
if self.template_definition
else None
)

if item:
return item

old_template = self.template_definition if self.template_definition else None

await self.get_template()

if (
self.template_definition is not None
and self.template_definition is not old_template
):
return self.template_definition.get_question_by_id(question_id)

return None

async def close(self):
await self.session.close()
self.session = None
Expand Down
8 changes: 8 additions & 0 deletions src/pupil_labs/realtime_api/simple/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import threading
import typing as T
import weakref
from uuid import UUID

try:
from typing import Literal
Expand Down Expand Up @@ -243,6 +244,13 @@ async def _post_template_data():

return asyncio.run(_post_template_data())

def get_question_by_id(self, question_id: T.Union[str, UUID]):
async def _get_question_by_id():
async with _DeviceAsync.convert_from(self) as control:
return await control.get_question_by_id(question_id)

return asyncio.run(_get_question_by_id())

def receive_scene_video_frame(
self, timeout_seconds: T.Optional[float] = None
) -> T.Optional[SimpleVideoFrame]:
Expand Down
Loading