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
Changes from 2 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
16 changes: 14 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,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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making a new http call every time someone calls this function, I'd first check if the question id is in the current self.template_definition and only update it if that's not the case.

Copy link
Member Author

@mikelgg93 mikelgg93 Jul 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Something like this? where we check first if the question_id is in the template already set, if it is not set or not found (assuming you changed it in between), we fetch it and then try again


for item in self.template_definition.items:
mikelgg93 marked this conversation as resolved.
Show resolved Hide resolved
if str(item.id) == str(question_id):
return item
return None

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