Bash

The following set of Bash code snippets will allow you to craft your own custom shell script for your Linux CLI needs.
This script was built and tested on Debian 10 operating system. The script requires the following dependencies.
  1. 1.
    curl
  2. 2.
    wget
  3. 3.
    jq

Getting Super Powers

To make things easier to manage in your bash script, let's set up some globals, as well as our debug function, at the top of the file.
logware.sh
#!/bin/bash
apiUsername="MyLogwareUsername"
apiPassword="ekkuDiJxbz6n6RKjgrwXifJFET4WMNgvbaC9"
apiEnvironment="prod" #[test|prod]
enableDebug="1" #[0|1]
function debug {
[ "$enableDebug" = "1" ] && { echo "$1"; }
}
A unique API key is recommended for each use case in your application. In the event a single API key needs to be deactivated, this won't effect your other implementations.
Let's create dedicated functions for the Authentication method. This way you can check the status of the token quickly later.
function create_auth_token {
token=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data '{"login":"'"$apiUsername"'","password":"'"$apiPassword"'"}' https://api.logware.io/api/users/authenticate | jq -r '.token')
}
function check_auth_token {
if [ -f /tmp/$apiUsername.token ]; then #Check to see if a previously created token exists. It does exist.
if [ "$(find /tmp/$apiUsername.token -mmin +360)" ]; then #Check to see if a previously created token is not aged out
create_auth_token
echo "$token" > "/tmp/$apiUsername.token" #Save the token for next time, less auth requests
debug "Auth token had expired, recreated it."
else
token="$(cat /tmp/$apiUsername.token)" #use the auth token on file if not aged
debug "Auth token has not expired. Used the one on file."
fi
else #A previously created token did not exist.
create_auth_token
echo "$token" > "/tmp/$apiUsername.token" #save the token for next time, less auth requests
debug "Auth token didn't exist, creating one for the first time."
fi
}
Now the function to make the commit request to the API. This function will allow the caller to specify which of the the Logware Methods to use. The following commit function takes the Task Id response value and looks for the Transaction Id every 2 seconds till it gets one. Once complete, the Transaction Id is returned. That step is nice, as it encapsulates some work for you.
function commit_to_api {
# Write the data, get a TaskId from the API
w=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' -H "Authorization: Bearer $token" --data '{"env":"'"$apiEnvironment"'","data":"'"$c"'"}' https://api.logware.io/api/${method})
# Strip quotes from TaskId value
w="${w%\"}" && w="${w#\"}"
debug "TaskId: '$w'"
x="null"; i=1; while [ "$x" = "null" ] ; do
# Using the TaskId, get the TransactionId
x=$(curl -s -H 'Accept: application/json' -H "Authorization: Bearer $token" https://api.logware.io/api/${method}/$apiEnvironment/"${w}" | jq -r '.Content[0]')
debug "Data returned on attempt $i is '$x'"
[ $i -gt 5 ] && { exit;}
((i++))
sleep 2
done
}
The final code snippet is responsible for the Read and Write request options of this script.
while getopts ":m:r:w:" opt; do # only accepted values for the first argument are [hash|data|span].
case $opt in
m) method="$OPTARG"
;;
r) read="$OPTARG"
;;
w) write="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
;;
esac
done
if [ -n "$read" ]; then
# Example: ./logware.sh -r d254958cbbf634e2facc184748e2936c61bd266bea4c19d06e53be8233cabebe | base64 -d
wget -O - -q https://chaindata.logware.io/tx/"$read" | jq -r '.pkdata'
fi
if [ -n "$write" ]; then
# Example: ./logware.sh -w "Test" | base64 --encode
c=$write #What data are we stashing in Logware? Param should be a string value, .ie 'this is a string'
debug "Environment: $apiEnvironment"
debug "Payload: $c"
check_auth_token
debug "API key used: '$apiUsername'"
commit_to_api
echo "$x" #Print the TransactionId to the screen for future retrieval.
fi
In the above snippet, we are are using the chaindata resource to read back the data. It is very fast and doesn't tax the API with an additional call. The data pulled down from chaindata is them parsed with the jq package.
Don't forget to make your logware.sh file executable.