|
| 1 | +# Library of bash-functions to access https://getpantry.cloud/ API |
| 2 | +# |
| 3 | +# To avail the functions listed herein, source this script as |
| 4 | +# |
| 5 | +# source ./pantry-client.bash "Your_Pantry_ID_here" |
| 6 | +# |
| 7 | +# Tip: Put the above command inside ~/.bashrc to avoid sourcing manually everytime |
| 8 | +# |
| 9 | +# Author: Somajit Dey <dey.somajit@gmail.com> 2021 |
| 10 | + |
| 11 | +export PANTRY_ID="${1:-"${PANTRY_ID}"}" |
| 12 | + |
| 13 | +[[ -z "${PANTRY_ID}" ]] && \ |
| 14 | +echo 'Warning: PANTRY_ID not set |
| 15 | +Execute command: export PANTRY_ID="Your_Pantry_ID_here"' |
| 16 | + |
| 17 | +pantry_curl(){ |
| 18 | + # Common cURL parameters to avoid code duplication |
| 19 | + # Usage: pantry_curl POST|GET|PUT|DEL <basket name> |
| 20 | + # |
| 21 | + # Note: PANTRY_BASE_URL is available globally for later use, if any, after a single |
| 22 | + # invocation of this function |
| 23 | + |
| 24 | + [[ -n "${PANTRY_ID}" ]] || \ |
| 25 | + { echo "Please set the environment variable PANTRY_ID with your Pantry ID" && return 1;} |
| 26 | + |
| 27 | + declare -xg PANTRY_BASE_URL="https://getpantry.cloud/apiv1/pantry/${PANTRY_ID}" |
| 28 | + |
| 29 | + local method="${1}" |
| 30 | + local basket="${2}" |
| 31 | + shift;shift |
| 32 | + local endpoint_url="${PANTRY_BASE_URL}/basket/${basket}" |
| 33 | + curl -fsSL -H 'Content-Type: application/json' -X "${method}" "${endpoint_url}" $@ |
| 34 | + |
| 35 | +}; export -f pantry_curl |
| 36 | + |
| 37 | +pantry_create(){ |
| 38 | + # Create/Replace basket passed as parameter |
| 39 | + |
| 40 | + local basket="${1}" |
| 41 | + pantry_curl POST "${basket}" |
| 42 | + |
| 43 | +}; export -f pantry_create |
| 44 | + |
| 45 | +pantry_update(){ |
| 46 | + # Update basket passed as parameter. Read payload json from stdin. |
| 47 | + |
| 48 | + local basket="${1}" |
| 49 | + pantry_curl PUT "${basket}" -d @- |
| 50 | + |
| 51 | +}; export -f pantry_update |
| 52 | + |
| 53 | +pantry_retrieve(){ |
| 54 | + # Return full contents of basket passed as parameter. |
| 55 | + |
| 56 | + local basket="${1}" |
| 57 | + pantry_curl GET "${basket}" |
| 58 | + |
| 59 | +}; export -f pantry_retrieve |
| 60 | + |
| 61 | +pantry_delete(){ |
| 62 | + # Delete basket passed as parameter. |
| 63 | + |
| 64 | + local basket="${1}" |
| 65 | + pantry_curl DELETE "${basket}" |
| 66 | + |
| 67 | +}; export -f pantry_delete |
0 commit comments