#!/bin/bash

echo "Using org and environment configured in /setup/setenv.sh"
echo "To setup API products developers and apps, be sure to run scripts under ./setup/provisioning"
echo "Reading your account settings from /setup/setenv.sh"


source ../../setup/setenv.sh

echo -e "Is the Proxy for the grant_type: oauth-client-credentials already deployed? ( [yes]/no ):"
read deploy_status

if [ "$deploy_status" != "no" ]; then
	deploy_status="yes"
fi

if [ "$deploy_status" != "yes" ]; then
	echo -e "Deploying oauth-client-credentials proxy. \n"
	/bin/bash ../oauth-client-credentials/deploy.sh
fi

/bin/bash ../oauth-client-credentials/invoke.sh

echo -e "\nAccessToken has been generated. Now we can access the protected resource with the Access Token generated by the previous API call"

echo -e "\nPass a valid AccessToken to verify it: [Hint: Copy and paste the access token from the JSON response above!] "
read access_token


echo -e "\nRequesting the protected resource using the access token\n"

echo -e "curl -k https://$org-$env.$api_domain/mocktarget/json -H 'Authorization: Bearer $access_token' \n"

curl -k https://$org-$env.$api_domain/mocktarget/json -H "Authorization: Bearer $access_token"


echo -e "\nRequesting the Protected Resource with Invalid AccessToken\n"

echo -e "curl -k https://$org-$env.$api_domain/mocktarget/json -H 'Authorization: Bearer XYZ_INVALID_ACCESTOKEN_XYZ' \n"

curl -k https://$org-$env.$api_domain/mocktarget/json -H "Authorization: Bearer XYZ_INVALID_ACCESTOKEN_XYZ"

echo -e "\n\n"

echo -e "Try it yourself! \n"
echo -e "Issue the request below multiple times in the course of a minute, to see the Quota enforced\n"

echo -e "curl -k https://$org-$env.$api_domain/mocktarget/json -H 'Authorization: Bearer $access_token' \n"


echo -e "\n\n"
