<commit_before>#!/usr/bin/env bash

# ALL-INKL KAS API shell interface
# Source: https://github.com/o1oo11oo/kasapi.sh
# See https://kasapi.kasserver.com/dokumentation/phpdoc/packages/API%20Funktionen.html for function documentation

# Find directory in which this script is stored by traversing all symbolic links
SOURCE="${0}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# POST target URL
AUTHURL="$(curl -s "http://kasapi.kasserver.com/soap/wsdl/KasAuth.wsdl" | grep -oP "(?<=<soap:address location=')[^']+" | sed 's|http://|https://|')"
APIURL="$(curl -s "http://kasapi.kasserver.com/soap/wsdl/KasApi.wsdl" | grep -oP "(?<=<soap:address location=')[^']+" | sed 's|http://|https://|')"

# Request template
AUTHREQUEST='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethodsKasApiAuthentication" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:KasAuth><Params xsi:type="xsd:string">{"KasUser":"USER","KasAuthType":"sha1","KasPassword":"<PASSWORD>","SessionLifeTime":SESSIONLIFE,"SessionUpdateLifeTime":"SESSIONUPDATE"}</Params></ns1:KasAuth></SOAP-ENV:Body></SOAP-ENV:Envelope>'
APIREQUEST='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethodsKasApi" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:KasApi><Params xsi:type="xsd:string">{"KasUser":"USER","KasAuthType":"AUTHTYPE","KasAuthData":"AUTHDATA","KasRequestType":"REQUESTTYPE","KasRequestParams":PARAMS}</Params></ns1:KasApi></SOAP-ENV:Body></SOAP-ENV:Envelope>'

# Default config values
session_lifetime="1800"
session_update_lifetime="Y"

_exiterr() {
  echo "ERROR: ${1}" >&2
  exit 1
}

# Get config
. "${SCRIPTDIR}/config.sh"
[[ -z ${kas_user} ]] && _exiterr '${kas_user}'" not found in config."
[[ -z ${kas_pass} ]] && _exiterr '${kas_pass}'" not found in config."
kas_pass_hash="$(printf "%s" "${kas_pass}" | sha1sum | awk '{print $1}')"

command_help() {
    local helptext
    read -r -d '' helptext <<"ENDL"
Usage: KASAPISH [-h] [command [argument]] [parameter [argument]] [parameter [argument]] ...

Default command: help

Commands:
 --help (-h)                    Show this help text
 --login (-l)                   Login to ALL-INKL and get a session token
 --function (-f)                API Function to call

Parameters:
 --no-session (-n)              Don't use/create session token for API request (default)
 --session (-s)                 Use/create session token, send request and return new token with result (if one got created)
 --token (-t)                   API session token for continuous API requests (implies --session)
 --params (-p)                  JSON formatted function parameters (defaults to "{}")
ENDL

    helptext="${helptext/KASAPISH/${0}}"
    echo "${helptext}"
}

# login to ALL-INKL
command_login() {
    # build login request
    local authreq="${AUTHREQUEST}"
    authreq="${authreq/USER/${kas_user}}"
    authreq="${authreq/PWHASH/${kas_pass_hash}}"
    authreq="${authreq/SESSIONLIFE/${session_lifetime}}"
    authreq="${authreq/SESSIONUPDATE/${session_update_lifetime}}"

    # send login request and receive session token
    login_response=$(curl -s -X POST -H "Content-Type: text/xml" -H "SOAPAction: \"urn:xmethodsKasApiAuthentication#KasAuth\"" --data "${authreq}" "${AUTHURL}")
    session_token=$(<<<"${login_response}" grep -oP "(?<=<return xsi:type=\"xsd:string\">)[^<]+")
    faultstring=$(<<<"${login_response}" grep -oP "(?<=<faultstring>)[^<]+")

    # check if login was successful
    [[ -n "${faultstring}" ]] && _exiterr "Login failed, faultstring: ${faultstring}"
    echo "${session_token}"
}

