# shellcheck shell=dash disable=SC2154

# X_CCMD_TIMEOUT=10
___x_cmd_shodan_curl(){
    local op="$1"
    local _api_url="$2"
    local option_arg="$3"

    local SHODAN_KEY=
    ___x_cmd_shodan_cur    SHODAN_KEY:=key  2>/dev/null
    if [ -z "$SHODAN_KEY" ]; then
        shodan:error "Please setting up your apikey first ==> 'x shodan --cfg key=<your apikey>'"
        shodan:warn "You can get the shodan apikey from: https://account.shodan.io/"
        return 1
    fi
    local auth="key=${SHODAN_KEY}"
    local ___X_CMD_SHODAN_SHOW_HTTP_CODE=1
    case "$op" in
        get)

            local url="${_api_url}?${auth}${option_arg}"
            if [ -z "$X_CCMD_TIMEOUT" ]; then
                ___x_cmd_shodan_api get "$url"
            else
                local x_;   ___x_cmd_shq1_ "$url"
                x ccmd "$X_CCMD_TIMEOUT" -- eval "___x_cmd_shodan_api get $x_"   # What if the url is with ""
            fi
            ;;

        post|put)
            ___x_cmd_shodan_api "$op" "${_api_url}?${auth}" "${option_arg}"  "$___X_CMD_API_SHODAN_HEADER" ;;

        del)
            ___x_cmd_shodan_api del "${_api_url}?${auth}" ;;
    esac

}

___x_cmd_shodan_curl_get(){
    local SHODAN_KEY=
    ___x_cmd_shodan_cur    SHODAN_KEY:=key  2>/dev/null
    if [ -z "$SHODAN_KEY" ]; then
        shodan:error "Please setting up your apikey first ==> 'x shodan --cfg key=<your apikey>'"
        shodan:warn "You can get the shodan apikey from: https://account.shodan.io/"
        return 1
    fi

    local url="${___X_CMD_SHODAN_API_URL}/${1#/}"; shift

    if [ -z "$X_CCMD_TIMEOUT" ]; then
        shodan:debug x curl --rate 1/s -G "$url" -d "key=${SHODAN_KEY}" "$@"
        x curl -s -G "$url" -d "key=${SHODAN_KEY}" "$@"
        # x curl --rate 1/s -s -G "$url" -d "key=${SHODAN_KEY}" "$@"
    else
        shodan:debug x curl --rate 1/s -G "$url" -d "key=${SHODAN_KEY}" "$@"
        x ccmd "$X_CCMD_TIMEOUT" -- x curl -s -G "$url" --data-urlencode "key=${SHODAN_KEY}" "$@"
        # x ccmd "$X_CCMD_TIMEOUT" -- x curl --rate 1/s -s -G "$url" --data-urlencode "key=${SHODAN_KEY}" "$@"
    fi
}

