by Tyler Blevins
To start the API, run up.sh (bash up.sh) from the terminal. Once the docker containers are up and running, you may make requests.
Additionally, unit tests can be evaluated by running test.sh (bash test.sh)from the terminal if the docker containers are up. Stop the application
by hitting ctrl+C in the terminal where up.sh was run.
To demonstrate my API knowledge, I wanted to develop a sample REST API that allows an API consumer to create a household and read it back later. The API required the following elements.
- Ability to create a new Household resource.
- Ability to retrieve Household resources by unique ID.
- Return an HTTP 400 Bad Request response if API users try to create a household with an invalid format.
- Ability to retrieve a Household's percentage of Federal Poverty Level
- Ability to update an existing Household.
- Ability to delete an existing Household.
Endpoint for household creation.
METHOD - PUT
EXAMPLE PAYLOAD - '{"members": [{"gender": "female", "age": 22}, {"gender": "male", "age": 25}], "income": 16460.0}'
EXAMPLE REQUEST - curl -i -H "Content-Type: application/json" -X POST -d '{"income": 80000, "members": [{"age": 27, "gender": "female"}, {"age": 29, "gender": "male"}]}' http://0.0.0.0:5000/household/
EXAMPLE RESPONSE -
Content-Type: application/json
Content-Length: 61
Server: Werkzeug/0.14.1 Python/2.7.14
Date: Sun, 18 Mar 2018 23:27:18 GMT
{
"household_id": "e654dd7e-2b03-11e8-9e68-0242ac110003"
}
Endpoint for retrieving, updating, or deleting a household resource
METHOD - GET
EXAMPLE REQUEST curl http://0.0.0.0:5000/household/e654dd7e-2b03-11e8-9e68-0242ac110003
EXAMPLE RESPONSE - \
"income": 80000,
"members": [
{
"age": 27,
"gender": "female"
},
{
"age": 29,
"gender": "male"
}
]
}
METHOD - PUT
EXAMPLE PAYLOAD - '{"members": [{"gender": "female", "age": 22}, {"gender": "male", "age": 25}], "income": 16460.0}'
EXAMPLE REQUEST - curl -i -H "Content-Type: application/json" -X PUT -d '{"income": 140000, "members": [{"age": 25, "gender": "female"}, {"age": 25, "gender": "male"}]}' http://0.0.0.0:5000/household/
"income": 140000,
"members": [
{
"age": 25,
"gender": "female"
},
{
"age": 25,
"gender": "male"
}
]
}
METHOD - DELETE
EXAMPLE REQUEST - curl -i -X DELETE http://0.0.0.0:5000/household/e654dd7e-2b03-11e8-9e68-0242ac110003
EXAMPLE RESPONSE -
Content-Type: text/html; charset=utf-8
Content-Length: 44
Server: Werkzeug/0.14.1 Python/2.7.14
Date: Mon, 19 Mar 2018 01:26:48 GMT
8200d0ce-2b14-11e8-8363-0242ac110003 deleted
Endpoint for retrieving the percentage of Federal Poverty Level for a household
METHOD - GET
EXAMPLE REQUEST - http://0.0.0.0:5000/fpl/e654dd7e-2b03-11e8-9e68-0242ac110003
EXAMPLE RESPONSE -
8.50546780072904
Unit tests can be found in /test_api.py.
- test_api_connection()
- test_sample_household()
- test_create_household()
- test_bad_data()
- test_get_household()
- test_fpl_calc()
- test_invalid_get_household()
- test_invalid_fpl_household()
- test_update_household()
- test_delete_household()
Unit tests are evaluated upon running bash test.sh from the terminal after bash up.sh. This script docker exec's into
the api-sample_web_1 container and runs pytest -v from the root directory.
Checks for 200 OK response from host.
Checks that /sample-household/ returns the sample household data.
Checks that household creation returns 200 OK response.
Checks that an HTTP 400 Bad Request response is returned if API users try to create a household with an invalid format.
Checks that a household object can be retrieved after household creation.
Checks that a percentage of Federal Poverty Level can be retrieved after household creation.
Checks that an HTTP 400 Bad Request response is returned if an unknown household id is supplied when requesting household resource.
Checks that an HTTP 400 Bad Request response is returned if an unknown household id is supplied when requesting percentage of Federal Poverty Limit.
Checks that an existing household can be successfully updated.
Checks that an existing household can be successfully deleted.