Make sending Matrix requests easier
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
from resolvematrix import ServerResolver
|
||||
from types import SimpleNamespace
|
||||
from collections import Counter
|
||||
import vona.config as config
|
||||
import nacl.signing
|
||||
@@ -10,7 +12,6 @@ import json
|
||||
import re
|
||||
|
||||
version = "1.4.3"
|
||||
http_client = httpx.Client(headers={"User-Agent": f"Vona/{version}"})
|
||||
|
||||
|
||||
def canonical_json(value):
|
||||
@@ -131,7 +132,12 @@ def pubkey() -> str:
|
||||
)
|
||||
|
||||
|
||||
def make_auth_header(destination, method, path, content=None) -> str:
|
||||
def make_auth_header(
|
||||
destination: str,
|
||||
method: str,
|
||||
path: str,
|
||||
content = None
|
||||
) -> str:
|
||||
request_json = {
|
||||
"method": method,
|
||||
"uri": path,
|
||||
@@ -255,6 +261,7 @@ def room_version_from_id(room_id):
|
||||
|
||||
return most_common_character(nums)[0]
|
||||
|
||||
|
||||
room_dir = {
|
||||
"chunk": [{
|
||||
"avatar_url": f"mxc://{config.server_name}/cat",
|
||||
@@ -270,3 +277,72 @@ room_dir = {
|
||||
}],
|
||||
"total_room_count_estimate": 1
|
||||
}
|
||||
|
||||
|
||||
class http_client:
|
||||
http = httpx.Client(headers={"User-Agent": f"Vona/{version}"})
|
||||
resolver = ServerResolver(client=http)
|
||||
|
||||
def _resolve(self, target) -> SimpleNamespace:
|
||||
r = self.resolver.resolve(target)
|
||||
|
||||
if r.sni:
|
||||
sni = r.sni
|
||||
else:
|
||||
sni = r.host_header
|
||||
|
||||
return SimpleNamespace(
|
||||
base_url=r.base_url,
|
||||
host_header=r.host_header,
|
||||
sni=sni
|
||||
)
|
||||
|
||||
def put(
|
||||
self,
|
||||
path: str,
|
||||
destination: str,
|
||||
headers: dict = {},
|
||||
authorize: bool = True,
|
||||
json: dict = {},
|
||||
):
|
||||
resolved = self._resolve(destination)
|
||||
|
||||
if authorize:
|
||||
headers["Authorization"] = make_auth_header(
|
||||
method="PUT",
|
||||
destination=destination,
|
||||
path=path,
|
||||
)
|
||||
|
||||
headers["Host"] = resolved.host_header
|
||||
|
||||
return self.http.put(
|
||||
f"{resolved.base_url}{path}",
|
||||
headers=headers,
|
||||
extensions={"sni_hostname": resolved.sni},
|
||||
json=json
|
||||
)
|
||||
|
||||
def get(
|
||||
self,
|
||||
path: str,
|
||||
destination: str,
|
||||
headers: dict = {},
|
||||
authorize: bool = True,
|
||||
):
|
||||
resolved = self._resolve(destination)
|
||||
|
||||
if authorize:
|
||||
headers["Authorization"] = make_auth_header(
|
||||
method="GET",
|
||||
destination=destination,
|
||||
path=path,
|
||||
)
|
||||
|
||||
headers["Host"] = resolved.host_header
|
||||
|
||||
return self.http.get(
|
||||
f"{resolved.base_url}{path}",
|
||||
headers=headers,
|
||||
extensions={"sni_hostname": resolved.sni}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user