An example Phoenix project that provides an unified API to call different humorous APIs.
The humorous APIs are:
See also Phoenix installation.
Install Hex package manager:
$ mix local.hex
Install Phoenix application generator:
$ mix archive.install hex phx_new
Generator help:
$ mix help phx.new
Generate the application:
$ mix phx.new jokes --install --no-assets --no-ecto --no-gettext --no-html --no-mailer
Start the application:
$ cd jokes
$ mix phx.server
At this point there is nothing but the Phoenix dashboard: http://localhost:4000/dev/dashboard/
$ mix test
Supported query parameters:
- Mandatory:
family
- valid values:chucknorris
,ipsum
,simpsons
- Optional:
amount
- integer greater than zero, defaults to 1.
Examples:
$ curl --verbose 'http://localhost:4000/api/jokes/v1?family=simpsons&amount=2'
Note the version in the path, i.e. jokes/v1
or jokes/v2
.
HTTP status code 400 Bad Request with error details in payload:
{"message":"error details"}
The basic Phoenix skeleton where:
- the query parameters are validated
- the query is routed to the correct handler
- returns hard-coded response, no actual api calls yet
- all code in the controller
So no jokes returned yet!
Install additional dependencies:
$ mix deps.get
Compared to v1:
- Application code has been separated from the Phoenix code.
- Call the actual APIs.
- Customised responses for 404 and 500.
amount
can also be Roman numeral.