#!/bin/bash
# ======================================================================
#
# REST API CLIENT
#
# Example 1 :: Simple GET request
# This is an introduction, how it works.
#
# ======================================================================

cd "$( dirname "$0" )" || exit

# shellcheck source=../rest-api-client.sh
. ../http.class.sh

# shellcheck source=color.class.sh
. color.class.sh

sURL="http://www.iml.unibe.ch/"

# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------

function wait4Return(){

    local _sleep
    _sleep=${1:-10} # default 10
    echo
    echo
    color.print "cyan" "Press return to continue or wait $_sleep seconds ... "
    read -r -s -t $_sleep dummy
    echo
    echo
    echo
}


# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

color.fg "yellow"
cat <<EOH
----------------------------------------------------------------------

Example 1 :: Simple GET request
This is a introduction, how it works.

----------------------------------------------------------------------

EOH
color.reset


echo "We need to initialize the client with 'http.init' before a new request."
echo
color.echo "green" "> http.init"
http.init

wait4Return 10

echo "Let's make a GET request to our example url $sURL."
echo
color.echo "green" "> http.makeRequest GET '$sURL'"
http.makeRequest GET "$sURL"
echo "Done."
echo "Here is no output."
echo "Now we can read information from the response."

wait4Return 10

color.echo "green" "> http.getStatuscode - This returns the Http status code"
http.getStatuscode
echo
sleep 1

color.echo "green" "> http.getStatus - You get the Status as string OK|Redirect|Error"
http.getStatus
echo
sleep 1

color.echo "green" "> http.getResponseHeader - print Http response header"
http.getResponseHeader
echo
sleep 1

color.echo "green" "> http.getResponseData - get data from curl"
http.getResponseData
echo
sleep 1

color.echo "green" "> http.getResponse - get response body"
http.getResponse
echo
sleep 1

echo
echo "That's all for the moment. Bye."

# ----------------------------------------------------------------------