Are you working with Fatwire/Sites REST API, commonly named WEM api? We are currently working with it a lot, building AgileSitesNG.
The API is pretty complicated so we need a lot of processing to locate the informations we use. Furthermore, a common annoyance is getting an authentication token for each request. You can generate a token manually, with a couple of http call, but in the end working with it became awkward and time consuming.
So I started to dream a command line interface like “wem” where I could just select the call I wanto to get the whole json output well formatted. For example, I wish I could do this:
./wem sites
{ "schemaLocation": "http://www.fatwire.com/schema/rest/1.0 http://localhost:8181/cs/schema/rest-api.xsd",
"total": 2,
"startindex": 0,
"count": 2,
"site": [
{
"href": "http://localhost:8080/cs/REST/sites/AgileSitesNG",
"id": 1000000000000,
"name": “AgileSitesNG",
"description": “AgileSites NG"
},
{
"href": "http://localhost:8181/cs/REST/sites/AdminSite",
"id": 1253041378564,
"name": "AdminSite",
"description": "AdminSite"
}
]
}
Even better, when I have such a complicated output, Iwould be able to filter part of the content. For example in an ideal world so I could get the list of the users with its roles just typing:
./wem sites/AdminSite ".siteUsers.user[]|{name,roles}"
{
"name": "fwadmin",
"roles": [
"AdvancedUser",
"WorkflowAdmin",
"SiteAdmin",
"SitesUser",
"GeneralAdmin"
]
}
Actually the samples below are not a dream, they are the actual what you have seen before is the real output of the following script (for Linux and OS X).
#!/bin/bash CALL=${1:-sites}
HOST=localhost:8080
USER=fwadmin
PASS=xceladmin
REQ=$(curl -XPOST -sD - -d "username=$USER&password=$PASS" http://$HOST/cas/v1/tickets -o /dev/null | awk '/Location:/ { print $2 }')
TOK=$(curl -s -XPOST -d service=\* $REQ)
curl -s -H "Accept: application/json" "http://$HOST/cs/REST/$CALL?multiticket=$TOK" | jq "${2:-.}"
You can put the script in your local directory or copy the script in a /usr/local/bin/wem file, then execute a chmod +x. Then, most important ensure you have in the paths the commands curl and jq.
While curl is common and available everywhere, jq is less common but you can download it from here http://stedolan.github.io/jq/download/. If you are in OSX and you have homebrew installed you can just type “brew install jq”.
Actually the scripts works fine also on Windows provided you have installed a bash shell, that is provided in counless packages (cygwin, cmder, msysgit etc etc), curl and jq
Note the script must be edited to point to your current installation of Sites. If you have a Jump Start running on localhost with default users and passwords, then you are fine. If it is not the case, the script must be adapted (changing the HOST, USER, PASS variables of course).
The script takes 2 parameters. The first parameter is the Sites API you want to call, as listed here: (without the “/“ prefix). Some examples:
./wem sites
./wem types
./wem types/CSElement
./wem sites/AdminSite
The output is pretty verbose, so it helps using the “jq” tool syntax that is pretty handy.
The second parameter is indeed the a “jq” selector. While you can read the complete manual, a few examples are:
Selecting only site information:
./wem sites ".site[]"
{
"href": "http://localhost:8080/cs/REST/sites/AgileSites",
"id": 1000000000000,
"name": "AgileSites",
"description": "AgileSites NG"
}
{
"href": "http://localhost:8080/cs/REST/sites/AdminSite",
"id": 1253041378564,
"name": "AdminSite",
"description": "AdminSite"
}
Selecting only attribute values:
./wem sites “.sites[]|.id,.href"
"http://localhost:8080/cs/REST/sites/AgileSites"
1000000000000
"http://localhost:8080/cs/REST/sites/AdminSite”
1253041378564
Rewrap the selected information in a json map:
./wem '.sites[]|{"id":.id,”href:":.href}'
{
"id": 1000000000000,
"href": "http://localhost:8080/cs/REST/sites/AgileSites"
}
{
"id": 1253041378564,
"href": "http://localhost:8080/cs/REST/sites/AdminSite"
}
Those simple examples should give an idea of the power of the tool. Enjoy.