Path: blob/master/docker/dd-extension/backend/utils.py
1082 views
import logging12try:3from urllib.error import URLError4from urllib.request import Request, urlopen5except ImportError:6from urllib2 import Request, URLError, urlopen # type: ignore78lgr = logging.getLogger(__name__)91011def verify_token(token, license_key, is_stream=True):12if not token or not license_key:13raise ValueError("API token and license key is required.")1415path = "stream/license" if is_stream else "sdk-webhooks"16try:17req = Request(18f"https://api.platerecognizer.com/v1/{path}/{license_key.strip()}/"19)20req.add_header("Authorization", f"Token {token.strip()}")21urlopen(req).read()22return True, None23except URLError as e:24if "404" in str(e):25return (26False,27"The License Key cannot be found. Please use the correct License Key.",28)29elif str(403) in str(e):30return False, "The API Token cannot be found. Please use the correct Token."3132else:33return True, None34except Exception as e:35lgr.exception(e)36return False, str(e)373839