Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3bc82b3

Browse files
initial version
1 parent 4a21260 commit 3bc82b3

File tree

30 files changed

+535
-77
lines changed

30 files changed

+535
-77
lines changed

‎.gitpod.Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM gitpod/workspace-full
2+
USER gitpod
3+
RUN pip3 install pytest==4.4.2 pytest-testdox mock && npm i breathecode-cli@1.2.52 -g

‎.gitpod.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
image:
2+
file: .gitpod.Dockerfile
3+
4+
# List the ports you want to expose and what to do when they are served. See https://www.gitpod.io/docs/config-ports/
5+
ports:
6+
- port: 3000
7+
onOpen: open-preview
8+
9+
# List the start up tasks. You can start them in parallel in multiple terminals. See https://www.gitpod.io/docs/config-start-tasks/
10+
tasks:
11+
- command: bc run

‎bc.json

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,3 @@
11
{
2-
"port": 3000,
3-
"address": "localhost",
4-
"editor": "standalone",
5-
"outputPath": "./.breathecode/dist",
6-
"publicPath": "/preview",
7-
"grading": "isolated",
8-
"ignoreRegex": null,
9-
"webpack_template": null,
10-
"disable_grading": false,
11-
"onCompilerSuccess": null,
12-
"language": "python3",
13-
"compiler": "python3",
14-
"tester": "pytest",
15-
"actions": [
16-
"run",
17-
"test",
18-
"reset"
19-
],
20-
"session": 8780419452529280000,
21-
"exercisesPath": "./exercises",
22-
"exercises": [
23-
{
24-
"slug": "01-hello-world",
25-
"title": "01-hello-world",
26-
"path": "exercises/01-hello-world",
27-
"translations": [
28-
"us"
29-
]
30-
},
31-
{
32-
"slug": "01-what-is-a-request",
33-
"title": "01-what-is-a-request",
34-
"path": "exercises/01-what-is-a-request",
35-
"translations": [
36-
"us"
37-
]
38-
}
39-
]
2+
"language": "python3"
403
}
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
# 02 What is a request
1+
# 02 Creating a request
22

3-
HTTP is a set of rules that allow two endpoints A - B to communicate with each other in only one direction:
4-
```txt
5-
A = Client B = Server
6-
```
7-
8-
- The client always asks a `request` to one server URL.
9-
- The server wais for request and answers max one `response` for each request.
3+
Python has a [requests package](https://requests.readthedocs.io/en/master/) that allows developers to create HTTP request pretty easily.
104

11-
In python this is how we make a request:
5+
In python this is how we make an HTTP GET request:
126

137
```python
14-
from http.client import HTTPSConnection
15-
16-
host_url = "assets.breatheco.de"
17-
path = "/apis/fake/hello.php"
18-
19-
conn = HTTPSConnection(host_url)
20-
conn.request("GET", path, body=None, headers={})
21-
22-
r1 = conn.getresponse()
23-
response = {
24-
"status": r1.status,
25-
"body": r1.read()
26-
}
27-
28-
print(response)
8+
response = requests.get('<destination url>')
9+
print(response.status_code)
2910
```
3011

3112
# 📝 Instructions
3213

33-
Change the following code to print on the console `success` if the response status is between 200 and 399 or `fail` if the response is between 400 and 599.
14+
Change the variable url to make it request to:
15+
16+
```bash
17+
https://assets.breatheco.de/apis/fake/hello.php
18+
```
3419

35-
Note: Only print success or fail, do not print anything else.
20+
Note: The console should print a 200 status code.

‎exercises/01-what-is-a-request/app.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
1-
fromhttp.clientimport HTTPSConnection
1+
import requests
22

3-
host_url = "assets.breatheco.de"
4-
path = "/apis/fake/hello.php"
3+
url = "https://assets.breatheco.de/apis/fake/sample/404-example.php"
4+
response = requests.get(url)
55

6-
conn = HTTPSConnection(host_url)
7-
conn.request("GET", path, body=None, headers={})
8-
9-
r1 = conn.getresponse()
10-
response = {
11-
"status": r1.status,
12-
"body": r1.read()
13-
}
14-
15-
print(response)
6+
print("The response status is: "+str(response.status_code))

‎exercises/01-what-is-a-request/test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import json, pytest
2+
from mock import patch
3+
4+
@pytest.mark.it("Update the url to hello.php")
5+
def test_url(app):
6+
with patch('requests.get') as mock_request:
7+
app()
8+
url = "https://assets.breatheco.de/apis/fake/hello.php"
9+
assert mock_request.call_args.args[0] == url

‎exercises/02-random-status/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# `02` Handle Response Status
2+
3+
The following GET request is always returning a different status code, you never know what reponse you are getting form the server.
4+
5+
## 📝Instructions
6+
7+
Create a condition to print on the console the following messages depending on the response status:
8+
9+
| Status | Message |
10+
| ----- | ----- |
11+
| 404 | The URL you asked is not found |
12+
| 503 | Unavailable right now |
13+
| 200 | Everythign went perfect |
14+
| 400 | Something is wrong on the request params |

‎exercises/02-random-status/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import requests
2+
3+
response = requests.get("https://assets.breatheco.de/apis/fake/sample/random-status.php")

‎exercises/02-random-status/test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json, pytest
2+
from mock import patch
3+
4+
@pytest.mark.it("requests.get has to be called for the random-status.php url")
5+
def test_url_call(capsys, app):
6+
with patch('requests.get') as mock_request:
7+
app()
8+
mock_request.assert_called_once_with("https://assets.breatheco.de/apis/fake/sample/random-status.php")
9+
10+
11+
@pytest.mark.it("Testing for 200: Everythign went perfect")
12+
def test_url_200(capsys, app):
13+
with patch('requests.get') as mock_request:
14+
mock_request.return_value.status_code = 200
15+
app()
16+
captured = capsys.readouterr()
17+
assert "Everythign went perfect\n" == captured.out
18+
19+
@pytest.mark.it("When testing for 404 it should print The URL you asked is not found'")
20+
def test_url_404(capsys, app):
21+
with patch('requests.get') as mock_request:
22+
mock_request.return_value.status_code = 404
23+
app()
24+
captured = capsys.readouterr()
25+
assert "The URL you asked is not found\n" == captured.out
26+
27+
@pytest.mark.it("Testing for 503: Unavailable right now")
28+
def test_url_503(capsys, app):
29+
with patch('requests.get') as mock_request:
30+
mock_request.return_value.status_code = 503
31+
app()
32+
captured = capsys.readouterr()
33+
assert "Unavailable right now\n" == captured.out
34+
35+
@pytest.mark.it("Testing for 400: Something is wrong on the request params")
36+
def test_url_400(capsys, app):
37+
with patch('requests.get') as mock_request:
38+
mock_request.return_value.status_code = 400
39+
app()
40+
captured = capsys.readouterr()
41+
assert "Something is wrong on the request params\n" == captured.out

‎exercises/03-response-body/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# `03` Response Body
2+
3+
Click on this link to see the response body that this GET request is giving:
4+
[https://assets.breatheco.de/apis/fake/sample/random-status.php](https://assets.breatheco.de/apis/fake/sample/random-status.php)
5+
6+
Now, if you want to get that response body (text) all you have to do is:
7+
```py
8+
# plain text
9+
print(response.text)
10+
```
11+
12+
# 📝 Instructions
13+
14+
Print on the console the response body only for 200 requests, for all the other print "Something wend wrong".

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /