Skip to content
This repository was archived by the owner on Sep 27, 2024. It is now read-only.

Commit fd457ec

Browse files
move falcon tutorials
1 parent ea3e907 commit fd457ec

8 files changed

Lines changed: 228 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Watch the video for this code its in 2 parts.
2+
3+
Part 1: https://www.youtube.com/watch?v=W6JvboT8Uo8
4+
5+
Part 2: https://www.youtube.com/watch?v=vHbdyL9aZ-M
6+
7+
Subscribe to my Youtube channel here:
8+
https://www.youtube.com/parisnakitakejser
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json, falcon
2+
3+
class ObjRequstClass:
4+
def on_get(self, req, resp):
5+
resp.status = falcon.HTTP_200
6+
7+
data = json.loads(req.stream.read())
8+
9+
content = {
10+
'name' : 'Paris',
11+
'age' : '31',
12+
'country' : 'Denmark'
13+
}
14+
15+
output = {}
16+
if('method' not in data):
17+
resp.status = falcon.HTTP_501
18+
output['value'] = 'Error: none method found - sorry'
19+
else:
20+
if(data['method'] == 'get-name'):
21+
output['value'] = content['name']
22+
else:
23+
resp.status = falcon.HTTP_404
24+
output['value'] = None
25+
26+
resp.body = json.dumps(output)
27+
28+
29+
api = falcon.API()
30+
api.add_route('/test', ObjRequstClass())
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Watch this codes tutorial on Youtube:
2+
3+
Part 3: https://youtu.be/Zx3LmI8tb0A
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import json, falcon
2+
3+
class ObjRequstClass:
4+
def on_get(self, req, resp):
5+
resp.status = falcon.HTTP_200
6+
7+
data = json.loads(req.stream.read())
8+
9+
output = {
10+
'msg' : 'Hello {0}'.format(data['name'])
11+
}
12+
resp.body = json.dumps(output)
13+
14+
def on_post(self, req, resp):
15+
resp.status = falcon.HTTP_200
16+
17+
data = json.loads(req.stream.read())
18+
19+
equel = int(data['x']) + int(data['y'])
20+
21+
output = {
22+
'msg' : 'x: {x} + y: {y} is equel to {e}'.format(x=data['x'],y=data['y'],e=equel)
23+
}
24+
resp.body = json.dumps(output)
25+
26+
def on_put(self, req, resp):
27+
resp.status = falcon.HTTP_200
28+
output = {
29+
'msg' : 'put is not supported for now - sorry :('
30+
}
31+
32+
resp.body = json.dumps(output)
33+
34+
def on_delete(self, req, resp):
35+
resp.status = falcon.HTTP_200
36+
output = {
37+
'msg' : 'delete is not supported for now - sorry :('
38+
}
39+
40+
resp.body = json.dumps(output)
41+
42+
43+
44+
api = falcon.API()
45+
api.add_route('/demo', ObjRequstClass())
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import json, falcon
2+
3+
class ObjRequstClass:
4+
def on_get(self, req, resp):
5+
resp.status = falcon.HTTP_200
6+
7+
validate_params = True
8+
9+
if 'name' not in req.params:
10+
validate_params = False
11+
12+
if 'age' not in req.params:
13+
validate_params = False
14+
15+
16+
if(validate_params is True):
17+
output = {
18+
'name' : req.params['name'],
19+
'age' : req.params['age'],
20+
}
21+
else:
22+
output = {
23+
'error' : 'You need name or age in your params'
24+
}
25+
26+
resp.body = json.dumps(output)
27+
28+
29+
api = falcon.API()
30+
api.add_route('/params', ObjRequstClass())
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import json, falcon
2+
3+
class ObjRequstClass:
4+
def on_post(self, req, resp):
5+
resp.status = falcon.HTTP_200
6+
json_validate = False
7+
8+
try:
9+
json_input = json.loads(req.stream.read())
10+
json_validate = True
11+
except:
12+
json_input = {}
13+
14+
if json_validate is not True:
15+
resp.status = falcon.HTTP_404
16+
output = {
17+
'validate' : {
18+
'status' : 404,
19+
'msg' : 'json data not validated currect.'
20+
}
21+
}
22+
23+
else:
24+
if 'name' not in json_input:
25+
output = {
26+
'welcom_msg' : 'Hello guest, how are you?'
27+
}
28+
elif 'age' not in json_input:
29+
output = {
30+
'welcom_msg' : 'Hello {name}, what is your age?'.format(name=json_input['name'])
31+
}
32+
else:
33+
output = {
34+
'welcom_msg' : 'Hello {name}, you are {age} years old.'.format(name=json_input['name'],age=json_input['age'])
35+
}
36+
37+
resp.body = json.dumps(output)
38+
39+
40+
api = falcon.API()
41+
api.add_route('/body-json', ObjRequstClass())
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Watch this tutorial on youtube:
2+
3+
Part 4: https://youtu.be/8vZfEarQC10
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import json, falcon
2+
3+
class ObjRequstClass:
4+
__json_content = {}
5+
6+
def __validate_json_input(self, req):
7+
try:
8+
self.__json_content = json.loads(req.stream.read())
9+
print 'json from client is validated! :)'
10+
return True
11+
12+
except ValueError, e:
13+
self.__json_content = {}
14+
print 'json from client is not validted :('
15+
return False
16+
17+
def on_get(self, req, resp):
18+
resp.status = falcon.HTTP_200
19+
validated = self.__validate_json_input(req)
20+
21+
output = {
22+
'status' : 200,
23+
'msg' : None
24+
}
25+
26+
if(validated == True):
27+
if 'name' in self.__json_content:
28+
output['msg'] = 'Hello {name}'.format(name=self.__json_content['name'])
29+
else:
30+
output['status'] = 404
31+
output['msg'] = 'json input need name params'
32+
33+
else:
34+
output['status'] = 404
35+
output['msg'] = 'json input is not validated'
36+
37+
resp.body = json.dumps(output)
38+
39+
def on_post(self, req, resp):
40+
resp.status = falcon.HTTP_200
41+
42+
data = json.loads(req.stream.read())
43+
44+
equel = int(data['x']) + int(data['y'])
45+
46+
output = {
47+
'msg' : 'x: {x} + y: {y} is equel to {e}'.format(x=data['x'],y=data['y'],e=equel)
48+
}
49+
resp.body = json.dumps(output)
50+
51+
def on_put(self, req, resp):
52+
resp.status = falcon.HTTP_200
53+
output = {
54+
'msg' : 'put is not supported for now - sorry :('
55+
}
56+
57+
resp.body = json.dumps(output)
58+
59+
def on_delete(self, req, resp):
60+
resp.status = falcon.HTTP_200
61+
output = {
62+
'msg' : 'delete is not supported for now - sorry :('
63+
}
64+
65+
resp.body = json.dumps(output)
66+
67+
api = falcon.API()
68+
api.add_route('/demo', ObjRequstClass())

0 commit comments

Comments
 (0)