Merge pull request '[AUTO-TEST] PR for issue 1757081819' (#14) from test/auto-branch-1757081819 into main
This commit is contained in:
@@ -83,48 +83,54 @@ fi
|
||||
#
|
||||
# @returns 0 on success, 1 on unsupported method. Curl exit code on curl failure.
|
||||
# [/CONTRACT]
|
||||
# ЗАМЕНИТЕ ВСЮ ФУНКЦИЮ api_request НА ЭТУ ВЕРСИЮ
|
||||
|
||||
function api_request() {
|
||||
local method="$1"
|
||||
local endpoint="$2"
|
||||
local data="$3"
|
||||
local url="$GITEA_URL/api/v1/$endpoint"
|
||||
|
||||
local -a curl_opts
|
||||
# Добавляем -i чтобы получить заголовки, включая HTTP-статус
|
||||
curl_opts=("-s" "-i" "-H" "Authorization: token $GITEA_TOKEN" "-H" "Content-Type: application/json")
|
||||
local http_code
|
||||
local response_body
|
||||
|
||||
# Переменная для хранения всего вывода curl
|
||||
local response
|
||||
# Создаем временный файл для хранения тела ответа
|
||||
local body_file=$(mktemp)
|
||||
|
||||
local -a curl_opts
|
||||
# -s: silent
|
||||
# -w '%{http_code}': записать http-код в stdout ПОСЛЕ ответа
|
||||
# -o "$body_file": записать тело ответа в файл
|
||||
curl_opts=("-s" "-w" "%{http_code}" "-o" "$body_file" \
|
||||
"-H" "Authorization: token $GITEA_TOKEN" \
|
||||
"-H" "Content-Type: application/json")
|
||||
|
||||
case "$method" in
|
||||
GET)
|
||||
response=$(curl "${curl_opts[@]}" "$url")
|
||||
GET|DELETE)
|
||||
http_code=$(curl "${curl_opts[@]}" -X "$method" "$url")
|
||||
;;
|
||||
POST|PATCH)
|
||||
response=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data")
|
||||
;;
|
||||
DELETE)
|
||||
response=$(curl "${curl_opts[@]}" -X "$method" "$url")
|
||||
http_code=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data")
|
||||
;;
|
||||
*)
|
||||
echo "Unsupported HTTP method: $method" >&2
|
||||
rm -f "$body_file" # Очистка перед выходом
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
# Извлекаем HTTP-статус из ответа
|
||||
local http_status=$(echo "$response" | head -n 1 | awk '{print $2}')
|
||||
# Извлекаем тело ответа (все, что после пустой строки, отделяющей заголовки)
|
||||
local body=$(echo "$response" | sed '1,/^\r$/d')
|
||||
response_body=$(<"$body_file")
|
||||
rm -f "$body_file" # Очистка после использования
|
||||
|
||||
# Проверяем, был ли запрос успешным (коды 2xx)
|
||||
if [[ "$http_status" -ge 200 && "$http_status" -lt 300 ]]; then
|
||||
echo "$body"
|
||||
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
||||
if [[ -z "$response_body" ]]; then
|
||||
echo "{\"http_status\": $http_code, \"body\": \"empty\"}"
|
||||
else
|
||||
echo "$response_body"
|
||||
fi
|
||||
return 0
|
||||
else
|
||||
# Если неуспешно, выводим ошибку и тело, если оно есть
|
||||
echo "API Error: Received HTTP status $http_status" >&2
|
||||
echo "$body" >&2
|
||||
echo "API Error: Received HTTP status $http_code. Body: $response_body" >&2
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
0
test_file_1757081819.txt
Normal file
0
test_file_1757081819.txt
Normal file
Reference in New Issue
Block a user