simplify misc.request

This commit is contained in:
Izalia Mae 2022-11-05 20:07:44 -04:00
parent 729477820f
commit 8ca198b611

View file

@ -222,16 +222,14 @@ async def request(uri, data=None, force=False, sign_headers=True, activity=True)
url = urlparse(uri) url = urlparse(uri)
method = 'POST' if data else 'GET' method = 'POST' if data else 'GET'
headers = {'User-Agent': 'ActivityRelay'} action = data.get('type') if data else None
mimetype = 'application/activity+json' if activity else 'application/json' headers = {
'Accept': 'application/activity+json, application/json;q=0.9',
'User-Agent': 'ActivityRelay',
}
## Set the content type for a POST if data:
if data and 'Content-Type' not in headers: headers['Content-Type'] = 'application/activity+json' if activity else 'application/json'
headers['Content-Type'] = mimetype
## Set the accepted content type for a GET
elif not data and 'Accept' not in headers:
headers['Accept'] = mimetype
if sign_headers: if sign_headers:
signing_headers = { signing_headers = {
@ -243,7 +241,6 @@ async def request(uri, data=None, force=False, sign_headers=True, activity=True)
if data: if data:
assert isinstance(data, dict) assert isinstance(data, dict)
action = data.get('type')
data = json.dumps(data) data = json.dumps(data)
signing_headers.update({ signing_headers.update({
'Digest': f'SHA-256={generate_body_digest(data)}', 'Digest': f'SHA-256={generate_body_digest(data)}',
@ -258,26 +255,24 @@ async def request(uri, data=None, force=False, sign_headers=True, activity=True)
headers.update(signing_headers) headers.update(signing_headers)
try: try:
# json_serializer=DotDict maybe?
async with ClientSession(trace_configs=http_debug()) as session, app['semaphore']: async with ClientSession(trace_configs=http_debug()) as session, app['semaphore']:
async with session.request(method, uri, headers=headers, data=data) as resp: async with session.request(method, uri, headers=headers, data=data) as resp:
## aiohttp has been known to leak if the response hasn't been read, ## aiohttp has been known to leak if the response hasn't been read,
## so we're just gonna read the request no matter what ## so we're just gonna read the request no matter what
resp_data = await resp.read() resp_data = await resp.json()
resp_payload = json.loads(resp_data.decode('utf-8'))
if resp.status not in [200, 202]: if resp.status not in [200, 202]:
if not data: if not resp_data:
logging.verbose(f'Received error when requesting {uri}: {resp.status} {resp_payload}') logging.verbose(f'Received error when requesting {uri}: {resp.status} {resp_data}')
return return
logging.verbose(f'Received error when sending {action} to {uri}: {resp.status} {resp_payload}') logging.verbose(f'Received error when sending {action} to {uri}: {resp.status} {resp_data}')
return return
logging.debug(f'{uri} >> resp {resp_payload}') logging.debug(f'{uri} >> resp {resp_data}')
app['cache'].json[uri] = resp_payload app['cache'].json[uri] = resp_data
return resp_payload return resp_data
except JSONDecodeError: except JSONDecodeError:
return return