Created
May 28, 2019 21:00
-
-
Save Jules-Baratoux/05edc1013e530fa8b92f3006a20b6ee3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -euo pipefail | |
MATTERMOST_URL="http://mattermost.qwant.loc" | |
MATTERMOST_API_URL="$MATTERMOST_URL/api/v4" | |
header_file=/tmp/header | |
body_file=/tmp/body | |
function request # ENDPOINT CURL_ARGUMENTS... | |
{ | |
eval local url=$(sed -E 's|:(\w+)|${\1}|g' <<< "${MATTERMOST_API_URL}${FUNCNAME[1]}") | |
( | |
set -x | |
curl --output "$body_file" \ | |
--dump-header "$header_file" \ | |
--location \ | |
--request "${FUNCNAME[2]}" \ | |
--show-error \ | |
--silent \ | |
"$@" "$url" | |
) | |
} | |
function POST | |
{ | |
"$@" | |
} | |
function GET | |
{ | |
"$@" | |
} | |
function body | |
{ | |
function json | |
{ | |
jq "${@:-.}" "$body_file" | |
} | |
function raw | |
{ | |
cat "$body_file" | |
} | |
"${@:-raw}" | |
} | |
function headers | |
{ | |
cat "$header_file" | |
} | |
function header | |
{ | |
headers | grep -Poi "(?<=$1: ).+" | |
} | |
function bearer | |
{ | |
local bearer_file=/tmp/bearer | |
function create | |
{ | |
cat > "$bearer_file" | |
} | |
function get | |
{ | |
cat "$bearer_file" | |
} | |
"${@:-get}" | |
} | |
function /users/login # JSON_DATA | |
{ | |
request --data "$@" | |
header Token | bearer create | |
} | |
function /users/me | |
{ | |
request -H "Authorization: Bearer $(bearer)" | |
body json | |
} | |
function /posts # JSON_DATA | |
{ | |
request -H "Authorization: Bearer $(bearer)" --data "$1" | |
body json | |
} | |
function /teams/name/:team_name/channels/name/:channel_name | |
{ | |
request -H "Authorization: Bearer $(bearer)" | |
body json | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Get a bearer token using user and password | |
# https://api.mattermost.com/#tag/authentication | |
POST /users/login ' | |
{ | |
"login_id": "j.baratoux", | |
"password": "secret" | |
}' | |
# Print information about the curent user | |
GET /users/me | |
# Get a channel by name and team name | |
team_name=qwant | |
channel_name=town-square | |
GET /teams/name/:team_name/channels/name/:channel_name | |
# Create a post | |
# https://api.mattermost.com/#tag/posts%2Fpaths%2F~1posts%2Fpost | |
POST /posts " | |
{ | |
\"channel_id\": \"$(body json -r .id)\", | |
\"message\": \"Posted on $(date)\" | |
}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment