Plex-Meta-Manager/modules/notifiarr.py

37 lines
1.5 KiB
Python
Raw Permalink Normal View History

2021-12-20 14:32:19 +00:00
from json import JSONDecodeError
from modules import util
2021-10-04 17:51:32 +00:00
from modules.util import Failed
from tenacity import retry, stop_after_attempt, wait_fixed, retry_if_not_exception_type
2021-10-04 17:51:32 +00:00
logger = util.logger
2021-10-04 17:51:32 +00:00
base_url = "https://notifiarr.com/api/v1/"
2021-11-03 14:36:11 +00:00
class Notifiarr:
2024-05-28 20:22:51 +00:00
def __init__(self, requests, params):
self.requests = requests
2021-11-03 14:36:11 +00:00
self.apikey = params["apikey"]
2022-10-26 18:01:39 +00:00
self.header = {"X-API-Key": self.apikey}
logger.secret(self.apikey)
self._request(path="user", params={"fetch": "settings"})
2023-09-19 19:28:03 +00:00
def notification(self, json):
return self._request(json=json)
2023-09-19 19:28:03 +00:00
@retry(stop=stop_after_attempt(6), wait=wait_fixed(10), retry=retry_if_not_exception_type(Failed))
def _request(self, json=None, path="notification", params=None):
2024-05-28 20:22:51 +00:00
response = self.requests.get(f"{base_url}{path}/pmm/", json=json, headers=self.header, params=params)
2021-12-20 14:32:19 +00:00
try:
response_json = response.json()
except JSONDecodeError as e:
logger.debug(f"Content: {response.content}")
logger.error(e)
raise Failed("Notifiarr Error: Invalid JSON response received")
2021-10-25 20:51:51 +00:00
if response.status_code >= 400 or ("result" in response_json and response_json["result"] == "error"):
2021-11-03 14:36:11 +00:00
logger.debug(f"Response: {response_json}")
2021-10-04 17:51:32 +00:00
raise Failed(f"({response.status_code} [{response.reason}]) {response_json}")
2022-10-26 18:01:39 +00:00
if not response_json["details"]["response"]:
2021-10-04 17:51:32 +00:00
raise Failed("Notifiarr Error: Invalid apikey")
2023-09-19 20:07:33 +00:00
return response