- Build an API to handle GET requests.
- Application Programming Interface (API): a software application that allows two or more software applications to communicate with one another. Can be standalone or incorporated into a larger product.
- HTTP Request Method: assets of HTTP requests that tell the server which actions the client is attempting to perform on the located resource.
GET: the most common HTTP request method. Signifies that the client is attempting to view the located resource.
This is a test-driven lab.
Run pipenv install to install the dependencies and pipenv shell to enter
your virtual environment before running your code.
$ pipenv install
$ pipenv shellChange into the server directory and configure the FLASK_APP and
FLASK_RUN_PORT environment variables:
$ cd server
$ export FLASK_APP=app.py
$ export FLASK_RUN_PORT=5555In this application, we'll be working on a JSON API to get a list of bakeries and their baked goods. We have two models, bakeries and baked goods, that have a one-to-many relationship. Here's what the ERD for these tables looks like:
The commands flask db init and flask db migrate have already been run. Run
the following command to initialize the database from the existing migration
script:
$ flask db upgrade headRun the following command to seed the table with sample data:
$ python seed.pyYou can run the app and explore your API in the browser by using Flask command:
$ flask runEdit app.py to handle the following GET requests:
GET /bakeries: returns a list of JSON objects for all bakeries in the database.GET /bakeries/<int:id>: returns a single bakery as JSON with its baked goods nested in a list. Use theidfrom the URL to look up the correct bakery.GET /baked_goods/by_price: returns a list of baked goods as JSON, sorted by price in descending order. (HINT: how can you use SQLAlchemy to sort the baked goods in descending order?)GET /baked_goods/most_expensive: returns the single most expensive baked good as JSON. (HINT: how can you use SQLAlchemy to sort the baked goods in descending order and limit the number of results?)
Once all of your tests are passing, commit and push your work using git to
submit.
[
{
"baked_goods": [
{
"bakery_id": 1,
"created_at": "2023-11-03 16:11:18",
"id": 1,
"name": "Chocolate dipped donut",
"price": 2.75,
"updated_at": null
},
{
"bakery_id": 1,
"created_at": "2023-11-03 16:11:18",
"id": 2,
"name": "Apple-spice filled donut",
"price": 3.5,
"updated_at": null
}
],
"created_at": "2023-11-03 16:11:18",
"id": 1,
"name": "Delightful donuts",
"updated_at": null
},
{
"baked_goods": [
{
"bakery_id": 2,
"created_at": "2023-11-03 16:11:18",
"id": 3,
"name": "Glazed honey cruller",
"price": 3.25,
"updated_at": null
},
{
"bakery_id": 2,
"created_at": "2023-11-03 16:11:18",
"id": 4,
"name": "Chocolate cruller",
"price": 100,
"updated_at": "2023-11-03 16:11:18"
}
],
"created_at": "2023-11-03 16:11:18",
"id": 2,
"name": "Incredible crullers",
"updated_at": null
}
]{
"baked_goods": [
{
"bakery_id": 1,
"created_at": "2023-11-03 16:11:18",
"id": 1,
"name": "Chocolate dipped donut",
"price": 2.75,
"updated_at": null
},
{
"bakery_id": 1,
"created_at": "2023-11-03 16:11:18",
"id": 2,
"name": "Apple-spice filled donut",
"price": 3.5,
"updated_at": null
}
],
"created_at": "2023-11-03 16:11:18",
"id": 1,
"name": "Delightful donuts",
"updated_at": null
}[
{
"bakery": {
"created_at": "2023-11-03 16:31:32",
"id": 1,
"name": "Delightful donuts",
"updated_at": null
},
"bakery_id": 1,
"created_at": "2023-11-03 16:31:32",
"id": 2,
"name": "Apple-spice filled donut",
"price": 3.5,
"updated_at": null
},
{
"bakery": {
"created_at": "2023-11-03 16:31:32",
"id": 2,
"name": "Incredible crullers",
"updated_at": null
},
"bakery_id": 2,
"created_at": "2023-11-03 16:31:32",
"id": 4,
"name": "Chocolate cruller",
"price": 3.4,
"updated_at": null
},
{
"bakery": {
"created_at": "2023-11-03 16:31:32",
"id": 2,
"name": "Incredible crullers",
"updated_at": null
},
"bakery_id": 2,
"created_at": "2023-11-03 16:31:32",
"id": 3,
"name": "Glazed honey cruller",
"price": 3.25,
"updated_at": null
},
{
"bakery": {
"created_at": "2023-11-03 16:31:32",
"id": 1,
"name": "Delightful donuts",
"updated_at": null
},
"bakery_id": 1,
"created_at": "2023-11-03 16:31:32",
"id": 1,
"name": "Chocolate dipped donut",
"price": 2.75,
"updated_at": null
}
]{
"bakery": {
"created_at": "2023-11-03 16:16:21",
"id": 1,
"name": "Delightful donuts",
"updated_at": null
},
"bakery_id": 1,
"created_at": "2023-11-03 16:16:21",
"id": 2,
"name": "Apple-spice filled donut",
"price": 3.5,
"updated_at": null
}