App-Adenosine
view release on metacpan or search on metacpan
over. You can save them in a file and adenosine will pass them to curl
for you. Say this is a frequent pattern for you:
$ adenosine localhost:8080
$ GET /Blah -H "Accept: application/json"
$ GET /Other -H "Accept: application/json"
...
$ POST /Something -H "Content-Type: text/plain" -u user:pass
$ POST /SomethingElse -H "Content-Type: text/plain" -u user:pass
...
It's annoying to add the -H and -u options to curl all the time. So
create a file ~/.resty/localhost:8080, like this:
~/.resty/localhost:8080
GET -H "Accept: application/json"
POST -H "Content-Type: text/plain" -u user:pass
Then any GET or POST requests to localhost:8080 will have the specified
options prepended to the curl command line arguments, saving you from
having to type them out each time, like this:
$ GET /Blah
$ GET /Other
...
$ POST /Something
$ POST /SomethingElse
...
Sweet! Much better.
Exit Status
Successful requests (HTTP respose with 2xx status) return zero.
Otherwise, the first digit of the response status is returned (i.e., 1
for 1xx, 3 for 3xx, 4 for 4xx, etc.) This is because the exit status is
an 8 bit integer---it can't be greater than 255. If you want the exact
status code you can always just pass the -v option to curl.
Using Adenosine In Shell Scripts !!!
Since adenosine creates the REST verb functions in the shell, when
using it from a script you must source it before you use any of the
functions. However, it's likely that you don't want it to be
overwriting the adenosine host history file, and you will almost always
want to set the URI base explicitly.
#!/usr/bin/env bash
# Load adenosine, don't write to the history file, and set the URI base
. /path/to/adenosine-exports -W 'https://myhost.com/data*.json'
# GET the JSON list of users, set each of their 'disabled' properties
# to 'false', and PUT the modified JSON back
GET /users | jsawk 'this.disabled = false' | PUT
Here the -W option was used when loading the script to prevent writing
to the history file and an initial URI base was set at the same time.
Then a JSON file was fetched, edited using jsawk
<http://github.com/micha/jsawk>, and re-uploaded to the server.
Application Configuration
Adenosine may be configured by placing a YAML document in
~/.adenosinerc.yml. More parts of adenosine will be configurable as
time goes on, but right now the only real configuration is for plugins.
Adenosine's plugin architecture (documented at "USING PLUGINS" in
App::Adenosine and "CREATING PLUGINS" in App::Adenosine) can be used to
color code headers, time the request, or more, if you choose to write
more plugins. Enabling a plugin is simple with the ~/.adenosinerc.yml
file. Here is how you would enable both
App::Adenosine::Plugin::Stopwatch and App::Adenosine::Plugin::Rainbow,
including a little bit of extra (non-required) configuration to
customize some colors for ::Rainbow.
plugins:
- ::Stopwatch
- ::Rainbow: {
request_method_color: cyan
}
The following would work if you didn't want to configure ::Rainbow
plugins:
- ::Stopwatch
- ::Rainbow
Another option allows the user to disable the XDG based directory
structure (typically ~/.config). Simply put the following in your
~/.adenosinerc.yml:
enable_xdg: 0
Working With JSON or XML Data
JSON REST web services require some special tools to make them
accessible and easily manipulated in the shell environment. The
following are a few scripts that make dealing with JSON data easier.
* Jsawk <http://github.com/micha/jsawk> can be used to process and
filter JSON data from and to adenosine, in a shell pipeline. This
takes care of parsing the input JSON correctly, rather than using
regexes and sed, awk, perl or the like, and prints the resulting
output in correct JSON format, as well.
GET /blogs.json |jsawk -n 'out(this.title)' # prints all the blog titles
* The included pp script will pretty-print JSON for you.
GET /blogs.json |pp # pretty-prints the JSON output from adenosine
* Another way to format JSON output:
$ echo '{"json":"obj"}' | python -mjson.tool
{
"json": "obj"
}
* The tidy tool can be used to format HTML/XML:
( run in 2.013 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )