Wrapped HTTP responses in async context managers to prevent connection leaks.
CD / Build (push) Successful in 8s
CI / Formatting (push) Successful in 6s
CI / Linting (push) Successful in 5s
CI / Type Checking (push) Successful in 8s
CI / Spelling (push) Successful in 5s

This commit is contained in:
2026-03-12 15:43:03 -04:00
parent afa1584ec5
commit 1429e2de11
+42 -44
View File
@@ -69,35 +69,34 @@ class OwncastClient:
# Make a request to the endpoint
try:
response = await self.session.request(
async with self.session.request(
"GET", status_url, allow_redirects=False
)
) as response:
# Check the response code is success
if response.status != 200:
self.log.warning(
f"[{domain}] Response to request on "
f"{OWNCAST_STATUS_PATH} was not 200, "
f"got {response.status} instead."
)
return None
# Try to interpret the response as JSON
try:
new_state = json.loads(await response.read())
except Exception as e:
self.log.warning(
f"[{domain}] Rejecting response to request on "
f"{OWNCAST_STATUS_PATH} as could not be "
f"interpreted as JSON: {e}"
)
return None
except Exception as e:
self.log.warning(
f"[{domain}] Error making GET request to {OWNCAST_STATUS_PATH}: {e}"
)
return None
# Check the response code is success
if response.status != 200:
self.log.warning(
f"[{domain}] Response to request on "
f"{OWNCAST_STATUS_PATH} was not 200, "
f"got {response.status} instead."
)
return None
# Try to interpret the response as JSON
try:
new_state = json.loads(await response.read())
except Exception as e:
self.log.warning(
f"[{domain}] Rejecting response to request on "
f"{OWNCAST_STATUS_PATH} as could not be "
f"interpreted as JSON: {e}"
)
return None
# Validate the response contains all basic info needed
required_fields = [
"lastConnectTime",
@@ -130,35 +129,34 @@ class OwncastClient:
# Make a request to the endpoint
try:
response = await self.session.request(
async with self.session.request(
"GET", config_url, allow_redirects=False
)
) as response:
# Check the response code is success
if response.status != 200:
self.log.warning(
f"[{domain}] Response to request on "
f"{OWNCAST_CONFIG_PATH} was not 200, "
f"got {response.status} instead."
)
return None
# Try to interpret the response as JSON
try:
config = json.loads(await response.read())
except Exception as e:
self.log.warning(
f"[{domain}] Rejecting response to request on "
f"{OWNCAST_CONFIG_PATH} as could not be "
f"interpreted as JSON: {e}"
)
return None
except Exception as e:
self.log.warning(
f"[{domain}] Error making GET request to {OWNCAST_CONFIG_PATH}: {e}"
)
return None
# Check the response code is success
if response.status != 200:
self.log.warning(
f"[{domain}] Response to request on "
f"{OWNCAST_CONFIG_PATH} was not 200, "
f"got {response.status} instead."
)
return None
# Try to interpret the response as JSON
try:
config = json.loads(await response.read())
except Exception as e:
self.log.warning(
f"[{domain}] Rejecting response to request on "
f"{OWNCAST_CONFIG_PATH} as could not be "
f"interpreted as JSON: {e}"
)
return None
# Create StreamConfig from response (fields are truncated to max lengths)
return StreamConfig.from_api_response(config)