From 3bb9e027873bfaa17da4b7a4a5e8daa68ddf333a Mon Sep 17 00:00:00 2001 From: busya Date: Fri, 5 Sep 2025 17:16:59 +0300 Subject: [PATCH] feat: Add test file for run 1757081819 --- gitea-client.zsh | 48 ++++++++++++++++++++++------------------ test_file_1757081819.txt | 0 2 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 test_file_1757081819.txt diff --git a/gitea-client.zsh b/gitea-client.zsh index c02d69b..b5ffbc2 100644 --- a/gitea-client.zsh +++ b/gitea-client.zsh @@ -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 } diff --git a/test_file_1757081819.txt b/test_file_1757081819.txt new file mode 100644 index 0000000..e69de29 -- 2.39.5