From c51579fa79028c274aef518e766300bf24ada4d8 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 28 Aug 2022 16:34:35 -0400 Subject: [PATCH 1/2] Add types --- pyprusalink/__init__.py | 55 +++++++++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/pyprusalink/__init__.py b/pyprusalink/__init__.py index 73ed3f6..9a9794a 100644 --- a/pyprusalink/__init__.py +++ b/pyprusalink/__init__.py @@ -1,6 +1,8 @@ """Prusalink API.""" +from __future__ import annotations from collections.abc import AsyncGenerator from contextlib import asynccontextmanager +from typing import TypedDict from aiohttp import ClientResponse, ClientSession @@ -13,6 +15,45 @@ class InvalidAuth(PrusaLinkError): """Error to indicate there is invalid auth.""" +class VersionInfo(TypedDict): + """Version data.""" + + api: str + server: str + text: str + hostname: str + + +class PrinterInfo(TypedDict): + """Printer data.""" + + telemetry: dict + temperature: dict + state: dict + + +class JobInfo(TypedDict): + """Job data.""" + + state: str + job: dict | None + + +class FileInfo(TypedDict): + """File data.""" + + name: str + origin: str + size: int + refs: dict + + +class FilesInfo(TypedDict): + """Files data.""" + + files: list[FileInfo] + + class PrusaLink: """Wrapper for the Prusalink API. @@ -26,37 +67,37 @@ def __init__(self, session: ClientSession, host: str, api_key: str) -> None: self.host = host self._api_key = api_key - async def get_version(self) -> dict: + async def get_version(self) -> VersionInfo: """Get the version.""" async with self.request("GET", "api/version") as response: return await response.json() - async def get_printer(self) -> dict: + async def get_printer(self) -> PrinterInfo: """Get the printer.""" async with self.request("GET", "api/printer") as response: return await response.json() - async def get_job(self) -> dict: + async def get_job(self) -> JobInfo: """Get current job.""" async with self.request("GET", "api/job") as response: return await response.json() - async def get_file(self, path) -> dict: + async def get_file(self, path: str) -> FileInfo: """Get specific file info.""" async with self.request("GET", f"api/files{path}") as response: return await response.json() - async def get_files(self) -> dict: + async def get_files(self) -> FilesInfo: """Get all files.""" async with self.request("GET", "api/files?recursive=true") as response: return await response.json() - async def get_small_thumbnail(self, path): + async def get_small_thumbnail(self, path: str) -> bytes: """Get a small thumbnail.""" async with self.request("GET", f"thumb/s{path}") as response: return await response.read() - async def get_large_thumbnail(self, path): + async def get_large_thumbnail(self, path: str) -> bytes: """Get a large thumbnail.""" async with self.request("GET", f"thumb/l{path}") as response: return await response.read() From dbcef2b54889c02845b4577aa55524615aa6c8ef Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 28 Aug 2022 16:36:39 -0400 Subject: [PATCH 2/2] Isort --- pyprusalink/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pyprusalink/__init__.py b/pyprusalink/__init__.py index 9a9794a..ec84560 100644 --- a/pyprusalink/__init__.py +++ b/pyprusalink/__init__.py @@ -1,5 +1,6 @@ """Prusalink API.""" from __future__ import annotations + from collections.abc import AsyncGenerator from contextlib import asynccontextmanager from typing import TypedDict