feat: implement S3 attachments and presigned uploads
This commit is contained in:
+20
-6
@@ -182,13 +182,27 @@ class Client:
|
||||
headers = {'Idempotency-Key':key or uuid.uuid4().hex} if LAB>=3 else {}
|
||||
return self.call('POST','/api/v1/chats/'+chat['id']+'/messages',201,actor=actor,body=body,headers=headers,schema='Message')
|
||||
|
||||
def attachment(self, actor, chat):
|
||||
def attachment(self, actor, chat, include_instance=False):
|
||||
content = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
||||
att = self.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',201 if LAB==4 else 202,actor=actor,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')['json']
|
||||
check(att['status']==('ready' if LAB==4 else 'queued'),'incorrect upload status')
|
||||
check(att['chat_id']==chat['id'] and att['owner_id']==actor['id'],'attachment ownership mismatch')
|
||||
check(att['sha256']==hashlib.sha256(content).hexdigest() and att['size_bytes']==len(content),'original checksum/size mismatch')
|
||||
return att,content
|
||||
response = self.call(
|
||||
'POST',
|
||||
'/api/v1/chats/' + chat['id'] + '/attachments?filename=sample.png',
|
||||
201 if LAB == 4 else 202,
|
||||
actor=actor,
|
||||
raw=content,
|
||||
headers={'Content-Type': 'image/png'},
|
||||
schema='Attachment'
|
||||
)
|
||||
att = response['json']
|
||||
check(att['status'] == ('ready' if LAB == 4 else 'queued'), 'incorrect upload status')
|
||||
check(att['chat_id'] == chat['id'] and att['owner_id'] == actor['id'], 'attachment ownership mismatch')
|
||||
check(
|
||||
att['sha256'] == hashlib.sha256(content).hexdigest()
|
||||
and att['size_bytes'] == len(content),
|
||||
'original checksum/size mismatch'
|
||||
)
|
||||
instance_id = response['headers'].get('X-Instance-Id', '')
|
||||
return (att, content, instance_id) if include_instance else (att, content)
|
||||
|
||||
def wait_ready(self, actor, att, wait_seconds=60):
|
||||
deadline = time.monotonic()+wait_seconds
|
||||
|
||||
Vendored
BIN
Binary file not shown.
|
After Width: | Height: | Size: 5.5 MiB |
+15
-5
@@ -40,12 +40,22 @@ def main():
|
||||
alice,bob,eve,chat = c.bootstrap()
|
||||
sent = c.message(alice,chat,'Сохранить при отказе')
|
||||
original = sent['json']
|
||||
target = sent['headers'].get('X-Instance-Id','')
|
||||
|
||||
target = sent['headers'].get('X-Instance-Id', '')
|
||||
attachment = content = None
|
||||
if LAB>=4:
|
||||
attachment,content = c.attachment(alice,chat)
|
||||
if LAB>=5:
|
||||
attachment = c.wait_ready(alice,attachment)
|
||||
|
||||
if LAB >= 4:
|
||||
attachment, content, upload_instance = c.attachment(alice, chat, include_instance=True)
|
||||
|
||||
if args.mode == 'failover':
|
||||
check(bool(upload_instance), 'attachment upload response has no X-Instance-Id')
|
||||
target = upload_instance
|
||||
|
||||
if LAB >= 5:
|
||||
attachment = c.wait_ready(alice, attachment)
|
||||
|
||||
if args.mode == 'failover':
|
||||
check(bool(target), 'target API instance ID is empty')
|
||||
env = dict(os.environ)
|
||||
env['TARGET_INSTANCE_ID'] = target
|
||||
observed = set()
|
||||
|
||||
Reference in New Issue
Block a user