Compare commits
2 Commits
b5accc304a
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 405a4f71f0 | |||
| 3bb9e02787 |
@@ -83,48 +83,54 @@ fi
|
|||||||
#
|
#
|
||||||
# @returns 0 on success, 1 on unsupported method. Curl exit code on curl failure.
|
# @returns 0 on success, 1 on unsupported method. Curl exit code on curl failure.
|
||||||
# [/CONTRACT]
|
# [/CONTRACT]
|
||||||
|
# ЗАМЕНИТЕ ВСЮ ФУНКЦИЮ api_request НА ЭТУ ВЕРСИЮ
|
||||||
|
|
||||||
function api_request() {
|
function api_request() {
|
||||||
local method="$1"
|
local method="$1"
|
||||||
local endpoint="$2"
|
local endpoint="$2"
|
||||||
local data="$3"
|
local data="$3"
|
||||||
local url="$GITEA_URL/api/v1/$endpoint"
|
local url="$GITEA_URL/api/v1/$endpoint"
|
||||||
|
|
||||||
local -a curl_opts
|
local http_code
|
||||||
# Добавляем -i чтобы получить заголовки, включая HTTP-статус
|
local response_body
|
||||||
curl_opts=("-s" "-i" "-H" "Authorization: token $GITEA_TOKEN" "-H" "Content-Type: application/json")
|
|
||||||
|
|
||||||
# Переменная для хранения всего вывода 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
|
case "$method" in
|
||||||
GET)
|
GET|DELETE)
|
||||||
response=$(curl "${curl_opts[@]}" "$url")
|
http_code=$(curl "${curl_opts[@]}" -X "$method" "$url")
|
||||||
;;
|
;;
|
||||||
POST|PATCH)
|
POST|PATCH)
|
||||||
response=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data")
|
http_code=$(curl "${curl_opts[@]}" -X "$method" -d @- "$url" <<< "$data")
|
||||||
;;
|
|
||||||
DELETE)
|
|
||||||
response=$(curl "${curl_opts[@]}" -X "$method" "$url")
|
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Unsupported HTTP method: $method" >&2
|
echo "Unsupported HTTP method: $method" >&2
|
||||||
|
rm -f "$body_file" # Очистка перед выходом
|
||||||
return 1
|
return 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Извлекаем HTTP-статус из ответа
|
response_body=$(<"$body_file")
|
||||||
local http_status=$(echo "$response" | head -n 1 | awk '{print $2}')
|
rm -f "$body_file" # Очистка после использования
|
||||||
# Извлекаем тело ответа (все, что после пустой строки, отделяющей заголовки)
|
|
||||||
local body=$(echo "$response" | sed '1,/^\r$/d')
|
|
||||||
|
|
||||||
# Проверяем, был ли запрос успешным (коды 2xx)
|
if [[ "$http_code" -ge 200 && "$http_code" -lt 300 ]]; then
|
||||||
if [[ "$http_status" -ge 200 && "$http_status" -lt 300 ]]; then
|
if [[ -z "$response_body" ]]; then
|
||||||
echo "$body"
|
echo "{\"http_status\": $http_code, \"body\": \"empty\"}"
|
||||||
|
else
|
||||||
|
echo "$response_body"
|
||||||
|
fi
|
||||||
return 0
|
return 0
|
||||||
else
|
else
|
||||||
# Если неуспешно, выводим ошибку и тело, если оно есть
|
echo "API Error: Received HTTP status $http_code. Body: $response_body" >&2
|
||||||
echo "API Error: Received HTTP status $http_status" >&2
|
|
||||||
echo "$body" >&2
|
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
0
test_file_1757081819.txt
Normal file
0
test_file_1757081819.txt
Normal file
Reference in New Issue
Block a user