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 types #4

Merged
merged 2 commits into from
Aug 28, 2022
Merged
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
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