_synu_get_quota.zsh

 1# SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
 2#
 3# SPDX-License-Identifier: Unlicense
 4
 5# Private function to fetch quota from Synthetic API
 6
 7_synu_get_quota() {
 8    # Check if API key is set
 9    if [[ -z "${SYNTHETIC_API_KEY}" ]]; then
10        print -u2 "Error: SYNTHETIC_API_KEY environment variable not set"
11        print -u2 "Set it with: export SYNTHETIC_API_KEY=your_api_key"
12        return 1
13    fi
14
15    # Fetch quota from API with fresh request (no caching)
16    local response
17    response=$(curl -s -f -H "Authorization: Bearer ${SYNTHETIC_API_KEY}" \
18        "https://api.synthetic.new/v2/quotas" 2>/dev/null)
19
20    # Check if curl failed or response is empty
21    if [[ $? -ne 0 || -z "${response}" ]]; then
22        return 1
23    fi
24
25    # Parse JSON response safely with jq
26    # Use fallback to 0 if field is missing/empty
27    local requests limit
28    requests=$(echo "${response}" | jq -r '.subscription.requests // 0')
29    limit=$(echo "${response}" | jq -r '.subscription.limit // 0')
30
31    # Return the values as space-separated string
32    echo "${requests} ${limit}"
33}