# Assignment Handout

## Assignment Scope

{% hint style="warning" %}
**Due Date:** Monday 3/23, 11:59pm EST
{% endhint %}

You will be building a [Reddit](https://www.reddit.com/)-style community forum where people can make **posts** and make **comments** on posts.&#x20;

### 1. Download Starter Code

We have provided the starting files you will need to complete this assignment for your convenience. This also simplifies your submission by just having to zip this same folder.

{% file src="/files/Dk4eJpGUN4uMJdRJyrrB" %}

### 2. Implement API Specification

We have shown you how to use the Flask framework to set up routes and respond to network requests. Your assignment is to implement a series of routes following the provided specification for responding to sample requests. This assignment has 7 routes for you to implement. If you are struggling with the assignment, we recommend referring to the demo to see a concrete example.

{% content-ref url="/pages/-Lu9\_ONXDoxcG-L-SC0v" %}
[API Specification](/chapters/routes/api.md)
{% endcontent-ref %}

#### 2a. Create a global variable for storing Post data

Just like we showed in the demo, we want you to create a datastore for all posts using a Python dictionary. Your keys should be integers corresponding to post ids and values should be post dictionary objects that mimic the data shown in the API Specification.

You may also pre-populate your dictionary with data for testing purposes.

#### 2b. Implement routes

Reference the demo for specifics on defining route syntax. We recommend approaching routes in the order they appear in the API specification. After defining your route and function to return a response, open up Postman and test your route. Testing with Postman simply involves creating a new request tab, entering your server's URL, choosing a request method, and clicking the `Send` button.&#x20;

#### 2c. Test with provided test script

{% file src="/files/YMqvsmXNCWerPwOTCfqq" %}

Use the provided test script to test your implementation. To run the test script, add the pa1\_test.py file to your src directory, and run:

<pre class="language-bash"><code class="lang-bash"><strong>>>> python3 pa1_test.py
</strong></code></pre>

Remember that we are checking that your code matches the API specification exactly! Some common errors that can cause the test cases to fail:<br>

* Ports not matching (5000)
* Routes misspelled
* Forgetting to jsonify return

### 3. Submit Assignment

#### 3a. Fill out your README.txt

A `README.txt` file is included in the starter code for you to fill out after completing the assignment. Also note that you will not receive extra credit for extra credit challenges you complete if you do not let us know for when we grade!

{% code title="README.txt" %}

```
Name: Jane Smith
NetID: js123

Challenges Attempted: <all the Tiers you completed here>
```

{% endcode %}

#### 3b. Make sure you have proper Python styling in your code

Common mistakes in styling are:

* NOT HAVING documentation/comments in each of your routes (a one-line comment is sufficient!)
* Naming variables with CamelCase instead of snake\_case
* Too much/too little empty spaces/empty lines
* Leaving in commented code

#### 3c. **Verify your routes using** [**Postman Tests**](https://www.youtube.com/watch?v=V2ZWdPMBwSA) **and submit an exported JSON of them**

Take some time looking through the guide to learn how to make tests using Postman. You should submit your Postman collection along with your CMS submission. The best way to get full credit for your tests is to ensure that you check that the response from your API matches what the API specification is expecting to receive. Please make sure that all your tests pass when we run your collection (make sure to clear your database before running to ensure you get the result we will get).

While that should be enough to get full credit, your tests can be cleaner and easier to change if you use [variables](https://learning.postman.com/docs/sending-requests/variables/) or [environments](https://learning.postman.com/docs/sending-requests/managing-environments/). It's up to you how much you want to learn and incorporate into your tests - the better you get at it the easier the assignments will be in the future and the likelier your API will pass the test cases we release on the first try.

If you'd prefer to learn how to use Postman through a video, [here](https://youtu.be/V2ZWdPMBwSA) is a link to everything you'll need to know for this class; however, the audio quality is quite poor due to a technical error. If you think you would benefit from a re-recording of a Postman demo, indicate that on the [feedback form](https://docs.google.com/forms/d/e/1FAIpQLSfRXyPmpfSkEQjDHtAPqSTbBCV47cR6O0363TgbxhzUsT9WHQ/viewform?usp=sf_link) and we will be happy to record one.

#### 3d. Zip and submit your assignment files

Next, zip the starter folder and submit on CMS. For reference, your directory structure should look like the following:&#x20;

```
pa1/
 |-README.txt
 |-postman_collection.json
 |-src/
    |-app.py
    |-requirements.txt
```

For clarification, this means that you **SHOULD NOT** include your virtual environment or pycache file in your final submission. Doing so will lose you a few points on the project.

#### 3e. Submit Feedback Form

To receive credit for your assignment, you **must** fill out the feedback form (link on the left sidebar of the textbook) so that we can better understand how effectively we are teaching and how long students are spending on assignments.

### 4. Prepare for Next Lecture

Students will most benefit from our class time if they come in with a few things done.

{% content-ref url="/pages/-M1OOdG4nVhm0\_8yzC4M" %}
[Pre-Class TODO's](/chapters/databases/pre-class.md)
{% endcontent-ref %}

## Optional Challenges

### **Tier I**&#x20;

**+1 point**<br>

**Validate body content of POST requests**

You've already enforced that post bodies come in with the required fields, now make sure those fields contain valid content. We will passing in malformed inputs that violate

* type preconditions
* logical preconditions (e.g. invalid URL\*)

We are expecting a **400 Bad Request** to be thrown when validating the contents as well. To ensure that your changes do not conflict with the base test cases, please create these changes in new routes under

**`POST`**`/api/extra/`

Specifically, we are looking for changes in all post requests so

**`POST`**`/api/extra/posts/`

**`POST`**`/api/extra/posts/{id}/comments/`

**`POST`**`/api/extra/posts/{pid}/comments/{cid}/`

### **Tier II**

**+1 point**

**Allow sorting through URL parameters**

URL parameters are a way to pass data through a GET request through the request URL (just like how we pass data through a POST request by using a post body). We want you to build functionality to accept a URL parameter at the end of the `/api/posts/` route (now as `/api/extra/posts/`) in the following form:&#x20;

`{WHEREVER YOUR SERVER IS HOSTED}/api/extra/posts/?key1=value1&key2=value2…`&#x20;

Since we want to sort the most popular posts (according to number of upvotes), you will also need to implement your own route to increment the upvote value of a post.

**`POST`**`/api/extra/posts/{id}/`

{% code title="Request" %}

```javascript
{
  "upvotes": 3
}
```

{% endcode %}

{% code title="Success Response" %}

```javascript
<HTTP STATUS CODE 200>
{
  "id": <ID>,
  "upvotes": <STORED UPVOTES OF POST WITH ID {id}> + 3,
  "title": <STORED TITLE OF POST WITH ID {id}>,
  "link": <STORED LINK OF POST WITH ID {id}>,
  "username": <STORED USERNAME OF POST WITH ID {id}>,
}
```

{% endcode %}

**Make the default behavior of calling this route (making a request with no post body) increment the upvotes by 1.**

**You will need to Google around on your own to implement this.** We highly recommend trying to Google your question and find a StackOverflow post first (chances are someone has tried to do this before) and if you cannot find anything then perhaps consider the official [Flask documentation](https://flask.palletsprojects.com/en/1.1.x/) (though this may be very dense to look through!). Of course, feel free to ask TAs during office hours if you are really stumped!

Please use the following naming conventions for your URL parameters:&#x20;

* Key: `sort`
* Value: `increasing` or `decreasing`

So, for example, `/api/extra/posts/?sort=increasing` would return posts in increasing order.

### Total Extra Credit Points to Gain: 3


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://backend-course.cornellappdev.com/chapters/routes/handout.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
