Riddle Server #
In this lab we are going to learn how the riddle server is made using Banjo.
πOpen the Banjo documentation: https://cs.fablearn.org/docs/banjo/index.html
[0] Set Up #
You are each able to run a locally hosted riddle server on your laptop using Banjo.
π Github Repo: github.com/Making-with-Code/lab_riddle_server
π» Enter the Poetry shellpoetry shell
poetry install
pip3 install django-banjo
[1] Local Riddle Server #
You are each able to run a locally hosted riddle server on your laptop using Banjo.
[Starting the Server] #
π» Now, let's start your local server.banjo
No changes detected in apps 'app', 'banjo'
Operations to perform:
Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
No changes detected in apps 'banjo', 'app'
Operations to perform:
Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
Performing system checks...
System check identified no issues (0 silenced).
September 16, 2022 - 02:40:21
Django version 4.1.1, using settings 'banjo.settings'
Starting development server at http://127.0.0.1:5000/
Quit the server with CONTROL-C.
[Accessing the Server] #
π You can now visit this server in your web browser, just as you did with the riddler server hosted on the internet: http://127.0.0.1:5000/riddles/all
In order to send requests to the other endpoints, you will need always have 2 Terminal windows open.
- 1 window will run the server
- 1 window will send
HTTP
requests
π» Open another Terminal window.
You can either open a new tab, command+t
, or open a new window with command+n
.
http get http://127.0.0.1:5000/riddles/all
[16/Sep/2022 02:44:20] "GET /riddles/all HTTP/1.1" 200 1069
Your version of the riddle server only has the 2 endpoints:
/riddles/all
/riddles/guess
β CHECKPOINT:
π» Explore both endpoints via the Terminal and be sure to successfully:
- view all riddles without the answers
- guess a riddle
[2] What is Banjo? #
This server is written using Banjo, a wrapper for Django. It allows users to quickly create models with a persistant database and API.
Banjo apps must have an app
folder. Within the app folder must be two files: models.py
, views.py
. The database.sqlite
file is created when the server is first started. It is stored at the same level as app
. Here is an example file structure:
lab_riddle_server
|
βββapp
| βββmodels.py
| βββviews.py
βββdatabase.sqlite
models.py
- A model is essentially an abstracted class. Just as a class has properties, a model has fields. When defining a model’s fields, you must specify the data type.views.py
- The views are where you define the API functionality. Here is where you decide what theendpoints
are and the type ofHTTP
request it will require.
In this lab, we are going to be primarily focused on the views.py
file.
[3] Writing Routes #
In this lab, you will build out the full functionality of the Riddle server. Currently, your file only has riddles/all
and riddle/guess
.
It is up to you to add the following endpoints:
riddles/one
riddles/new
riddle/random
π» Start by opening up the primary folder:
app
atom app
π»
Open the views.py
file. Here is where you will write the additional endpoints.
[riddles/one] #
π»
Write the riddles/one
endpoint.
- http:
get
- payload:
id
- return: a single
Riddle
with thequestion
,guesses
, andcorrect
properties
β CHECKPOINT:
π»
Test the
riddles/one
endpoint in a separate Terminal window from the server..http get http://127.0.0.1:5000/riddles/one id=0
[riddles/new] #
π»
Write the riddles/new
endpoint.
- http:
post
- payload:
question
andanswer
- return: a single
Riddle
with thequestion
,guesses
, andcorrect
properties
β CHECKPOINT:
π»
Test the
riddles/new
endpoint in a separate Terminal window from the server..http post http://127.0.0.1:5000/riddles/new question="It goes up and down the stairs without moving." answer="A carpet"
[riddles/random] #
π»
Write the riddles/random
endpoint.
- http:
get
- return: a single
Riddle
with thequestion
,correct
, andguess
properties
β CHECKPOINT:
π»
Test the
riddles/random
endpoint in a separate Terminal window from the server..http get http://127.0.0.1:5000/riddles/random
[4] Deliverables #
β‘β¨
Once you’ve successfully completed the worksheet be sure to fill out this Google form.
[5] Extension #
Currently, there’s not way to see the answer unless you correctly guess the riddle.
π» Write an endpoint that has the ability to view the solution of a given Riddle.
Hint: a new method in the
Riddle
class may be useful
It should return JSON
that looks something like:
{
"riddle": [
{
"correct": 0,
"guesses": 3,
"id": 1,
"question": "It goes up and down the stairs without moving.",
"answer": "A carpet"
}
]
}
π» Write an endpoint that has the ability to change the question or answer or a riddle.
Consider, what kind of HTTP request would be best for this?