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,16 +51,16 @@ def matrix_room_id_from_alias(server, alias): ...@@ -49,16 +51,16 @@ def matrix_room_id_from_alias(server, alias):
if not server or not alias: if not server or not alias:
return "" return ""
r = requests.get(
"https://{}/_matrix/client/v3/directory/room/{}".format(
url_quote(server),
url_quote(alias),
),
)
try: try:
r = requests.get(
"https://{}/_matrix/client/v3/directory/room/{}".format(
url_quote(server),
url_quote(alias),
),
)
return r.json().get("room_id") return r.json().get("room_id")
except JSONDecodeError: except (JSONDecodeError, ConnectionError, LocationParseError):
return "" return ""
...@@ -66,19 +68,19 @@ def matrix_room_canonical_alias_from_id(server, token, room_id): ...@@ -66,19 +68,19 @@ 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 ""
r = requests.get(
"https://{}/_matrix/client/v3/rooms/{}/state/m.room.canonical_alias".format(
url_quote(server),
url_quote(room_id),
),
headers={
"Authorization": "Bearer {}".format(token),
},
)
try: try:
r = requests.get(
"https://{}/_matrix/client/v3/rooms/{}/state/m.room.canonical_alias".format(
url_quote(server),
url_quote(room_id),
),
headers={
"Authorization": "Bearer {}".format(token),
},
)
return r.json().get("alias") return r.json().get("alias")
except JSONDecodeError: except (JSONDecodeError, ConnectionError, LocationParseError):
return "" return ""
...@@ -86,17 +88,17 @@ def matrix_room_name_from_id(server, token, room_id): ...@@ -86,17 +88,17 @@ 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 ""
r = requests.get(
"https://{}/_matrix/client/v3/rooms/{}/state/m.room.name".format(
url_quote(server),
url_quote(room_id),
),
headers={
"Authorization": "Bearer {}".format(token),
},
)
try: try:
r = requests.get(
"https://{}/_matrix/client/v3/rooms/{}/state/m.room.name".format(
url_quote(server),
url_quote(room_id),
),
headers={
"Authorization": "Bearer {}".format(token),
},
)
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