Created
May 14, 2025 10:36
-
-
Save adamsthws/dfbb18525598a2a3396b8acb3770b24e to your computer and use it in GitHub Desktop.
koios_api_header_test.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Use this script to test the two variations of Koios API headers | |
# The script will test the headers by continuously and concurrently querying the Koios API | |
# Allow the test to run 5000 times in order to reach the Koios rate limit of the public tier | |
# Usage: | |
# Test the broken headers: ./koios_api_header_test.sh broken | |
# Test the working headers: ./koios_api_header_test.sh working | |
# Broken headers are the ones as defined in the env file: | |
# KOIOS_API_HEADERS=$($CURL -s "${KOIOS_API_HEADERS[@]}" "${URL}" | |
# Working headers are the ones as defined in the proposed changes to the new heathcheck: | |
# KOIOS_API_HEADERS=$(${CURL} -s ${KOIOS_API_TOKEN:+-H "Authorization: Bearer ${KOIOS_API_TOKEN}"} ${URL}) | |
KOIOS_API_TOKEN="<your_token_here>" | |
MAX_CONCURRENT=10 # Define the maximum number of concurrent processes | |
URL="https://api.koios.rest/api/v1/tip" # Mainnet | |
CURL=$(which curl) | |
JQ=$(which jq) | |
if [[ -z "${KOIOS_API_HEADERS[*]}" ]]; then | |
if [[ -n "${KOIOS_API_TOKEN}" ]]; then | |
KOIOS_API_HEADERS=(-H "'Authorization: Bearer ${KOIOS_API_TOKEN}'") | |
else | |
KOIOS_API_HEADERS=() | |
fi | |
fi | |
# Function to wait for background processes to finish | |
wait_for_jobs() { | |
while [ $(jobs -r | wc -l) -ge $MAX_CONCURRENT ]; do | |
sleep 0.1 | |
done | |
} | |
# Function to print the test results | |
print_test_results() { | |
local test_number=$1 | |
local koios_response=$2 | |
local command=$4 | |
local current_height=$5 | |
if [[ -z "$current_height" ]]; then | |
echo -e "\n----------------------------------------" | |
echo -e "Error. Test ${test_number} failed" | |
else | |
echo -e "\n----------------------------------------" | |
echo -e "\nTest ${test_number} passed" | |
echo -e "\nHeight reported by Koios API:\n${current_height}" | |
fi | |
echo -e "\nKoios output:\n\"${koios_response}\"" | |
echo -e "\nCommand used:\n${command}" | |
} | |
# Function to run the tests based on the argument | |
run_header_test() { | |
local test_type=$1 | |
local koios_response_command | |
if [[ "$test_type" == "broken" ]]; then | |
koios_response_command="$CURL -s \"${KOIOS_API_HEADERS[*]}\" \"${URL}\"" | |
elif [[ "$test_type" == "working" ]]; then | |
koios_response_command="$CURL -s ${KOIOS_API_TOKEN:+-H \"Authorization: Bearer ${KOIOS_API_TOKEN}\"} \"${URL}\"" | |
else | |
echo "Invalid argument. Use 'broken' or 'working'." | |
exit 1 | |
fi | |
# Test runs 5k+ times to get above the koios public daily tier rate limit | |
for i in {1..5010}; do | |
wait_for_jobs # Wait if the number of concurrent jobs reaches the limit | |
{ | |
# Query the Koios API | |
KOIOS_RESPONSE=$(eval $koios_response_command) | |
# Check if the response is valid json | |
if ! echo ${KOIOS_RESPONSE} | $JQ . &>/dev/null; then | |
print_test_results $i "${KOIOS_RESPONSE}" "${KOIOS_API_HEADERS[*]}" "${koios_response_command}" | |
else | |
CURRENT_HEIGHT=$(echo "${KOIOS_RESPONSE}" | $JQ '.[0].block_no') | |
print_test_results $i "${KOIOS_RESPONSE}" "${KOIOS_API_HEADERS[*]}" "${koios_response_command}" "$CURRENT_HEIGHT" | |
fi | |
} & # Run the block in the background | |
# Wait for all background jobs to finish | |
wait | |
done | |
} | |
# Call the function to run the tests with the provided argument | |
run_header_test "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the python script I mentioned that I used to examine the headers: