[perf] activity auth & logger

This commit is contained in:
SouthFox 2023-07-28 01:21:24 +08:00
parent 03af5cedb7
commit cb2d3fd580
2 changed files with 27 additions and 3 deletions

View file

@ -3,6 +3,8 @@
import httpx
from demo import config
from demo.utils.key import get_pubkey_as_pem
from demo.httpsig import auth
from loguru import logger
AS_CTX = "https://www.w3.org/ns/activitystreams"
@ -63,7 +65,7 @@ def fetch(
url: str,
) -> dict:
"""Fetch url and return json"""
print(f"fetch {url}")
logger.debug(f"fetch {url}")
with httpx.Client() as client:
resp = client.get(
@ -75,3 +77,24 @@ def fetch(
)
return resp.json()
def post(
url: str,
payload : dict,
) -> httpx.Response :
logger.debug(f"send post, {payload=}")
with httpx.Client() as client:
resp = client.post(
url,
headers={
"User-Agent": config.USER_AGENT,
"Content-Type": config.AP_CONTENT_TYPE,
},
json=payload,
auth=auth,
)
resp.raise_for_status()
return resp

View file

@ -136,7 +136,7 @@ class HTTPXSigAuth(httpx.Auth):
else:
sigheaders = "(request-target) user-agent host date accept"
logger.warning(request.headers)
logger.info(request.headers)
sigdate = SignedData(
method = request.method,
@ -150,13 +150,14 @@ class HTTPXSigAuth(httpx.Auth):
sigdate
)
logger.debug(f"{to_be_signed}")
if not self.key:
raise ValueError("Should never happen")
signer = PKCS1_v1_5.new(self.key)
digest = SHA256.new()
digest.update(to_be_signed.encode("utf-8"))
sig = base64.b64encode(signer.sign(digest))
logger.debug(f"{sig=}")
key_id = f"{ID}#main-key"