Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ pythonpath = [
asyncio_default_fixture_loop_scope = "function"

[tool.ruff.lint]
extend-select = ["I", "TRY", "UP", "D", "W"]
extend-ignore = ["D213", "D202", "D203", "D213", "UP038", "TRY003"]
extend-select = ["ALL"]
extend-ignore = ["D213", "D202", "D203", "D213", "TRY003", "E501", "ANN401", "A005", "N818", "N812"]

[tool.ruff.lint.per-file-ignores]
"cli.py" = ["FBT001"]
"tests/*" = ["SLF001", "S101", "S105", "ARG001", "FBT001"]

[tool.ruff.lint.pydocstyle]
convention = "numpy"
Expand Down
88 changes: 62 additions & 26 deletions src/pyloadapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@
import aiohttp
from yarl import URL

from .exceptions import CannotConnect, InvalidAuth, ParserError
from .types import Destination, LoginResponse, PyLoadCommand, StatusServerResponse
from pyloadapi.exceptions import CannotConnect, InvalidAuth, ParserError
from pyloadapi.types import (
Destination,
LoginResponse,
PyLoadCommand,
StatusServerResponse,
)

_LOGGER = logging.getLogger(__package__)

Expand Down Expand Up @@ -74,18 +79,23 @@ async def login(self) -> LoginResponse:
try:
async with self._session.post(url, data=user_data) as r:
_LOGGER.debug(
"Response from %s [%s]: %s", url, r.status, await r.text()
"Response from %s [%s]: %s",
url,
r.status,
await r.text(),
)

r.raise_for_status()
try:
data: LoginResponse = await r.json()
except (JSONDecodeError, TypeError, aiohttp.ContentTypeError) as e:
_LOGGER.debug(
"Exception: Cannot parse login response:\n %s", exc_info=True
"Exception: Cannot parse login response:\n %s",
exc_info=True,
)
msg = "Login failed during parsing of request response."
raise ParserError(
"Login failed during parsing of request response."
msg,
) from e
else:
if not data:
Expand All @@ -96,7 +106,9 @@ async def login(self) -> LoginResponse:
raise CannotConnect from e

async def get(
self, command: PyLoadCommand, params: dict[str, Any] | None = None
self,
command: PyLoadCommand,
params: dict[str, Any] | None = None,
) -> Any:
"""Execute a pyLoad API command.

Expand Down Expand Up @@ -139,12 +151,16 @@ async def get(
try:
async with self._session.get(url, params=params) as r:
_LOGGER.debug(
"Response from %s [%s]: %s", r.url, r.status, await r.text()
"Response from %s [%s]: %s",
r.url,
r.status,
await r.text(),
)

if r.status == HTTPStatus.UNAUTHORIZED:
msg = "Request failed due invalid or expired authentication cookie."
raise InvalidAuth(
"Request failed due invalid or expired authentication cookie."
msg,
)
r.raise_for_status()
try:
Expand All @@ -155,8 +171,9 @@ async def get(
command,
exc_info=True,
)
msg = "Get {command} failed during parsing of request response."
raise ParserError(
"Get {command} failed during parsing of request response."
msg,
) from e

return data
Expand All @@ -167,8 +184,9 @@ async def get(
command,
exc_info=True,
)
msg = "Executing command {command} failed due to request exception"
raise CannotConnect(
"Executing command {command} failed due to request exception"
msg,
) from e

async def post(
Expand Down Expand Up @@ -224,12 +242,16 @@ async def post(
try:
async with self._session.post(url, data=data) as r:
_LOGGER.debug(
"Response from %s [%s]: %s", r.url, r.status, await r.text()
"Response from %s [%s]: %s",
r.url,
r.status,
await r.text(),
)

if r.status == HTTPStatus.UNAUTHORIZED:
msg = "Request failed due invalid or expired authentication cookie."
raise InvalidAuth(
"Request failed due invalid or expired authentication cookie."
msg,
)
r.raise_for_status()
try:
Expand All @@ -240,8 +262,9 @@ async def post(
command,
exc_info=True,
)
msg = f"Get {command} failed during parsing of request response."
raise ParserError(
f"Get {command} failed during parsing of request response."
msg,
) from e

return data
Expand All @@ -252,8 +275,9 @@ async def post(
command,
exc_info=True,
)
msg = f"Executing command {command} failed due to request exception"
raise CannotConnect(
f"Executing command {command} failed due to request exception"
msg,
) from e

async def get_status(self) -> StatusServerResponse:
Expand Down Expand Up @@ -291,7 +315,8 @@ async def get_status(self) -> StatusServerResponse:
try:
data: StatusServerResponse = await self.get(PyLoadCommand.STATUS)
except CannotConnect as e:
raise CannotConnect("Get status failed due to request exception") from e
msg = "Get status failed due to request exception"
raise CannotConnect(msg) from e
else:
return data

Expand Down Expand Up @@ -323,8 +348,9 @@ async def pause(self) -> None:
try:
await self.get(PyLoadCommand.PAUSE)
except CannotConnect as e:
msg = "Pausing download queue failed due to request exception"
raise CannotConnect(
"Pausing download queue failed due to request exception"
msg,
) from e

async def unpause(self) -> None:
Expand Down Expand Up @@ -355,8 +381,9 @@ async def unpause(self) -> None:
try:
await self.get(PyLoadCommand.UNPAUSE)
except CannotConnect as e:
msg = "Unpausing download queue failed due to request exception"
raise CannotConnect(
"Unpausing download queue failed due to request exception"
msg,
) from e

async def toggle_pause(self) -> None:
Expand Down Expand Up @@ -387,8 +414,9 @@ async def toggle_pause(self) -> None:
try:
await self.get(PyLoadCommand.TOGGLE_PAUSE)
except CannotConnect as e:
msg = "Toggling pause download queue failed due to request exception"
raise CannotConnect(
"Toggling pause download queue failed due to request exception"
msg,
) from e

async def stop_all_downloads(self) -> None:
Expand Down Expand Up @@ -419,8 +447,9 @@ async def stop_all_downloads(self) -> None:
try:
await self.get(PyLoadCommand.ABORT_ALL)
except CannotConnect as e:
msg = "Aborting all running downlods failed due to request exception"
raise CannotConnect(
"Aborting all running downlods failed due to request exception"
msg,
) from e

async def restart_failed(self) -> None:
Expand Down Expand Up @@ -451,8 +480,9 @@ async def restart_failed(self) -> None:
try:
await self.get(PyLoadCommand.RESTART_FAILED)
except CannotConnect as e:
msg = "Restarting all failed files failed due to request exception"
raise CannotConnect(
"Restarting all failed files failed due to request exception"
msg,
) from e

async def toggle_reconnect(self) -> None:
Expand Down Expand Up @@ -510,8 +540,9 @@ async def delete_finished(self) -> None:
try:
await self.get(PyLoadCommand.DELETE_FINISHED)
except CannotConnect as e:
msg = "Deleting all finished files failed due to request exception"
raise CannotConnect(
"Deleting all finished files failed due to request exception"
msg,
) from e

async def restart(self) -> None:
Expand Down Expand Up @@ -542,8 +573,9 @@ async def restart(self) -> None:
try:
await self.get(PyLoadCommand.RESTART)
except CannotConnect as e:
msg = "Restarting pyLoad core failed due to request exception"
raise CannotConnect(
"Restarting pyLoad core failed due to request exception"
msg,
) from e

async def version(self) -> str:
Expand Down Expand Up @@ -580,7 +612,8 @@ async def version(self) -> str:
r = await self.get(PyLoadCommand.VERSION)
return str(r)
except CannotConnect as e:
raise CannotConnect("Get version failed due to request exception") from e
msg = "Get version failed due to request exception"
raise CannotConnect(msg) from e

async def free_space(self) -> int:
"""Get the available free space at the download directory in bytes.
Expand Down Expand Up @@ -617,7 +650,8 @@ async def free_space(self) -> int:
r = await self.get(PyLoadCommand.FREESPACE)
return int(r)
except CannotConnect as e:
raise CannotConnect("Get free space failed due to request exception") from e
msg = "Get free space failed due to request exception"
raise CannotConnect(msg) from e

async def add_package(
self,
Expand Down Expand Up @@ -675,7 +709,8 @@ async def add_package(
)
return int(r)
except CannotConnect as e:
raise CannotConnect("Adding package failed due to request exception") from e
msg = "Adding package failed due to request exception"
raise CannotConnect(msg) from e

async def upload_container(
self,
Expand Down Expand Up @@ -719,6 +754,7 @@ async def upload_container(
)

except CannotConnect as e:
msg = "Uploading container to pyLoad failed due to request exception"
raise CannotConnect(
"Uploading container to pyLoad failed due to request exception"
msg,
) from e
Loading