Wrapped HTTP responses in async context managers to prevent connection leaks.
This commit is contained in:
@@ -69,35 +69,34 @@ class OwncastClient:
|
|||||||
|
|
||||||
# Make a request to the endpoint
|
# Make a request to the endpoint
|
||||||
try:
|
try:
|
||||||
response = await self.session.request(
|
async with self.session.request(
|
||||||
"GET", status_url, allow_redirects=False
|
"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:
|
except Exception as e:
|
||||||
self.log.warning(
|
self.log.warning(
|
||||||
f"[{domain}] Error making GET request to {OWNCAST_STATUS_PATH}: {e}"
|
f"[{domain}] Error making GET request to {OWNCAST_STATUS_PATH}: {e}"
|
||||||
)
|
)
|
||||||
return None
|
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
|
# Validate the response contains all basic info needed
|
||||||
required_fields = [
|
required_fields = [
|
||||||
"lastConnectTime",
|
"lastConnectTime",
|
||||||
@@ -130,35 +129,34 @@ class OwncastClient:
|
|||||||
|
|
||||||
# Make a request to the endpoint
|
# Make a request to the endpoint
|
||||||
try:
|
try:
|
||||||
response = await self.session.request(
|
async with self.session.request(
|
||||||
"GET", config_url, allow_redirects=False
|
"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:
|
except Exception as e:
|
||||||
self.log.warning(
|
self.log.warning(
|
||||||
f"[{domain}] Error making GET request to {OWNCAST_CONFIG_PATH}: {e}"
|
f"[{domain}] Error making GET request to {OWNCAST_CONFIG_PATH}: {e}"
|
||||||
)
|
)
|
||||||
return None
|
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)
|
# Create StreamConfig from response (fields are truncated to max lengths)
|
||||||
return StreamConfig.from_api_response(config)
|
return StreamConfig.from_api_response(config)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user