forked from mtusi2026/mtusi2026lab2
реализация второй лабы
This commit is contained in:
+22
-37
@@ -1,42 +1,27 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
if ! command -v jq >/dev/null; then
|
||||
echo "Для demo.sh нужен jq" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
base_url="${BASE_URL:-http://localhost:8080}"
|
||||
base_url="${base_url%/}"
|
||||
|
||||
suffix="$(date +%s)_$RANDOM"
|
||||
|
||||
post_json() {
|
||||
local path="$1"
|
||||
local body="$2"
|
||||
shift 2
|
||||
curl --fail --silent --show-error --connect-timeout 5 --max-time 10 \
|
||||
-H "Content-Type: application/json" --data "$body" "$@" "$base_url$path"
|
||||
# Только синтетические пароли; токены остаются в памяти shell и не выводятся.
|
||||
password="Demo-password-$suffix"
|
||||
register() {
|
||||
local username="$1" display_name="$2"
|
||||
jq -nc --arg u "$username" --arg d "$display_name" --arg p "$password" \
|
||||
'{username:$u, display_name:$d, password:$p}' |
|
||||
curl -fsS -H 'Content-Type: application/json' --data-binary @- "$base_url/api/v1/users" | jq -er '.id'
|
||||
}
|
||||
|
||||
echo "Создаём Алису и Боба"
|
||||
alice_body="$(jq -nc --arg name "alice_$suffix" '{username: $name, display_name: "Алиса"}')"
|
||||
bob_body="$(jq -nc --arg name "bob_$suffix" '{username: $name, display_name: "Боб"}')"
|
||||
alice_id="$(post_json /api/v1/users "$alice_body" | jq -er '.id')"
|
||||
bob_id="$(post_json /api/v1/users "$bob_body" | jq -er '.id')"
|
||||
|
||||
echo "Создаём общий чат"
|
||||
chat_body="$(jq -nc --arg alice "$alice_id" --arg bob "$bob_id" \
|
||||
'{title: "Учебный чат", member_ids: [$alice, $bob]}')"
|
||||
chat_id="$(post_json /api/v1/chats "$chat_body" -H "X-User-Id: $alice_id" | jq -er '.id')"
|
||||
path="/api/v1/chats/$chat_id/messages"
|
||||
|
||||
echo "Отправляем сообщение от Алисы"
|
||||
post_json "$path" '{"text":"Привет, Боб! 👋"}' -H "X-User-Id: $alice_id" | jq .
|
||||
|
||||
echo "Отправляем ответ от Боба"
|
||||
post_json "$path" '{"text":"Привет, Алиса!"}' -H "X-User-Id: $bob_id" | jq .
|
||||
|
||||
echo "Читаем общую историю от имени Алисы"
|
||||
curl --fail --silent --show-error --connect-timeout 5 --max-time 10 \
|
||||
-H "X-User-Id: $alice_id" "$base_url$path?limit=50" | jq .
|
||||
login() {
|
||||
curl -fsS -X POST -u "$1:$password" "$base_url/api/v1/auth/sessions" | jq -er '.token'
|
||||
}
|
||||
alice_id="$(register "alice_$suffix" Алиса)"
|
||||
bob_id="$(register "bob_$suffix" Боб)"
|
||||
alice_token="$(login "alice_$suffix")"
|
||||
bob_token="$(login "bob_$suffix")"
|
||||
chat_id="$(jq -nc --arg a "$alice_id" --arg b "$bob_id" '{title:"Учебный чат",member_ids:[$a,$b]}' |
|
||||
curl -fsS -H "Authorization: Bearer $alice_token" -H 'Content-Type: application/json' \
|
||||
--data-binary @- "$base_url/api/v1/chats" | jq -er '.id')"
|
||||
for token in "$alice_token" "$bob_token"; do
|
||||
curl -fsS -H "Authorization: Bearer $token" -H 'Content-Type: application/json' \
|
||||
-d '{"text":"Привет из lab2! 👋"}' "$base_url/api/v1/chats/$chat_id/messages" | jq .
|
||||
done
|
||||
curl -fsS -H "Authorization: Bearer $bob_token" "$base_url/api/v1/chats/$chat_id/messages" | jq .
|
||||
|
||||
Reference in New Issue
Block a user