2021-12-20 14:32:19 +00:00
|
|
|
from json import JSONDecodeError
|
2022-02-13 16:33:57 +00:00
|
|
|
from modules import util
|
2021-10-04 17:51:32 +00:00
|
|
|
from modules.util import Failed
|
|
|
|
|
2022-02-13 16:33:57 +00:00
|
|
|
logger = util.logger
|
2021-10-04 17:51:32 +00:00
|
|
|
|
|
|
|
base_url = "https://notifiarr.com/api/v1/"
|
|
|
|
dev_url = "https://dev.notifiarr.com/api/v1/"
|
|
|
|
|
|
|
|
|
2021-11-03 14:36:11 +00:00
|
|
|
class Notifiarr:
|
|
|
|
def __init__(self, config, params):
|
|
|
|
self.config = config
|
|
|
|
self.apikey = params["apikey"]
|
|
|
|
self.develop = params["develop"]
|
|
|
|
self.test = params["test"]
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.secret(self.apikey)
|
2021-12-21 18:48:27 +00:00
|
|
|
logger.debug(f"Environment: {'Test' if self.test else 'Develop' if self.develop else 'Production'}")
|
2021-11-03 14:36:11 +00:00
|
|
|
url, _ = self.get_url("user/validate/")
|
|
|
|
response = self.config.get(url)
|
2021-12-20 14:32:19 +00:00
|
|
|
try:
|
|
|
|
response_json = response.json()
|
|
|
|
except JSONDecodeError as e:
|
2021-12-21 18:48:27 +00:00
|
|
|
logger.debug(e)
|
|
|
|
raise Failed("Notifiarr Error: Invalid response")
|
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}")
|
2021-11-03 14:36:11 +00:00
|
|
|
if not params["test"] and not response_json["details"]["response"]:
|
2021-10-04 17:51:32 +00:00
|
|
|
raise Failed("Notifiarr Error: Invalid apikey")
|
|
|
|
|
2021-11-03 14:36:11 +00:00
|
|
|
def get_url(self, path):
|
|
|
|
url = f"{dev_url if self.develop else base_url}{'notification/test' if self.test else f'{path}{self.apikey}'}"
|
2021-12-03 22:26:38 +00:00
|
|
|
if self.config.trace_mode:
|
2022-02-13 16:33:57 +00:00
|
|
|
logger.debug(url)
|
2021-12-18 02:24:19 +00:00
|
|
|
params = {"event": "pmm"} if self.test else None
|
2021-11-03 14:36:11 +00:00
|
|
|
return url, params
|