Skip to content

Commit

Permalink
Fix http calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Xyaren committed Jul 7, 2024
1 parent 64ef4d6 commit bf03e2c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
19 changes: 11 additions & 8 deletions custom_components/magentatv/api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from xml.sax.saxutils import escape

from async_upnp_client.aiohttp import AiohttpRequester
from async_upnp_client.const import HttpRequest
from async_upnp_client.exceptions import UpnpCommunicationError, UpnpConnectionError, UpnpConnectionTimeoutError

from .const import LOGGER, KeyCode
Expand Down Expand Up @@ -187,14 +188,16 @@ async def _async_send_upnp_soap(
"</s:Envelope>"
)
return await self._requester.async_http_request(
method="POST",
url=f"{self._url}/upnp/service/{service}/Control",
headers={
"SOAPACTION": f"urn:schemas-upnp-org:service:{service}:1#{action}",
"HOST": f"{self._host}:{self._port}",
"Content-Type": 'text/xml; charset="utf-8"',
},
body=full_body,
http_request=HttpRequest(
method="POST",
url=f"{self._url}/upnp/service/{service}/Control",
headers={
"SOAPACTION": f"urn:schemas-upnp-org:service:{service}:1#{action}",
"HOST": f"{self._host}:{self._port}",
"Content-Type": 'text/xml; charset="utf-8"',
},
body=full_body,
)
)
except UpnpConnectionTimeoutError as ex:
raise CommunicationTimeoutException() from ex
Expand Down
52 changes: 29 additions & 23 deletions custom_components/magentatv/api/notify_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from aiohttp import web
from async_upnp_client.aiohttp import AiohttpRequester
from async_upnp_client.client import NS
from async_upnp_client.const import HttpRequest
from async_upnp_client.exceptions import UpnpCommunicationError, UpnpConnectionTimeoutError
from async_upnp_client.utils import get_local_ip

Expand Down Expand Up @@ -132,16 +133,17 @@ async def _async_subscribe(self, target, service) -> str:
adv_port = self._listen_ip_port[1]

response = await self._requester.async_http_request(
method="SUBSCRIBE",
url=url,
headers={
"NT": "upnp:event",
"TIMEOUT": f"Second-{self._subscription_timeout}",
"HOST": f"{target[0]}:{target[1]}",
"CALLBACK": f"<http://{adv_host}:{adv_port}/eventSub>",
},
body=None,
)
http_request=HttpRequest(
method="SUBSCRIBE",
url=url,
headers={
"NT": "upnp:event",
"TIMEOUT": f"Second-{self._subscription_timeout}",
"HOST": f"{target[0]}:{target[1]}",
"CALLBACK": f"<http://{adv_host}:{adv_port}/eventSub>",
},
body=None,
))
assert response[0] == 200
sid = response[1]["SID"]
LOGGER.debug("Subscribed %s on %s at %s", sid, service, target)
Expand All @@ -150,26 +152,30 @@ async def _async_subscribe(self, target, service) -> str:
@wrap_exceptions
async def _async_resubscribe(self, target, service, sid) -> str:
response = await self._requester.async_http_request(
method="SUBSCRIBE",
url=f"http://{target[0]}:{target[1]}/upnp/service/{service}/Event",
headers={
"SID": sid,
"TIMEOUT": f"Second-{self._subscription_timeout}",
},
body=None,
http_request=HttpRequest(
method="SUBSCRIBE",
url=f"http://{target[0]}:{target[1]}/upnp/service/{service}/Event",
headers={
"SID": sid,
"TIMEOUT": f"Second-{self._subscription_timeout}",
},
body=None,
)
)
assert response[0] == 200
return response[1]["SID"]

@wrap_exceptions
async def _async_unsubscribe(self, target, service, sid) -> str:
response = await self._requester.async_http_request(
method="UNSUBSCRIBE",
url=f"http://{target[0]}:{target[1]}/upnp/service/{service}/Event",
headers={
"SID": sid,
},
body=None,
http_request=HttpRequest(
method="UNSUBSCRIBE",
url=f"http://{target[0]}:{target[1]}/upnp/service/{service}/Event",
headers={
"SID": sid,
},
body=None,
)
)
assert response[0] in [200, 412]
LOGGER.debug("Unsubscribed %s on %s at %s", sid, service, target)
Expand Down

0 comments on commit bf03e2c

Please sign in to comment.