Skip to content

Commit

Permalink
Merge pull request #66 from imRohan/develop
Browse files Browse the repository at this point in the history
Live Dashboards and Client Libraries
  • Loading branch information
Rohan Likhite authored May 12, 2021
2 parents 25cf38f + 78b1a7d commit 16d20af
Show file tree
Hide file tree
Showing 15 changed files with 881 additions and 187 deletions.
38 changes: 38 additions & 0 deletions client-libraries/Bash/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Bash-script to access [Pantry](https://getpantry.cloud/) [API](https://documenter.getpostman.com/view/3281832/SzmZeMLC)

Source the library first

```bash
. ./pantry-client.bash "Your_Pantry_ID_here"
```

Or, better yet, put the above command inside the file `~/.bashrc`. This would make Bash source the library automatically on startup.

### Usage:

- Create/replace basket

```bash
pantry_create <name of basket>
```

- Add contents to / update contents of basket

```bash
echo '{"newKey":"newVal","oldKey":"newerVal"}' | pantry_update <name of basket>
```

- Retrieve all contents of basket

```bash
pantry_retrieve <name of basket>
```

- Delete basket

```bash
pantry_delete <name of basket>
```



67 changes: 67 additions & 0 deletions client-libraries/Bash/pantry-client.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Library of bash-functions to access https://getpantry.cloud/ API
#
# To avail the functions listed herein, source this script as
#
# source ./pantry-client.bash "Your_Pantry_ID_here"
#
# Tip: Put the above command inside ~/.bashrc to avoid sourcing manually everytime
#
# Author: Somajit Dey <[email protected]> 2021

export PANTRY_ID="${1:-"${PANTRY_ID}"}"

[[ -z "${PANTRY_ID}" ]] && \
echo 'Warning: PANTRY_ID not set
Execute command: export PANTRY_ID="Your_Pantry_ID_here"'

pantry_curl(){
# Common cURL parameters to avoid code duplication
# Usage: pantry_curl POST|GET|PUT|DEL <basket name>
#
# Note: PANTRY_BASE_URL is available globally for later use, if any, after a single
# invocation of this function

[[ -n "${PANTRY_ID}" ]] || \
{ echo "Please set the environment variable PANTRY_ID with your Pantry ID" && return 1;}

declare -xg PANTRY_BASE_URL="https://getpantry.cloud/apiv1/pantry/${PANTRY_ID}"

local method="${1}"
local basket="${2}"
shift;shift
local endpoint_url="${PANTRY_BASE_URL}/basket/${basket}"
curl -fsSL -H 'Content-Type: application/json' -X "${method}" "${endpoint_url}" $@

}; export -f pantry_curl

pantry_create(){
# Create/Replace basket passed as parameter

local basket="${1}"
pantry_curl POST "${basket}"

}; export -f pantry_create

pantry_update(){
# Update basket passed as parameter. Read payload json from stdin.

local basket="${1}"
pantry_curl PUT "${basket}" -d @-

}; export -f pantry_update

pantry_retrieve(){
# Return full contents of basket passed as parameter.

local basket="${1}"
pantry_curl GET "${basket}"

}; export -f pantry_retrieve

pantry_delete(){
# Delete basket passed as parameter.

local basket="${1}"
pantry_curl DELETE "${basket}"

}; export -f pantry_delete
Loading

0 comments on commit 16d20af

Please sign in to comment.