#!/bin/bash SED=/usr/bin/sed CURL="/usr/bin/curl -s" DATE=/bin/date TOKEN=${TOKEN:?"TOKEN environment variable expected"} NYLAS_API="https://$TOKEN:@api.nylas.com" setCursor() { if [ -z $AT ]; then CURSOR=$($CURL -X POST "$NYLAS_API/delta/latest_cursor" |$SED -n 's/\s*"cursor": "\(.*\)"/\1/p'|tr -d '[:space:]') else TIME_IN_S=$($DATE -v "$AT" '+%s') CURSOR=$($CURL --data "{\"start\":$TIME_IN_S}" "$NYLAS_API/delta/generate_cursor" |$SED -n 's/\s*"cursor": "\(.*\)"/\1/p'|tr -d '[:space:]') fi } main() { command=$1 shift case "$command" in stream) if [ -z "$CURSOR" ]; then setCursor fi $CURL -G -N $NYLAS_API/delta/streaming --data-urlencode "cursor=$CURSOR" $* ;; cursor) setCursor $* if [ -z "$TIME_IN_S" ]; then echo "Latest Cursor> $CURSOR" else echo "Cursor at $(date -r $TIME_IN_S)> $CURSOR" fi ;; download) $CURL -O -J "$NYLAS_API/files/$*/download" ;; search) QUERY=$1 shift $CURL -G -N $NYLAS_API/threads/search/streaming --data-urlencode "q=$QUERY" ;; *) case "$command" in get|post|delete|head|put) METHOD=`echo "$command" | tr '[a-z]' '[A-Z]'` ENDPOINT=$1 shift $CURL -X $METHOD $NYLAS_API$ENDPOINT $* ;; *) cat<<-EOL ------------------------------------------------------------------- NOTE: For more info about the TIME_DELTA, check 'man date at -v' ------------------------------------------------------------------- Usage: TOKEN=token AT=TIME_DETLA $0 stream - to stream deltas # will stream deltas TOKEN=token $0 [get|post|delete|head|put] /ENDPOINT [ARGS] # will hit Nylas TOKEN=token $0 cursor # will return a Nylas cursor TOKEN=token $0 search "from:foo@bar.com" # will search for threads TOKEN=token $0 download FILE_ID # will download a file locally Examples: TOKEN=token $0 AT=-1H stream --data-urlencode "include_types=file" |jq TOKEN=token $0 cursor -3M TOKEN=token $0 get /messages/:id TOKEN=token $0 get /files/:id TOKEN=token $0 put /threads/:id -d '{"folder_id":FOLDER_ID}' EOL ;; esac ;; esac } [[ "$0" == "$BASH_SOURCE" ]] && main $@