Skip to content

Commit

Permalink
Merge pull request #148 from marksie1988/146-multiple-test-failures-s…
Browse files Browse the repository at this point in the history
…ince-adding-500-exception

fix: multiple test failures since adding 500 exception
  • Loading branch information
marksie1988 authored Apr 18, 2023
2 parents bfa1f36 + bc699b4 commit 2796541
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 116 deletions.
17 changes: 16 additions & 1 deletion pyarr/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,14 @@ def get_history(
Returns:
JsonObject: Dictionary with items
"""
params: dict[str, Union[int, PyarrHistorySortKey, PyarrSortDirection]] = {}
params: dict[
str,
Union[
int,
PyarrHistorySortKey,
PyarrSortDirection,
],
] = {}

if page:
params["page"] = page
Expand Down Expand Up @@ -392,6 +399,14 @@ def upd_quality_definition(self, id_: int, data: JsonObject) -> JsonObject:
"""
return self._put(f"qualitydefinition/{id_}", self.ver_uri, data=data)

def get_quality_profile_schema(self) -> JsonArray:
"""Get the schemas for quality profiles
Returns:
JsonArray: List of dictionaries with items
"""
return self._get("qualityprofile/schema", self.ver_uri)

# INDEXER

# GET /indexer/schema
Expand Down
42 changes: 31 additions & 11 deletions pyarr/lidarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,23 +744,43 @@ def get_retag(

# POST /qualityprofile
def add_quality_profile(
self, name: str, upgrades_allowed: bool, cutoff: int, items: list
self,
name: str,
upgrade_allowed: bool,
cutoff: int,
schema: dict[str, Any],
language: dict,
min_format_score: int = 0,
cutoff_format_score: int = 0,
format_items: list = None,
) -> JsonObject:
"""Add new quality profile
"""Add new quality profile.
Args:
name (str): Name of the profile
upgrades_allowed (bool): Are upgrades in quality allowed?
upgrade_allowed (bool): Are upgrades in quality allowed?
cutoff (int): ID of quality definition to cutoff at. Must be an allowed definition ID.
items (list): Add a list of items (from `get_quality_definition()`)
schema (dict[str, Any]): Add the profile schema (from `get_quality_profile_schema()`)
language (dict): Language profile (from `get_language()`)
min_format_score (int, optional): Minimum format score. Defaults to 0.
cutoff_format_score (int, optional): Cutoff format score. Defaults to 0.
format_items (list, optional): Format items. Defaults to [].
Note:
Update the result from `get_quality_profile_schema()` set the items you need
from `"allowed": false` to `"allowed": true`. See tests for more details.
Returns:
JsonObject: An object containing the profile
"""
data = {
"name": name,
"upgradeAllowed": upgrades_allowed,
"cutoff": cutoff,
"items": items,
}
return self._post("qualityprofile", self.ver_uri, data=data)
if format_items is None:
format_items = []
schema["name"] = name
schema["upgradeAllowed"] = upgrade_allowed
schema["cutoff"] = cutoff
schema["formatItems"] = format_items
schema["language"] = language
schema["minFormatScore"] = min_format_score
schema["cutoffFormatScore"] = cutoff_format_score

return self._post("qualityprofile", self.ver_uri, data=schema)
15 changes: 14 additions & 1 deletion pyarr/models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,22 @@
within Arr api docs.
"""

PyarrHistorySortKey = Literal["time"]
PyarrHistorySortKey = Literal[
"id",
"date",
"eventType",
"series.title",
"episode.title",
"movieFile.relativePath",
"sourceTitle",
"status",
]
"""History sort keys
series.title (Sonarr)
episode.title (Sonarr)
status (Lidarr only)
Note:
There may be more, but these are not well documented
within Arr api docs.
Expand Down
3 changes: 3 additions & 0 deletions pyarr/models/lidarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@
"SpotifyPlaylist",
"SpotifySavedAlbums",
]

#: Lidarr History Sort Keys
LidarrHistorySortKey = Literal["sourceTitle", "status"]
44 changes: 34 additions & 10 deletions pyarr/radarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,26 +541,50 @@ def get_custom_filter(self) -> JsonArray:

# POST /qualityprofile
def add_quality_profile(
self, name: str, upgrades_allowed: bool, cutoff: int, items: list
self,
name: str,
schema: dict[str, Any],
cutoff: int,
upgrade_allowed: Optional[bool] = None,
language: Optional[dict[str, Any]] = None,
min_format_score: Optional[int] = None,
cutoff_format_score: Optional[int] = None,
format_items: Optional[list] = None,
) -> JsonObject:
"""Add new quality profile
Args:
name (str): Name of the profile
upgrades_allowed (bool): Are upgrades in quality allowed?
schema (dict[str, Any]): Add the profile schema (from `get_quality_profile_schema()`)
cutoff (int): ID of quality definition to cutoff at. Must be an allowed definition ID.
items (list): Add a list of items (from `get_quality_definition()`)
upgrade_allowed (bool, optional): Are upgrades in quality allowed?. Default provided by schema.
language (dict, optional): Language profile (from `get_language()`). Default provided by schema.
min_format_score (int, optional): Minimum format score. Default provided by schema.
cutoff_format_score (int, optional): Cutoff format score. Default provided by schema.
format_items (list, optional): Format items. Default provided by schema.
Note:
Update the result from `get_quality_profile_schema()` set the items you need
from `"allowed": false` to `"allowed": true`. See tests for more details.
Returns:
JsonObject: An object containing the profile
"""
data = {
"name": name,
"upgradeAllowed": upgrades_allowed,
"cutoff": cutoff,
"items": items,
}
return self._post("qualityprofile", self.ver_uri, data=data)
schema["name"] = name
schema["cutoff"] = cutoff

if format_items is not None:
schema["formatItems"] = format_items
if language is not None:
schema["language"] = language
if upgrade_allowed is not None:
schema["upgradeAllowed"] = upgrade_allowed
if min_format_score is not None:
schema["minFormatScore"] = min_format_score
if cutoff_format_score is not None:
schema["cutoffFormatScore"] = cutoff_format_score

return self._post("qualityprofile", self.ver_uri, data=schema)

# GET /manualimport
def get_manual_import(
Expand Down
28 changes: 23 additions & 5 deletions pyarr/readarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,20 +432,25 @@ def add_book(
return self._post("book", self.ver_uri, data=book)

# PUT /book/{id}
def upd_book(self, id_: int, data: JsonObject) -> JsonObject:
"""Update the given book, currently only monitored is changed, all other modifications are ignored.
def upd_book(self, book: JsonObject, editions: JsonArray) -> JsonObject:
"""Update the given book.
Note:
To be used in conjunction with get_book()
To be used in conjunction with get_book() and get_edition()
Currently only monitored states are updated (for the book and edition).
Args:
id_ (int): Book database ID to update
data (JsonObject): All parameters to update book
book (JsonObject): All parameters to update book
editions (JsonArray): List of editions to update book from `get_edition()`
Returns:
JsonObject: Dictionary with updated record
"""
return self._put(f"book/{id_}", self.ver_uri, data=data)
book["editions"] = editions

return self._put("book", self.ver_uri, data=book)

# PUT /book/monitor
def upd_book_monitor(
Expand Down Expand Up @@ -841,3 +846,16 @@ def upd_manual_import(self, data: JsonObject) -> JsonObject:
JsonObject: Dictionary of updated record
"""
return self._put("manualimport", self.ver_uri, data=data)

# GET /edition
def get_edition(self, id_: int) -> JsonArray:
"""Get edition's for specific book
Args:
id_ (int): Database ID of book
Returns:
JsonObject: Dictionary of editions
"""

return self._get("edition", self.ver_uri, params={"bookId": id_})
1 change: 0 additions & 1 deletion pyarr/request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ def _process_response(
if res.status_code == 502:
raise PyarrBadGateway("Bad Gateway. Check your server is accessible.")

print(res.status_code)
content_type = res.headers.get("Content-Type", "")
if "application/json" in content_type:
return res.json()
Expand Down
14 changes: 5 additions & 9 deletions pyarr/sonarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,7 @@ def add_series(
"searchForMissingEpisodes": search_for_missing_episodes,
}

response: dict[str, Any] = self._post("series", self.ver_uri, data=series)
for item in response:
if "errorMessage" in item:
raise Exception(item)
else:
continue

return response
return self._post("series", self.ver_uri, data=series)

# PUT /series
def upd_series(self, data: JsonObject) -> JsonObject:
Expand Down Expand Up @@ -617,7 +610,10 @@ def get_history(
Returns:
JsonObject: Dictionary with items
"""
params: dict[str, Union[int, PyarrHistorySortKey, PyarrSortDirection]] = {}
params: dict[
str,
Union[int, PyarrHistorySortKey, PyarrSortDirection],
] = {}

if page:
params["page"] = page
Expand Down
67 changes: 22 additions & 45 deletions tests/test_lidarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -781,16 +781,17 @@ def test_get_history(lidarr_client: LidarrAPI):
data = lidarr_client.get_history()
assert isinstance(data, dict)

data = lidarr_client.get_history(
page=1,
page_size=10,
sort_key="time",
sort_dir="default",
)
assert isinstance(data, dict)
for key in ["id", "date", "eventType", "status"]:
data = lidarr_client.get_history(
page=1,
page_size=10,
sort_key=key,
sort_dir="default",
)
assert isinstance(data, dict)

with contextlib.suppress(PyarrMissingArgument):
data = lidarr_client.get_history(sort_key="time")
data = lidarr_client.get_history(sort_key="date")
assert False

with contextlib.suppress(PyarrMissingArgument):
Expand All @@ -799,45 +800,16 @@ def test_get_history(lidarr_client: LidarrAPI):


def test_add_quality_profile(lidarr_client: LidarrAPI):
language = lidarr_client.get_language()[0]
schema = lidarr_client.get_quality_profile_schema()
schema["items"][1]["allowed"] = True

data = lidarr_client.add_quality_profile(
name="music",
upgrades_allowed=True,
cutoff=1005,
items=[
{"quality": {"id": 0, "name": "Unknown"}, "items": [], "allowed": True},
{
"name": "Lossless",
"items": [
{
"quality": {"id": 7, "name": "ALAC"},
"items": [],
"allowed": True,
},
{
"quality": {"id": 6, "name": "FLAC"},
"items": [],
"allowed": True,
},
{
"quality": {"id": 35, "name": "APE"},
"items": [],
"allowed": True,
},
{
"quality": {"id": 36, "name": "WavPack"},
"items": [],
"allowed": True,
},
{
"quality": {"id": 21, "name": "FLAC 24bit"},
"items": [],
"allowed": True,
},
],
"allowed": True,
"id": 1005,
},
],
upgrade_allowed=True,
cutoff=schema["items"][1]["id"],
schema=schema,
language=language,
)
assert isinstance(data, dict)

Expand Down Expand Up @@ -874,6 +846,11 @@ def test_upd_quality_definition(lidarr_client: LidarrAPI):
assert data["maxSize"] == rand_float


def test_get_quality_profile_schema(lidarr_client: LidarrAPI):
data = lidarr_client.get_quality_profile_schema()
assert isinstance(data, dict)


def test_get_indexer_schema(lidarr_client: LidarrAPI):
data = lidarr_client.get_indexer_schema()
assert isinstance(data, list)
Expand Down
Loading

0 comments on commit 2796541

Please sign in to comment.