Skip to content

Commit f60cbc0

Browse files
committed
couple more exercises
1 parent 5b119bd commit f60cbc0

File tree

16 files changed

+337
-5
lines changed

16 files changed

+337
-5
lines changed

README.md

+68-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,68 @@
1-
# Python HTTP Requests Exercises
1+
<table>
2+
<tr>
3+
<td><img src="https://assets.breatheco.de/apis/img/images.php?blob&random&cat=icon&tags=4geeks,32"></td>
4+
<td>
5+
<h1 align="center"> Python API Requests Tutorial at 4Geeks Academy<br /><small>By <a href="https://twitter.com/alesanchezr">@alesanchezr</a></small></h1>
6+
<img src="https://img.shields.io/github/last-commit/4geeksacademy/python-http-requests-api-tutorial-exercises" />
7+
<a href="https://breatheco.de"><img src="https://img.shields.io/badge/certified-BreatheCode-blue" /></a>
8+
<a href="https://twitter.com/alesanchezr"><img src="https://img.shields.io/twitter/follow/alesanchezr?style=social&logo=twitter" alt="follow on Twitter"></a>
9+
<a href="https://gitpod.io#https://github.com/4GeeksAcademy/python-http-requests-api-tutorial-exercises.git"><img src="https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod" /></a>
10+
</td>
11+
</tr>
12+
</table>
13+
14+
Learn HTTP and how to use the Python Requests package to create HTTP Requests GET, POST, PUT, DELETE.
15+
16+
<ol>
17+
<li><a href="https://github.com/4GeeksAcademy/python-beginner-programming-exercises">Python for Beginners</a></li>
18+
<li><a href="https://github.com/4GeeksAcademy/python-lists-loops-programming-exercises">Practice Looping Lists and Tuples</a></li>
19+
<li><a href="https://github.com/4GeeksAcademy/python-functions-programming-exercises">Practice functions</a></li>
20+
<li><a href="https://github.com/4GeeksAcademy/master-python-programming-exercises">Master Python</a></li>
21+
<li><a href="https://github.com/4GeeksAcademy/python-http-requests-api-tutorial-exercises">Python API Requests</a>← you are here right now</li>
22+
</ol>
23+
24+
These exercises were built in collaboration, we need you! If you find any bugs or misspells plese contribute and report them.
25+
26+
<p align="center">
27+
<img src="https://raw.githubusercontent.com/4GeeksAcademy/react-exercises/master/preview.gif">
28+
</p>
29+
30+
<h2>One click installation:</h2>
31+
32+
[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io#https://github.com/4GeeksAcademy/python-http-requests-api-tutorial-exercises.git)
33+
34+
35+
## Local Installation
36+
37+
1) Make sure you have the [breathecode-cli](https://github.com/breatheco-de/breathecode-cli) installed and `node.js` version 10+ and python version 3+. This is the command to install the breathecode-cli
38+
```
39+
$ npm i breathecode-cli -g
40+
```
41+
42+
2) Clone or download this repository. Once you finish downloading, you will find a new folder with a subdirectory "exercises" that contains all the exercises within.
43+
44+
3) Start the tutorial/exercises by running the following command from the root of the project:
45+
46+
```sh
47+
$ pip install pytest==4.4.2 mock pytest-testdox
48+
$ breathecode run
49+
```
50+
51+
## How are the exercises organized?
52+
53+
Each exercise is a small react application containing the following files:
54+
55+
1. **app.py:** represents the entry python file that will be executed by the computer.
56+
2. **README.md:** contains exercise instructions.
57+
3. **test.py:** you don't have to open this file, it contains the testing script for the exercise.
58+
59+
## Contributors
60+
61+
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
62+
63+
1. [Alejandro Sanchez (alesanchezr)](https://github.com/alesanchezr), contribution: (coder) :computer: (idea) 🤔, (build-tests) :warning:, (pull-request-review) :eyes: (build-tutorial) :white_check_mark: (documentation) :book:
64+
2. [Paolo (plucodev)](https://github.com/plucodev), contribution: (bug reports) :bug:, contribution: (coder), (translation) :earth_americas:
65+
66+
This project follows the
67+
[all-contributors](https://github.com/kentcdodds/all-contributors)
68+
specification. Contributions of any kind are welcome!

exercises/01-hello-world/README.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
# Hello World
1+
# Python API Requests
22

3-
Type here your exercise instructions
3+
Python Requests is the most popular package for consuming API's and doing HTTP requests.
4+
5+
Here you will learn:
6+
7+
1. How to do GET requests.
8+
2. How to fech data properties and information.
9+
3. How to set request headers.
10+
4. How to set request content-type.
11+
5. How to do POST requests.
12+
13+
Click the `next →` button on the top right to continue.

exercises/09-array-of-blog-titles/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# `09` Array of post titles
1+
# `09` Array of blog titles
22

33
# 📝 Instructions
44

exercises/09-array-of-blog-titles/test.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,23 @@ def json(self):
1414
]
1515
}
1616

17-
@pytest.mark.it("Make sure you are printing the author name of the FIRST post")
17+
@pytest.mark.it("You seem to be returning None or not retuning anything")
18+
def test_for_null(app):
19+
with patch('requests.get') as mock_request:
20+
from app import get_titles
21+
mock_request.return_value = FakeResponse()
22+
resp = get_titles()
23+
assert resp is not None
24+
25+
@pytest.mark.it("The function should have returned a list but returned something different")
26+
def test_return_type(app):
27+
with patch('requests.get') as mock_request:
28+
from app import get_titles
29+
mock_request.return_value = FakeResponse()
30+
tags = get_titles()
31+
assert isinstance(tags, list)
32+
33+
@pytest.mark.it("Return a list of post titles like: ['title 1','title 2', 'title 3']")
1834
def test_array_content(app):
1935
with patch('requests.get') as mock_request:
2036
from app import get_titles

exercises/10-get_post_tags/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# `10` Get post tags
2+
3+
# 📝 Instructions
4+
5+
Using the same endpoint from the previous exercise, create a function `get_post_tags` that returns the array of tags of a given `post id`
6+
7+
## 💡Hint
8+
9+
1. Create the function `get_post_tags`.
10+
2. GET all the posts by doing the GET request to the endpoint.
11+
3. declare the variable to store the serialized body
12+
4. Using the `post_id` parameter loop all the posts and compare their id's to see if they match the `post_id`.
13+
3. When you find the post you want, return it's list of tags
14+

exercises/10-get_post_tags/app.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests
2+
3+
def get_post_tags(post_id):
4+
# your code here
5+
return None
6+
7+
8+
print(get_post_tags(146))

exercises/10-get_post_tags/test.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import json, pytest
2+
from mock import patch
3+
4+
class FakeResponse(object):
5+
# default response attributes
6+
status_code = 200
7+
def json(self):
8+
return {
9+
"posts": [
10+
{ "id": 234, "tags": [
11+
{ "id": 25, "title": "programming" },
12+
{ "id": 26, "title": "software development"}
13+
]
14+
},
15+
{ "id": 224,
16+
"tags": [
17+
{ "id": 23, "title": "react" },
18+
{ "id": 11, "title": "cooking"}
19+
]
20+
},
21+
{ "id": 14, "tags": [] },
22+
{ "id": 34, "tags": [] },
23+
{ "id": 24, "tags": [] },
24+
]
25+
}
26+
27+
@pytest.mark.it("You seem to be returning None or not retuning anything")
28+
def test_for_null(app):
29+
with patch('requests.get') as mock_request:
30+
from app import get_post_tags
31+
mock_request.return_value = FakeResponse()
32+
tags = get_post_tags(224)
33+
assert tags is not None
34+
35+
@pytest.mark.it("The function should have returned a list but returned something different")
36+
def test_return_type(app):
37+
with patch('requests.get') as mock_request:
38+
from app import get_post_tags
39+
mock_request.return_value = FakeResponse()
40+
tags = get_post_tags(224)
41+
assert isinstance(tags, list)
42+
43+
@pytest.mark.it("Given the post id, return a list with the post tags given")
44+
def test_array_content(app):
45+
with patch('requests.get') as mock_request:
46+
from app import get_post_tags
47+
mock_request.return_value = FakeResponse()
48+
49+
tags = get_post_tags(224)
50+
51+
assert len(tags) == 2
52+
assert tags[0]["id"] == 23
53+
assert tags[1]["id"] == 11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `11` Get atthachment by id
2+
3+
# 📝 Instructions
4+
5+
Using the same endpoint from the previous exercise, create a function `get_attachment_by_id` that returns the title of the attachtment that corresponds to the given id.
6+
7+
## 💡Hint
8+
9+
1. Create the function `get_attachment_by_id` that recives the `attachment_id` as a parameter.
10+
2. Loop all the posts.
11+
3. Loop all the attachments for each post.
12+
4. If the attachchment has the given `attachment_id` in the function paramter, return it.
13+
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import requests
2+
3+
def get_attachment_by_id(attachment_id):
4+
# your code here
5+
return None
6+
7+
print(get_attachment_by_id(137))
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import json, pytest
2+
from mock import patch
3+
4+
class FakeResponse(object):
5+
# default response attributes
6+
status_code = 200
7+
def json(self):
8+
return {
9+
"posts": [
10+
{ "attachments": [{ "id": 22, "title": "asd" }] },
11+
{ "attachments": [{ "id": 137, "title": "cert" }] },
12+
{ "attachments": [{ "id": 11, "title": "asd" }] },
13+
{ "attachments": [{ "id": 40, "title": "asd" }] },
14+
{ "attachments": [{ "id": 314, "title": "asd" }] },
15+
]
16+
}
17+
18+
@pytest.mark.it("You seem to be returning None or not retuning anything on the function")
19+
def test_for_null(app):
20+
with patch('requests.get') as mock_request:
21+
from app import get_attachment_by_id
22+
mock_request.return_value = FakeResponse()
23+
att = get_attachment_by_id(137)
24+
assert att is not None
25+
26+
@pytest.mark.it("The function should have returned a string (the att title) but returnied something different")
27+
def test_return_type(app):
28+
with patch('requests.get') as mock_request:
29+
from app import get_attachment_by_id
30+
mock_request.return_value = FakeResponse()
31+
title = get_attachment_by_id(137)
32+
assert isinstance(title, str)
33+
34+
@pytest.mark.it("Return the attachment title")
35+
def test_array_content(app):
36+
with patch('requests.get') as mock_request:
37+
from app import get_attachment_by_id
38+
mock_request.return_value = FakeResponse()
39+
title = get_attachment_by_id(137)
40+
assert title == "cert"

exercises/12-post-request/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# `12` POST request
2+
3+
POST requests are meant for creation, for example, if your business is an t-shirt e-commerce you probably want to have a way to add new clients.
4+
5+
A post request usually have:
6+
7+
| | |
8+
| ---- | ---- |
9+
| Method | POST |
10+
| Content-Type | application/json |
11+
| Body (payload) | JSON string |
12+
13+
# 📝 Instructions
14+
15+
Send a POST request to the following URL:
16+
17+
POST: https://assets.breatheco.de/apis/fake/sample/post.php
18+
19+
And print the response text on the console
20+
21+
## 💡Hint
22+
23+
Use the requests package post method:
24+
https://www.w3schools.com/python/ref_requests_post.asp
25+

exercises/12-post-request/app.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
# your code here

exercises/12-post-request/test.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json, pytest
2+
from mock import patch
3+
4+
@pytest.mark.it("POST request to the specified endpoint")
5+
def test_url(app):
6+
with patch('requests.post') as mock_request:
7+
app()
8+
url = "https://assets.breatheco.de/apis/fake/sample/post.php"
9+
assert mock_request.call_args.args[0] == url
10+
11+
@pytest.mark.it("Print the response text in the console")
12+
def test_print(capsys, app):
13+
with patch('requests.get') as mock_request:
14+
app()
15+
captured = capsys.readouterr()
16+
assert captured.out.find("Excelent") > 0
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# `13` POST request body
2+
3+
# 📝 Instructions
4+
5+
Send a POST request to the following URL:
6+
https://assets.breatheco.de/apis/fake/sample/save-project-json.php
7+
8+
With `Content-Type: application/json`
9+
10+
With the following request body as JSON text:
11+
12+
```json
13+
{
14+
"id":2323,
15+
"title": "Very big project"
16+
}
17+
18+
```
19+
20+
## 💡Hint
21+
22+
Use the requests package post method:
23+
https://www.w3schools.com/python/ref_requests_post.asp
24+

exercises/13-post-request-body/app.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import requests
2+
3+
resp = requests.post("https://assets.breatheco.de/apis/fake/sample/save-project-json.php")
4+
print(resp.text)
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import json, pytest
2+
from mock import patch
3+
4+
@pytest.mark.it("POST request to the specified endpoint")
5+
def test_url(app):
6+
with patch('requests.post') as mock_request:
7+
app()
8+
url = "https://assets.breatheco.de/apis/fake/sample/save-project-json.php"
9+
assert mock_request.call_args.args[0] == url
10+
11+
@pytest.mark.it("Request header content-type must be application/json")
12+
def test_headers(app):
13+
with patch('requests.post') as mock_request:
14+
app()
15+
assert "headers" in mock_request.call_args.kwargs
16+
assert "Content-Type" in mock_request.call_args.kwargs["headers"] or "content-type" in mock_request.call_args.kwargs["headers"]
17+
18+
headers = mock_request.call_args.kwargs["headers"]
19+
if "Content-Type" in headers:
20+
assert headers["Content-Type"].lower() == "application/json"
21+
if "content-type" in headers:
22+
assert headers["content-type"].lower() == "application/json"
23+
24+
@pytest.mark.it("Request body must be a dictionary json with id and title")
25+
def test_body(app):
26+
with patch('requests.post') as mock_request:
27+
app()
28+
assert "json" in mock_request.call_args.kwargs
29+
30+
body = mock_request.call_args.kwargs["json"]
31+
assert "id" in body
32+
assert "title" in body

0 commit comments

Comments
 (0)