Skip to content

Instantly share code, notes, and snippets.

@gregory
Last active July 3, 2018 21:24
Show Gist options
  • Save gregory/3c98a10cc009ae9f333cd0018523afc5 to your computer and use it in GitHub Desktop.
Save gregory/3c98a10cc009ae9f333cd0018523afc5 to your computer and use it in GitHub Desktop.

Just a Handy Nylas client based on curl

curl https://gist.githubusercontent.com/gregory/3c98a10cc009ae9f333cd0018523afc5/raw/40917888cd1e903bbd9ab0cbe33d6863728bcf4b/nylas.sh --output /usr/local/bin/nylas
chmod +x /usr/local/bin/nylas
/usr/local/bin/nylas
#!/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"
;;
upload)
$CURL "$NYLAS_API/files -F filename=$1 -F image=@$2"
;;
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 AT=TIME_DELTA $0 cursor # will return a Nylas cursor
TOKEN=token $0 [get|post|delete|head|put] /ENDPOINT [ARGS] # will hit Nylas
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 AT=-1H $0 stream --data-urlencode "include_types=file" |jq
TOKEN=token AT=-3M $0 cursor
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 $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment