Skip to content

Instantly share code, notes, and snippets.

@lotas
Last active February 8, 2023 18:38
Show Gist options
  • Save lotas/e612954eab8947978c8026ba193302cd to your computer and use it in GitHub Desktop.
Save lotas/e612954eab8947978c8026ba193302cd to your computer and use it in GitHub Desktop.
make taskclsuter api calls from command line. Depends on 'hawk' package to generate auth headers
function tc_api() {
# this requires httpie package
# pip install httpie
local method="$1"
local url="$2"
local payload="$3"
if [[ "$payload" == "" ]]; then
payload="{}"
fi
cmd="https"
if [[ "${TASKCLUSTER_ROOT_URL}" =~ "http://" ]]; then
cmd="http"
fi
$cmd "${method}" "${TASKCLUSTER_ROOT_URL}api/${url}" --auth-type=hawk --auth="${TASKCLUSTER_CLIENT_ID}:${TASKCLUSTER_ACCESS_TOKEN}" --raw "$payload"
}
#!env node
const hawk = require('hawk');
const {
TASKCLUSTER_CLIENT_ID,
TASKCLUSTER_ROOT_URL,
TASKCLUSTER_ACCESS_TOKEN,
} = process.env;
if (!TASKCLUSTER_ROOT_URL) {
console.error('TASKCLUSTER_ROOT_URL is not defined');
process.exit(1);
}
const path = require('path');
if (process.argv.length < 3) {
console.error(`TC API call
Usage: ./${path.basename(__filename)} METHOD URL PAYLOAD
`);
process.exit(2);
}
let [, , method, url, payload] = process.argv;
url = `${TASKCLUSTER_ROOT_URL}api/${url}`;
payload = payload || '';
const headers = {};
// Authenticate, if credentials are provided
if (TASKCLUSTER_CLIENT_ID && TASKCLUSTER_ACCESS_TOKEN) {
// Create hawk authentication header
const hawkAuth = hawk.client.header(url, method.toUpperCase(), {
credentials: {
id: TASKCLUSTER_CLIENT_ID,
key: TASKCLUSTER_ACCESS_TOKEN,
algorithm: 'sha256',
},
ext: payload,
});
headers['Authorization'] = hawkAuth.header;
}
fetch(url, {
method,
headers,
}).catch(err => {
console.error(err);
}).then(async (res) => {
const data = await res.json();
console.log(data);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment