54 lines
2.4 KiB
Python
Executable File
54 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Local synthetic demonstration; credentials stay in ignored secrets/demo.json."""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import sys
|
|
import uuid
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0,str(ROOT/'tests'))
|
|
from client import Client
|
|
|
|
parser=argparse.ArgumentParser()
|
|
parser.add_argument('action',choices=['register','chat','message','history','instances','idempotency'])
|
|
parser.add_argument('--text',default='Синтетическое сообщение для демонстрации')
|
|
parser.add_argument('--key')
|
|
args=parser.parse_args()
|
|
os.umask(0o077)
|
|
state_file=ROOT/'secrets/demo.json'
|
|
state=json.loads(state_file.read_text()) if state_file.exists() else {}
|
|
c=Client()
|
|
if args.action=='register':
|
|
state={'actor':c.user('demo')}
|
|
print('Registered and authenticated synthetic user:',state['actor']['id'])
|
|
elif args.action=='chat':
|
|
state['chat']=c.call('POST','/api/v1/chats',201,actor=state['actor'],body={'title':'Демонстрация','member_ids':[state['actor']['id']]},schema='Chat')['json']
|
|
print(json.dumps(state['chat'],ensure_ascii=False,indent=2))
|
|
elif args.action=='message':
|
|
result=c.message(state['actor'],state['chat'],args.text,key=args.key)
|
|
print('HTTP',result['status'],'instance',result['headers']['X-Instance-Id'])
|
|
print(json.dumps(result['json'],ensure_ascii=False,indent=2))
|
|
elif args.action=='history':
|
|
from resilience import history
|
|
print(json.dumps(history(c,state['actor'],state['chat']),ensure_ascii=False,indent=2))
|
|
elif args.action=='instances':
|
|
seen=set()
|
|
for _ in range(12):
|
|
result=c.call('GET','/api/v1/users/me',actor=state['actor'],schema='User')
|
|
alias=result['headers']['X-Instance-Id']
|
|
seen.add(alias)
|
|
print('HTTP 200 instance',alias,'same_user',result['json']['id']==state['actor']['id'])
|
|
print('Distinct API processes:',len(seen))
|
|
elif args.action=='idempotency':
|
|
key=uuid.uuid4().hex
|
|
first=c.message(state['actor'],state['chat'],'idem demonstration',key)
|
|
second=c.message(state['actor'],state['chat'],'idem demonstration',key)
|
|
assert first['json']==second['json']
|
|
path='/api/v1/chats/'+state['chat']['id']+'/messages'
|
|
c.call('POST',path,409,actor=state['actor'],body={'text':'changed'},headers={'Idempotency-Key':key})
|
|
print('PASS: 201 + 201 same message',first['json']['id'],'; changed body 409')
|
|
state_file.parent.mkdir(exist_ok=True)
|
|
state_file.write_text(json.dumps(state))
|