# make API request
command_api_request() {
    # check required parameters
    [[ -z "${PARAM_FUNCTION}" ]] && _exiterr "Missing parameter: --function"
    [[ -z "${PARAM_PARAMS}" ]] && PARAM_PARAMS="{}"

    # wait for KasFloodDelay
    if [[ -f "${SCRIPTDIR}/next_request_timestamp" && "$(<"${SCRIPTDIR}/next_request_timestamp")" -gt "$(date +%s%3N)" ]]; then
        local diff="$(($(<"${SCRIPTDIR}/next_request_timestamp")-$(date +%s%3N)))"
        sleep $(awk "BEGIN {printf \"%.1f\",${diff}/1000}")
    fi
<commit_msg>Add error handling for missing config file<commit_after>#!/usr/bin/env bash

# ALL-INKL KAS API shell interface
# Source: https://github.com/o1oo11oo/kasapi.sh
# See https://kasapi.kasserver.com/dokumentation/phpdoc/packages/API%20Funktionen.html for function documentation

# Find directory in which this script is stored by traversing all symbolic links
SOURCE="${0}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
SCRIPTDIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

# POST target URL
AUTHURL="$(curl -s "http://kasapi.kasserver.com/soap/wsdl/KasAuth.wsdl" | grep -oP "(?<=<soap:address location=')[^']+" | sed 's|http://|https://|')"
APIURL="$(curl -s "http://kasapi.kasserver.com/soap/wsdl/KasApi.wsdl" | grep -oP "(?<=<soap:address location=')[^']+" | sed 's|http://|https://|')"

# Request template
AUTHREQUEST='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethodsKasApiAuthentication" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:KasAuth><Params xsi:type="xsd:string">{"KasUser":"USER","KasAuthType":"sha1","KasPassword":"<PASSWORD>","SessionLifeTime":SESSIONLIFE,"SessionUpdateLifeTime":"SESSIONUPDATE"}</Params></ns1:KasAuth></SOAP-ENV:Body></SOAP-ENV:Envelope>'
APIREQUEST='<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:xmethodsKasApi" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:KasApi><Params xsi:type="xsd:string">{"KasUser":"USER","KasAuthType":"AUTHTYPE","KasAuthData":"AUTHDATA","KasRequestType":"REQUESTTYPE","KasRequestParams":PARAMS}</Params></ns1:KasApi></SOAP-ENV:Body></SOAP-ENV:Envelope>'

# Default config values
session_lifetime="1800"
session_update_lifetime="Y"

_exiterr() {
  echo "ERROR: ${1}" >&2
  exit 1
}

# Get config
if [[ -f "${SCRIPTDIR}/config.sh" ]]; then
	. "${SCRIPTDIR}/config.sh"
else
	_exiterr "No config file found"
fi

[[ -z ${kas_user} ]] && _exiterr '${kas_user}'" not found in config."
[[ -z ${kas_pass} ]] && _exiterr '${kas_pass}'" not found in config."
kas_pass_hash="$(printf "%s" "${kas_pass}" | sha1sum | awk '{print $1}')"

command_help() {
    local helptext
    read -r -d '' helptext <<"ENDL"
Usage: KASAPISH [-h] [command [argument]] [parameter [argument]] [parameter [argument]] ...

Default command: help

Commands:
 --help (-h)                    Show this help text
 --login (-l)                   Login to ALL-INKL and get a session token
 --function (-f)                API Function to call

Parameters:
 --no-session (-n)              Don't use/create session token for API request (default)
 --session (-s)                 Use/create session token, send request and return new token with result (if one got created)
 --token (-t)                   API session token for continuous API requests (implies --session)
 --params (-p)                  JSON formatted function parameters (defaults to "{}")
ENDL

    helptext="${helptext/KASAPISH/${0}}"
    echo "${helptext}"
}

# login to ALL-INKL
command_login() {
    # build login request
    local authreq="${AUTHREQUEST}"
    authreq="${authreq/USER/${kas_user}}"
    authreq="${authreq/PWHASH/${kas_pass_hash}}"
    authreq="${authreq/SESSIONLIFE/${session_lifetime}}"
    authreq="${authreq/SESSIONUPDATE/${session_update_lifetime}}"

    # send login request and receive session token
    login_response=$(curl -s -X POST -H "Content-Type: text/xml" -H "SOAPAction: \"urn:xmethodsKasApiAuthentication#KasAuth\"" --data "${authreq}" "${AUTHURL}")
    session_token=$(<<<"${login_response}" grep -oP "(?<=<return xsi:type=\"xsd:string\">)[^<]+")
    faultstring=$(<<<"${login_response}" grep -oP "(?<=<faultstring>)[^<]+")

    # check if login was successful
    [[ -n "${faultstring}" ]] && _exiterr "Login failed, faultstring: ${faultstring}"
    echo "${session_token}"
}

# make API request
command_api_request() {
    # check required parameters
    [[ -z "${PARAM_FUNCTION}" ]] && _exiterr "Missing parameter: --function"
    [[ -z "${PARAM_PARAMS}" ]] && PARAM_PARAMS="{}"

    # wait for KasFloodDelay
    if [[ -f "${SCRIPTDIR}/next_request_timestamp" && "$(<"${SCRIPTDIR}/next_request_timestamp")" -gt "$(date +%s%3N)" ]]; then
        local diff="$(($(<"${SCRIPTDIR}/next_request_timestamp")-$(date +%s%3N)))"
        sleep "$(awk "BEGIN {printf \"%.1f\",${diff}/1000}")"
    fi

{'fraction_non_alphanumeric': 0.19840716774514683, 'fraction_numerical': 0.011149825783972125, 'mean_word_length': 4.4776444929116686, 'pattern_counts': {'":': 20, '<': 81, '<?xml version=': 4, '>': 47, 'https://': 8, 'lorem ipsum': 0, 'www.': 8, 'xml': 38}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1809503', 'n_tokens_mistral': 3536, 'n_tokens_neox': 3365, 'n_words': 711}