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

Add NotFound error exception #72

Merged
merged 1 commit into from
Dec 20, 2023

Conversation

Skaronator
Copy link
Contributor

@Skaronator Skaronator commented Dec 16, 2023

The /v1/job API consistently indicates the presence of a thumbnail for the current print, even in cases where none actually exists.

The Idea is that we just add a NotFound exception that will be thrown on the get_file() function when the thumbnail file doesn't exist. We can then handle the error in home assistant core to not showing a thumbnail.

Ref: home-assistant/core#104887

Code can be tested when you slice a simple cube in Prusa Slicer, while disabling all thumbnail generation. Then start that print and run this python script:

#!/bin/env python3
import aiohttp
import asyncio
from pyprusalink import PrusaLink


async def main():
    async with aiohttp.ClientSession() as session:
        api = PrusaLink(session, "http://prusaxl.fritz.box", "maker", "xy")
        job = await api.get_job()
        await api.get_file(job['file']['refs']['thumbnail'])


if __name__ == "__main__":
    asyncio.run(main())

This should be the output:

$ ./main.py 
Traceback (most recent call last):
  File "/home/niklas/Workspace/pyprusalink/./main.py", line 15, in <module>
    asyncio.run(main())
  File "/usr/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/niklas/Workspace/pyprusalink/./main.py", line 11, in main
    await api.get_file(job['file']['refs']['thumbnail'])
  File "/home/niklas/Workspace/pyprusalink/pyprusalink/__init__.py", line 71, in get_file
    async with self.client.request("GET", path) as response:
  File "/usr/lib/python3.11/contextlib.py", line 204, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/home/niklas/Workspace/pyprusalink/pyprusalink/client.py", line 86, in request
    raise NotFound()
pyprusalink.types.NotFound

Copy link
Contributor

@agners agners left a comment

Choose a reason for hiding this comment

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

Make sense, thanks 👍

@agners agners merged commit 6bd9bf5 into home-assistant-libs:main Dec 20, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants