Making with Code

Riddle Database #

In this lab we are going to delve into databases and a new software, banjo.


[0] Experience the ISF Riddle Server #

The last time we saw riddles, they existed only within each of your computers. You had to manually add new ones and there was no way to keep track of how many people guessed them.

We are now going to look at Riddles that are hosted on the internet!

๐Ÿ’ป Visit http://sycs.student.isf.edu.hk/riddle/all to view riddle server.

Not very easy to read, right? You can install the JSON Formatter Chrome extension to view better formatted JSON.*

๐Ÿ’ป Now try making GET request to http://sycs.student.isf.edu.hk/riddle/all receive the same information using : httpie.io/app

๐Ÿ’ป Do you know the answer? Try sending a POST request to make a guess. This request to a different url endpoint: /guess

  1. Add a new request
  2. Change GET to POST
  3. Paste in the URL: http://sycs.student.isf.edu.hk/riddle/guess
  4. Select Body
  5. At the bottom, change None to Form
  6. Add the payloads: id and guess

It is common for POST requests to send a payload with the request. In this case, the payloads are:

  • id specifying which riddle we are guessing
  • guess specifying the guess We are also sending this request to a different url endpoint: /guess

Explore the Endpoints #

๐Ÿ”— Server APIs often rely on different URL endpoints to the base url to determine what the API should do.

  • The base url to this server is: http://sycs.student.isf.edu.hk/riddle

Here is a cheatsheet of the Riddle endpoints, what parameters they take in their payload, and what they do:

MethodURLRequired PayloadAction
GET/allReturns a list of all the riddles, without answers.
GET/oneidReturns the riddle if it exists
POST/newquestion, answerCreates a new riddle (with an automatically-assigned id). Returns the riddle.
POST/guessid, guessChecks whether the guess is correct. In the response, correct is True or False.
GET/difficultyidReturns the riddle if it exists with its difficulty score. (Otherwise, it returns an error with status code 404.)
โœ… CHECKPOINT:

๐Ÿ’ป Explore each endpoint in the httpie tool, and be sure to successfully:

  • view all riddles without the answers
  • view a single riddle
  • add a new riddle
  • guess a riddle
  • try to break the riddle server, what happens when you provide incorrect parameters?

๐Ÿ“– When using the the httpie tool,

  • for a GET request, put the payload in the Params
  • for a POST request, put the payload in the Body > Form


[1] Set Up #

๐Ÿ’ป Let's clone the repository in your unit03_networking folder. Be sure to change yourgithubusername to your actual Github username.

cd ~/desktop/making_with_code/unit03_networking
git clone https://github.com/the-isf-academy/lab_banjo_yourgithubusername
cd lab_banjo_yourgithubusername
๐Ÿ’ป Get the other necessary packages:
poetry install
๐Ÿ’ป Enter the Poetry shell
poetry shell
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D

๐Ÿ“„ This repository has two main files inside a the /app directory:

  • /app
    • models.py: This defines the model
    • views.py: This defines the routes

[2] Databases #

Riddle Model #

Now that you’ve experienced the riddle server, let’s delve into how it’s made.

The Riddle objects looks almost identical to how it looked in the Riddler lab. The main difference, is we must define the datatype of each property. This tells the database, what type of information each property will store.

class Riddle(Model):
    question = StringField()
    answer = StringField()
    guesses = IntegerField()
    correct = IntegerField()

    MIN_FUZZ_RATIO = 80

Banjo Shell #

In this unit, we will be using Banjo and wrapper over Django. Django is a popular web framework that is used to quickly create web apps. We will be using Django in the spring, so for now we will Banjo to introduce the software.

๐Ÿ’ป Let's enter the Banjo shell to interact with the riddles.
banjo --shell
No changes detected in apps 'app', 'banjo'
Operations to perform:
  Apply all migrations: admin, app, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
# Shell Plus Model Imports
from app.models import Riddle
Python 3.10.6 (v3.10.6:9c7b4bd164, Aug  1 2022, 17:13:48) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
๐Ÿ’ป Let's start by just viewing all the riddles in the shell:
Riddle.objects.all()
<QuerySet [<Riddle 1: I'm tall when I'm young, and I'm short when I'm old. What am I? (11/33)>, <Riddle 2: How many months of the year have 28 days? (5/10)>, <Riddle 3: I can fly but have no wings. I can cry but I have no eyes. Wherever I go, darkness follows me. What am I? (0/2)>, <Riddle 4: What has to be broken before you can use it? (3/7)>, <Riddle 5: The more you take, the more you leave behind. What are they? (3/10)>, <Riddle 6: What is so delicate that saying its name breaks it? (5/6)>, <Riddle 7: I am always in front and never behind. What am I? (2/2)>]>
๐Ÿ’ป Now, let's add a new riddle:
new_riddle = Riddle(question='If you feed it, it lives; If you water it, it dies.', answer='Fire')
new_riddle.save()

Note how you must save() the riddle to ensure it gets saved in the database.

โœ… CHECKPOINT:

Before moving on, make sure the Riddle was properly saved:

  • view all the Riddle objects
  • refresh the database in the DB Browser

๐Ÿ’ป Now, let's try to guess the riddle we just added:
>>> new_riddle.check_guess('fire')
True
โœ… CHECKPOINT:

Before moving on, make sure the Riddle properties, guesses, and correct, were properly updated:

  • view all the Riddle objects
  • refresh the database in the DB Browser


Database Querying #

Now that you’ve experienced adding and updating the database, we’re going to explore how to search, or query, the database.

๐Ÿ“– Open the Banjo documentation: cs.fablearn.org/docs/banjo/index.html

If you find any typos, please let a teacher know - we can fix it!

โœ๏ธ Fill out the worksheet and refence the Banjo documentation to properly query the database using the Banjo shell.

You will use the Banjo/Djanog syntax throughout the whole year! Please be sure to experiment and ask any/all questions.


[3] Deliverables #

โšกโœจ

Once you’ve successfully completed the worksheet be sure to fill out this Google form.


[4] Extension #

Next lab we will explore how to write the Riddle server.

๐Ÿ’ป Feel free to open up the code and start to understand how it works! Can you add a new route for just one riddle?

code .