46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
import requests
|
|
import urllib3
|
|
# Suppress only the single warning from urllib3.
|
|
urllib3.disable_warnings(category=urllib3.exceptions.InsecureRequestWarning)
|
|
import xmltodict
|
|
from requests.auth import HTTPBasicAuth
|
|
from datetime import datetime
|
|
|
|
from typing import Dict, List, Any
|
|
|
|
class ProjectorConnection():
|
|
ip: str
|
|
username: str
|
|
password: str
|
|
auth: HTTPBasicAuth
|
|
|
|
def __init__(self, ip: str, username: str, password: str) -> None:
|
|
self.ip = ip
|
|
self.username = username
|
|
self.password = password
|
|
self.auth = HTTPBasicAuth(username=self.username, password=self.password)
|
|
pass
|
|
|
|
def get(self, path: List[str], params: Dict[str, str] | None = None) -> Dict[str, Any] | None:
|
|
response: requests.Response = requests.get(
|
|
url=f"https://{self.ip}/{"/".join(path)}",
|
|
params=params,
|
|
headers={
|
|
"Accept": "application/xml"
|
|
},
|
|
auth=self.auth,
|
|
allow_redirects=False,
|
|
timeout=4,
|
|
verify=False
|
|
)
|
|
if response.status_code == 200: # HTTP ok response
|
|
rv: dict = {}
|
|
content: dict = xmltodict.parse(response.text)["SMSMessage"] # Common containert for all messages
|
|
header: dict = content["MessageHeader"]
|
|
body: dict = content["MessageBody"]
|
|
rv['type'] = header.get("Type", None)
|
|
rv['timestamp'] = int(header.get("Timestamp", datetime.now()))
|
|
rv['body'] = body[rv["type"]]
|
|
return rv
|
|
return None
|