Skip to content

Instantly share code, notes, and snippets.

@up42-epicycles
Last active January 18, 2022 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save up42-epicycles/cb241bbd104aab30274d80ced9e4b313 to your computer and use it in GitHub Desktop.
Save up42-epicycles/cb241bbd104aab30274d80ced9e4b313 to your computer and use it in GitHub Desktop.
UP42 API: Script to run your fist job via the API.
#!/bin/bash
# first_job_run.sh --- Simple bash script to run a first job via the
# UP42 API.
# Copyright (C) 2019 UP42 GmbH
# Author: António P. P. Almeida <antonio.almeida@up42.com>
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# Except as contained in this notice, the name(s) of the above copyright
# holders shall not be used in advertising or otherwise to promote the sale,
# use or other dealings in this Software without prior written authorization.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
SCRIPTNAME=${0##*/}
JOB_NAME="$RANDOM-My-first-job-run-via-API"
## Needed programs.
CURL=$(command -v curl) || exit 1
JQ=$(command -v jq) || exit 2
function print_usage() {
echo "Usage: $SCRIPTNAME -k <project API key> -p <project ID>"
}
## Check number of arguments.
if [ $# -lt 4 ]; then
print_usage
exit 3
fi
while getopts :k:p: OPT; do
case $OPT in
k|+k)
PKEY="$OPTARG"
;;
p|+p)
PROJ="$OPTARG"
;;
*)
print_usage
exit 4
esac
done
shift $(( OPTIND - 1 ))
OPTIND=1
## Job input.
JOB_INPUT=$(cat <<EOF
{
"oneatlas-pleiades-fullscene:1": {
"ids": null,
"time": "2018-01-01T00:00:00+00:00/2021-12-31T23:59:59+00:00",
"limit": 1,
"asset_ids": null,
"time_series": null,
"max_cloud_cover": 10,
"contains": {
"type": "Polygon",
"coordinates": [[[26.255884,47.100282],
[26.260523,47.102736],
[26.26645,47.100691],
[26.259578,47.094498],
[26.255884,47.100282]]]
}
},
"pansharpen:1": {
"bbox": null,
"method": "SFIM",
"contains": null,
"intersects": null,
"clip_to_aoi": false,
"include_pan": false
},
"ndvi:1": {
"output_original_raster": false
}
}
EOF
)
## Get the access token for the project.
PTOKEN=$($CURL -sX POST "https://$PROJ:$PKEY@api.up42.com/oauth/token" \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials' | $JQ -j '.data.accessToken')
## Get the workflow ID.
WORKFLOW_ID=$($CURL -s -L -H "Authorization: Bearer $PTOKEN" \
"https://api.up42.com/projects/$PROJ/jobs" | $JQ -j '.data[0] | .workflowId')
# Create the endpoint URL as variable.
URL_POST_JOB="https://api.up42.com/projects/$PROJ/workflows/$WORKFLOW_ID/jobs?name=$JOB_NAME"
## Create and run the job.
JOB="$($CURL -s -L -X POST -H "Authorization: Bearer $PTOKEN" \
-H 'Content-Type: application/json' \
$URL_POST_JOB -d "$JOB_INPUT")"
## Test job output to make sure we are not trying to run two jobs
## simultaneously.
if [ -z "$(echo $JOB | $JQ '.error as $e | if $e == null then "ok" else empty end')" ]; then
printf '%s\n' "$(echo $JOB | $JQ '.')"
exit 5
fi
## Extract the job ID.
JOB_ID=$(echo $JOB | $JQ -j '.data.id')
## Get the job status immediately after creating & launching it.
JOB_STATUS_URL="https://api.up42.com/projects/$PROJ/jobs/$JOB_ID"
JOB_STATUS=$($CURL -s -L -H "Authorization: Bearer $PTOKEN" \
$JOB_STATUS_URL | $JQ -j '.data.status')
printf 'Job %s is %s.\n' $JOB_ID $JOB_STATUS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment