Skip to content
Snippets Groups Projects
Commit 64eb2c17 authored by Felix Schäfer's avatar Felix Schäfer :construction_worker:
Browse files

Gracefully handle non-hostname matrix servers #16

parent 13503e62
No related branches found
No related tags found
No related merge requests found
Pipeline #171284 passed
import requests import requests
from json import JSONDecodeError from json import JSONDecodeError
from requests.exceptions import ConnectionError
from urllib3.exceptions import LocationParseError
from urllib.parse import quote as url_quote from urllib.parse import quote as url_quote
...@@ -49,6 +51,7 @@ def matrix_room_id_from_alias(server, alias): ...@@ -49,6 +51,7 @@ def matrix_room_id_from_alias(server, alias):
if not server or not alias: if not server or not alias:
return "" return ""
try:
r = requests.get( r = requests.get(
"https://{}/_matrix/client/v3/directory/room/{}".format( "https://{}/_matrix/client/v3/directory/room/{}".format(
url_quote(server), url_quote(server),
...@@ -56,9 +59,8 @@ def matrix_room_id_from_alias(server, alias): ...@@ -56,9 +59,8 @@ def matrix_room_id_from_alias(server, alias):
), ),
) )
try:
return r.json().get("room_id") return r.json().get("room_id")
except JSONDecodeError: except (JSONDecodeError, ConnectionError, LocationParseError):
return "" return ""
...@@ -66,6 +68,7 @@ def matrix_room_canonical_alias_from_id(server, token, room_id): ...@@ -66,6 +68,7 @@ def matrix_room_canonical_alias_from_id(server, token, room_id):
if not server or not token or not room_id: if not server or not token or not room_id:
return "" return ""
try:
r = requests.get( r = requests.get(
"https://{}/_matrix/client/v3/rooms/{}/state/m.room.canonical_alias".format( "https://{}/_matrix/client/v3/rooms/{}/state/m.room.canonical_alias".format(
url_quote(server), url_quote(server),
...@@ -76,9 +79,8 @@ def matrix_room_canonical_alias_from_id(server, token, room_id): ...@@ -76,9 +79,8 @@ def matrix_room_canonical_alias_from_id(server, token, room_id):
}, },
) )
try:
return r.json().get("alias") return r.json().get("alias")
except JSONDecodeError: except (JSONDecodeError, ConnectionError, LocationParseError):
return "" return ""
...@@ -86,6 +88,7 @@ def matrix_room_name_from_id(server, token, room_id): ...@@ -86,6 +88,7 @@ def matrix_room_name_from_id(server, token, room_id):
if not server or not token or not room_id: if not server or not token or not room_id:
return "" return ""
try:
r = requests.get( r = requests.get(
"https://{}/_matrix/client/v3/rooms/{}/state/m.room.name".format( "https://{}/_matrix/client/v3/rooms/{}/state/m.room.name".format(
url_quote(server), url_quote(server),
...@@ -96,7 +99,6 @@ def matrix_room_name_from_id(server, token, room_id): ...@@ -96,7 +99,6 @@ def matrix_room_name_from_id(server, token, room_id):
}, },
) )
try:
return r.json().get("name") return r.json().get("name")
except JSONDecodeError: except (JSONDecodeError, ConnectionError, LocationParseError):
return "" return ""
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment