TASK: Create a JSONPlaceholder class that implements GET, POST, PUT, and DELETE requests using the requests module. The class has the following class diagram
The class is initialized with a base_url property that will be the endpoint where the requests are sent. The endpoint for this assignment should be JSON Placeholder posts.
The class should have the following methods:
get_request(): Sends aGETrequest to thebase_url. Returns a dictionary with the (1) status code, (2) headers, and (3) first 500 characters in the response body.post_request(data): Takes a dictionary of data and sends aPOSTrequest with the data to the base_url. It then returns a dictionary with the (1) status code, (2) headers, and (3) first 500 characters in the response body. The format of the dictionary being returned is shown below:
example_dictionary = {
"status_code": "STATUS_CODE_HERE",
"headers": "HEADERS HERE",
"content": "CONTENT_HERE"
}update_user(userId, title, body): Accepts auserID,title, andbodyas arguments. The method should then: - Create a dictionary that contains thetitleandbody.
- Format thebase_urlto point to the specifieduserIdendpoint (e.g. If theuserIdis 5 then then url would bebase_url/5). - Send aPUTrequest to the modified base_url with the dictionary data you created. - Return a dictionary with the (1) status code, (2) headers, and (3) first 500 characters in the response body.delete_user(user_id): Accepts auserIDas an argument. The method should then:- Format the
base_urlto point to the specifieduserIdendpoint (e.g. If the userId is 5 then then url would bebase_url/5). - Send a
DELETErequest to the modifiedbase_url. - Return a dictionary with the status code.
- Format the
This assignment can grade itself! To setup the autograding, you should do the following:
- Clone this file to your local machine using the command
git clone PASTE_URL_HERE- Open the downloaded file in your VS Code editor.
- In the left hand sidebar, press the "Testing" menu represented by the picture of a flask.
- Click "Configure Python Tests".
- You'll have two options to select. Select pytest.
- Select the folder where the tests live. You can simply select
. Root directory. - You can now run the tests by pressing the play icon. A passing test will get a ✅ and a failing test will get a ❌.
- Run the tests as you code and by the end it should be all ✅ if you have followed the specifications for this assignment!
Below is a summary of what each unit test for this assignment is checking for.
test_jsonplaceholder_initialization: Test if JSONPlaceholder class is initialized correctly withbase_url.
test_get_request_success: Test ifget_requestmethod returns correct data structure with expected values.test_get_request_with_correct_url: Test ifget_requestmethod calls requests.get with the correct URL.
test_post_request_success: Test if post_request method returns correct data structure.test_post_request_correct_parameters: Test if post_request method calls requests.post with correct parameters.
test_update_user_success: Tests successfulPUTrequest to update a user usingupdate_user.test_update_user_correct_url_and_data: Tests ifupdate_usermakes PUT request with correct URL and data.
test_delete_user_success: Test successfulDELETErequest usingdelete_usermethod.test_delete_user_correct_url: Test ifdelete_usermakesDELETErequest with correct URL.
