This is a web service that exposes an API for computing a horoscope.
The service exposes HTTP routes for creating a new horoscope process, updating a horoscope process with an answer, and getting the current state of a horoscope process.
~/code/shipworthy/hello $ mix phx.routes
api_path POST /api/horoscopes HelloWeb.ApiController :create
api_path GET /api/horoscopes/:horoscope_id HelloWeb.ApiController :get
api_path POST /api/horoscopes/:horoscope_id/:question_id HelloWeb.ApiController :record_answer
You can run a basic test (against service running locally, at http://localhost:4000) by executing make run-quick-api-test
.
Here is a step-by-step example:
- Create a new horoscope process, get its ID:
$ export horoscope_id=$(curl -X POST http://localhost:4000/api/horoscopes | jq -r -M .horoscope_id)
- Take a peek at the state of the new horoscope process:
$ curl -s -X GET http://localhost:4000/api/horoscopes/${horoscope_id} | jq .
{
"horoscope": null,
"horoscope_id": "v3bs3nawjn",
"next_question_id": "first_name",
"status": "ready"
}
- Update the horoscope process with the answer to the question
"first_name"
:
$ curl -s -X POST -H "Content-Type: application/json" http://localhost:4000/api/horoscopes/${horoscope_id}/first_name --data '{"answer":"Pickles"}' | jq .
- Update horoscope with the answer to the question
"birth_month"
:
$ curl -s -X POST -H "Content-Type: application/json" http://localhost:4000/api/horoscopes/${horoscope_id}/birth_month --data '{"answer":4}' | jq .
- Update horoscope with the answer to the question
"birth_day"
:
$ curl -s -X POST -H "Content-Type: application/json" http://localhost:4000/api/horoscopes/${horoscope_id}/birth_day --data '{"answer":26}' | jq .
- Now that all the questions have been answered, let horoscope process compute the actual horoscope (sleep and/or polling):
$ sleep 1
- Get the horoscope:
$ curl -s -X GET http://localhost:4000/api/horoscopes/${horoscope_id} | jq .
{
"horoscope": "Pickles! You are a righteous taurus! Now is the perfect week to smash the racist patriarchy. PS Tauruses are awesome.",
"horoscope_id": "v3bs3nawjn",
"next_question_id": null,
"status": "ready"
}