_synu_get_quota.fish

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