Shell Scripting

Developer page

bash Example

Accessing the mDIS REST API with bash

This example uses Unix command line tools:

This example uses an mDIS loaded with data from the GRIND project.

Instructions

Put the code block below into a shell script, make it executable.

#!/usr/bin/env bash
## get data from mDIS
## knb Aug 2019, May 2020
if [[ -z "$user1password" ]]; then
echo "\$user1password env var is empty!"
echo "change the script code, or type this:"
echo "export user1password=user1password"
return
fi

host="https://data.icdp-online.org/mdis/dseis23" # or other
url="$host/api/v1/auth/login"
payload="{\"username\":\"user1\",\"password\":\"$user1password\"}"

## perform curl request to get bearer token
json=$(curl -s -H "Content-Type:application/json" \
    --data $payload $url)
# json="curl -s -H \"Content-Type:application/json\"     --data $payload $url"
# echo $json
# read -p "Press enter to continue"
token=$(echo $json | jq -r .token)
#echo -n $token

# perform curl request to fetch JSON data.
# here we return 5 "core" data items, from any site and hole, for simplicity
url2="$host/api/v1/form?name=core&per-page=5&page=0&sort=id"
json2=$(curl --globoff -s \
-H 'Accept: application/json, text/plain, */*'\
-H "Content-Type:application/json" -H "Authorization: Bearer $token" $url2)

# ## process JSON data with jq
echo "$json2" | jq -c '[.items[].id]' #.combined_id, .hole_id.hole_name])'
echo "$json2" | jq '.items | length | tostring + " cores found."'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

To set a password, run this command in a shell, before running this script above:

export user1password="...". Of course you can put in a different username/password, or change the source code of the script.

Then run the shell script.

Expected output with valid credentials:

[1,2,3,4,5]
"5 cores found."
1
2

Here, few data were found for the selected filter in $url2, to keep this codeblock short.

Getting names of all forms and models

Here we try the less exposed cg API, not the "common" REST API.

  • Call: "$host/cg/api/summary
  • jq filter: jq -c '[.forms[].name],[.models[].name] '
Expected Output
["cores","daily-message","expedition","hole","pieces","program","sample-request","sample-scientist","sample","section","site"]
["File","Core","InitGas","InitTemp","Pieces","Section","DailyMessage","Expedition","Explog","Hole","Program","Site","Request","Sample","Scientist"]
1
2

These are names of Tables and Input forms, they can come back in any order.

Expected output with WRONG username/password:

{
  "name": "Unauthorized",
  "message": "Your request was made with invalid credentials.",
  "code": 0,
  "status": 401,
  "type": "yii\\web\\UnauthorizedHttpException"
}
1
2
3
4
5
6
7

Getting all expeditions

WARNING

To get this example to work, there must exist a form named expedition in your mDIS.

Modify the above-mentioned script. Try the "$host/api/v1/form?name=expedition" call

and process it with

echo $json3 | jq '.items[] | {(.acr):(.chief_scientist)}'

Expected Result:

{"Hibo":"F. Anselmetti, A. Schwalb"}
{"GRIND":"Tony Prave"}
1
2

or similar.

Developer page

See developer page

REST API

See REST API page for extensive documentation.

More advanced examples

The mdis-installer (opens new window) git repository has more advanced use cases. There are shellscripts (opens new window) demonstrating how the mDIS REST API can be used for file upload, for example.