Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
parkpow
GitHub Repository: parkpow/deep-license-plate-recognition
Path: blob/master/docker/dd-extension/backend/utils.py
1082 views
1
import logging
2
3
try:
4
from urllib.error import URLError
5
from urllib.request import Request, urlopen
6
except ImportError:
7
from urllib2 import Request, URLError, urlopen # type: ignore
8
9
lgr = logging.getLogger(__name__)
10
11
12
def verify_token(token, license_key, is_stream=True):
13
if not token or not license_key:
14
raise ValueError("API token and license key is required.")
15
16
path = "stream/license" if is_stream else "sdk-webhooks"
17
try:
18
req = Request(
19
f"https://api.platerecognizer.com/v1/{path}/{license_key.strip()}/"
20
)
21
req.add_header("Authorization", f"Token {token.strip()}")
22
urlopen(req).read()
23
return True, None
24
except URLError as e:
25
if "404" in str(e):
26
return (
27
False,
28
"The License Key cannot be found. Please use the correct License Key.",
29
)
30
elif str(403) in str(e):
31
return False, "The API Token cannot be found. Please use the correct Token."
32
33
else:
34
return True, None
35
except Exception as e:
36
lgr.exception(e)
37
return False, str(e)
38
39