<commit_before>#!/usr/bin/bash
#
# Copyright (c) 2013 Joyent Inc., All rights reserved.
#
# Common functions for sdc-* bash tools.
#


TOP=$(cd $(dirname $0)/../ 2>/dev/null; pwd)
CONFIG=$TOP/etc/config.json

if [[ $1 == "--no-headers" ]]; then
    CURL_OPTS="-4 --connect-timeout 10 -sS -H accept:application/json"
    shift
else
    CURL_OPTS="-4 --connect-timeout 10 -sS -i -H accept:application/json -H content-type:application/json"
fi


function fatal() {
    echo "$@" >&2
    exit 1
}


AMON_URL=
function amon() {
    local path=$1
    shift
    if [[ -z "$AMON_URL" ]]; then
        AMON_URL="http://$(json -f $CONFIG amon_domain)"
    fi
    (curl ${CURL_OPTS} --url "${AMON_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

CLOUDAPI_URL=
CLOUDAPI_SDC_KEY_ID=
function cloudapi() {
    local path=$1
    shift
    if [[ -z "$CLOUDAPI_URL" ]]; then
        CLOUDAPI_URL="https://$(json -f $CONFIG cloudapi_domain)"
    fi
    if [[ -z "$CLOUDAPI_SDC_KEY_ID" ]]; then
        CLOUDAPI_SDC_KEY_ID="$(json -f $CONFIG sdc_key_id)"
    fi
    local cuser=$CLOUDAPI_USER
    if [[ -z "$cuser" ]]; then
        cuser=admin
    fi

    # Sign with the old http-signature scheme.
    local now=$(date -u "+%a, %d %h %Y %H:%M:%S GMT")
    local signature=$(echo "$now" | tr -d '\n' | \
        openssl dgst -sha256 -sign $HOME/.ssh/sdc.id_rsa | \
        openssl enc -e -a | tr -d '\n')
    local authz="Authorization: Signature keyId=\"/$cuser/keys/$CLOUDAPI_SDC_KEY_ID\",algorithm=\"rsa-sha256\" $signature"
    local version="X-Api-Version:~7"

    (curl ${CURL_OPTS} -k -H "$version" -H "$authz" -H "Date: $now" \
        --url "${CLOUDAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

CNAPI_URL=
function cnapi() {
    local path=$1
    shift
    if [[ -z "$CNAPI_URL" ]]; then
        CNAPI_URL="http://$(json -f $CONFIG cnapi_domain)"
    fi
    (curl ${CURL_OPTS} --url "${CNAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

NAPI_URL=
function napi() {
    local path=$1
    shift
    if [[ -z "$NAPI_URL" ]]; then
        NAPI_URL="http://$(json -f $CONFIG napi_domain)"
    fi
    (curl ${CURL_OPTS} --url "${NAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

FWAPI_URL=
function fwapi() {
    local path=$1
    shift
    if [[ -z "$FWAPI_URL" ]]; then
        FWAPI_URL="http://$(json -f $CONFIG fwapi_domain)"
    fi
    (curl ${CURL_OPTS} --url "${FWAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

PAPI_URL=
function papi() {
    local path=$1
    shift
    if [[ -z "$PAPI_URL" ]]; then
        PAPI_URL="http://$(json -f $CONFIG papi_domain)"
    fi
    (curl -g ${CURL_OPTS} --url "${PAPI_URL}${path/\\\"/\"}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

SAPI_URL=
function sapi() {
    local path=$1
    shift
    if [[ -z "$SAPI_URL" ]]; then
        SAPI_URL="http://$(json -f $CONFIG sapi_domain)"
    fi
    (curl ${CURL_OPTS} --url "${SAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

WORKFLOW_URL=
function workflow() {
    local path=$1
    shift
    if [[ -z "$WORKFLOW_URL" ]]; then
        WORKFLOW_URL="http://$(json -f $CONFIG workflow_domain)"
    fi
    (curl ${CURL_OPTS} --url "${WORKFLOW_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

VMAPI_URL=
function vmapi() {<commit_msg>TOOLS-572: Add API version support to sdc-sapi<commit_after>#!/usr/bin/bash
#
# Copyright (c) 2013 Joyent Inc., All rights reserved.
#
# Common functions for sdc-* bash tools.
#


TOP=$(cd $(dirname $0)/../ 2>/dev/null; pwd)
CONFIG=$TOP/etc/config.json

if [[ $1 == "--no-headers" ]]; then
    CURL_OPTS="-4 --connect-timeout 10 -sS -H accept:application/json"
    shift
else
    CURL_OPTS="-4 --connect-timeout 10 -sS -i -H accept:application/json -H content-type:application/json"
fi


function fatal() {
    echo "$@" >&2
    exit 1
}


AMON_URL=
function amon() {
    local path=$1
    shift
    if [[ -z "$AMON_URL" ]]; then
        AMON_URL="http://$(json -f $CONFIG amon_domain)"
    fi
    (curl ${CURL_OPTS} --url "${AMON_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

CLOUDAPI_URL=
CLOUDAPI_SDC_KEY_ID=
function cloudapi() {
    local path=$1
    shift
    if [[ -z "$CLOUDAPI_URL" ]]; then
        CLOUDAPI_URL="https://$(json -f $CONFIG cloudapi_domain)"
    fi
    if [[ -z "$CLOUDAPI_SDC_KEY_ID" ]]; then
        CLOUDAPI_SDC_KEY_ID="$(json -f $CONFIG sdc_key_id)"
    fi
    local cuser=$CLOUDAPI_USER
    if [[ -z "$cuser" ]]; then
        cuser=admin
    fi

    # Sign with the old http-signature scheme.
    local now=$(date -u "+%a, %d %h %Y %H:%M:%S GMT")
    local signature=$(echo "$now" | tr -d '\n' | \
        openssl dgst -sha256 -sign $HOME/.ssh/sdc.id_rsa | \
        openssl enc -e -a | tr -d '\n')
    local authz="Authorization: Signature keyId=\"/$cuser/keys/$CLOUDAPI_SDC_KEY_ID\",algorithm=\"rsa-sha256\" $signature"
    local version="X-Api-Version:~7"

    (curl ${CURL_OPTS} -k -H "$version" -H "$authz" -H "Date: $now" \
        --url "${CLOUDAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

CNAPI_URL=
function cnapi() {
    local path=$1
    shift
    if [[ -z "$CNAPI_URL" ]]; then
        CNAPI_URL="http://$(json -f $CONFIG cnapi_domain)"
    fi
    (curl ${CURL_OPTS} --url "${CNAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

NAPI_URL=
function napi() {
    local path=$1
    shift
    if [[ -z "$NAPI_URL" ]]; then
        NAPI_URL="http://$(json -f $CONFIG napi_domain)"
    fi
    (curl ${CURL_OPTS} --url "${NAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

FWAPI_URL=
function fwapi() {
    local path=$1
    shift
    if [[ -z "$FWAPI_URL" ]]; then
        FWAPI_URL="http://$(json -f $CONFIG fwapi_domain)"
    fi
    (curl ${CURL_OPTS} --url "${FWAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

PAPI_URL=
function papi() {
    local path=$1
    shift
    if [[ -z "$PAPI_URL" ]]; then
        PAPI_URL="http://$(json -f $CONFIG papi_domain)"
    fi
    (curl -g ${CURL_OPTS} --url "${PAPI_URL}${path/\\\"/\"}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

SAPI_URL=
function sapi() {
    local path=$1
    shift
    if [[ -z "$SAPI_URL" ]]; then
        SAPI_URL="http://$(json -f $CONFIG sapi_domain)"
    fi
    if [[ -z "$SDC_API_VERSION" ]]; then
        SDC_API_VERSION="*"
    fi
    (curl ${CURL_OPTS} -H "accept-version: ${SDC_API_VERSION}" \
        --url "${SAPI_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

WORKFLOW_URL=
function workflow() {
    local path=$1
    shift
    if [[ -z "$WORKFLOW_URL" ]]; then
        WORKFLOW_URL="http://$(json -f $CONFIG workflow_domain)"
    fi
    (curl ${CURL_OPTS} --url "${WORKFLOW_URL}${path}" "$@") || return $?
    echo ""  # sometimes the result is not terminated with a newline
    return 0
}

VMAPI_URL=
function vmapi() {
{'fraction_non_alphanumeric': 0.17340965266033564, 'fraction_numerical': 0.010277091192923117, 'mean_word_length': 2.7158047365877236, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '5722998', 'n_tokens_mistral': 2924, 'n_tokens_neox': 2824, 'n_words': 765}