Plex-Meta-Manager/modules/notifiarr.py

40 lines
1.5 KiB
Python
Raw Normal View History

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