Skip to content

Commit

Permalink
Add types (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored Aug 28, 2022
1 parent 4acbcf2 commit c58eced
Showing 1 changed file with 49 additions and 7 deletions.
56 changes: 49 additions & 7 deletions pyprusalink/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Prusalink API."""
from __future__ import annotations

from collections.abc import AsyncGenerator
from contextlib import asynccontextmanager
from typing import TypedDict

from aiohttp import ClientResponse, ClientSession

Expand All @@ -13,6 +16,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.
Expand All @@ -26,37 +68,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()
Expand Down

0 comments on commit c58eced

Please sign in to comment.