-
Notifications
You must be signed in to change notification settings - Fork 0
/
petcare-rest-api.py
132 lines (104 loc) · 3.99 KB
/
petcare-rest-api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# minimal-rest-api.py
# Minmal Restful API, a Pet care center.
# Show pets (dogs and cats) by 'name', and then the 'age', and 'gender'
# ------------------------ 0. Inizialiting
from flask import Flask, jsonify, request
petlist = Flask(__name__)
from petsfile import dogs,cats
# ------------------------ 1. ROUTE [GET]
@petlist.route('/', methods=['GET'])
def regards():
return jsonify({'response': 'Welcome!\n you can ask by dogs and cats'})
# ------------------------ 2. GET
#---------->>>>>Dogs
@petlist.route('/dogs', methods=['GET'])
def getDogs():
return jsonify({"dog's list": dogs})
@petlist.route('/dogs/<string:dog_name>')
def getDogName(dog_name):
dogsFound = [dogname for dogname in dogs if dogname['name'] == dog_name]
if (len(dogsFound) > 0):
return jsonify({"I found this dog on my list": dogsFound[0]})
return jsonify({'message': "Wow! this dog isn't in my list!"})
#---------->>>>>Cats
@petlist.route('/cats', methods=['GET'])
def getCats():
return jsonify({"cat's list": cats})
@petlist.route('/cats/<string:cat_name>')
def getCat(cat_name):
catsFound = [catname for catname in cats if catname['name'] == cat_name]
if (len(catsFound) > 0):
return jsonify({"I found this cat on my list": catsFound[0]})
return jsonify({'message': "Wow! this cat isn't in my list!"})
# ------------------------ 3. CREATE
#---------->>>>>Dogs
@petlist.route('/dogs', methods=['POST'])
def addDog():
new_dog = {
'name': request.json['name'],
'age': request.json['age'],
'gender': request.json['gender']
}
dogs.append(new_dog)
return jsonify({"Our new pet was successfully added": dogs})
#---------->>>>>Cats
@petlist.route('/cats', methods=['POST'])
def addCat():
new_cat = {
'name': request.json['name'],
'age': request.json['age'],
'gender': request.json['gender']
}
cats.append(new_cat)
return jsonify({"Our new pet was successfully added": cats})
# ------------------------ 4. UPDATE
#---------->>>>>Dogs
@petlist.route('/dogs/<string:dog_name>', methods=['PUT'])
def editDog(dog_name):
dogsFound = [dogname for dogname in dogs if dogname['name'] == dog_name]
if (len(dogsFound) > 0):
dogsFound[0]['name'] = request.json['name']
dogsFound[0]['age'] = request.json['age']
dogsFound[0]['gender'] = request.json['gender']
return jsonify({
'message': 'We have a new pet now!',
'dogs list': dogsFound[0]
})
return jsonify({'message': "Wow! this cat isn't in my list!"})
#---------->>>>>Cats
@petlist.route('/cats/<string:cat_name>', methods=['PUT'])
def editCat(cat_name):
catsFound = [catname for catname in cats if catname['name'] == cat_name]
if (len(catsFound) > 0):
catsFound[0]['name'] = request.json['name']
catsFound[0]['age'] = request.json['age']
catsFound[0]['gender'] = request.json['gender']
return jsonify({
'message': 'We have a new pet now!',
'cats list': catsFound[0]
})
return jsonify({'message': "Wow! this cat isn't in my list!"})
# ------------------------ 5. DELETE
#---------->>>>>Dogs
@petlist.route('/dogs/<string:dog_name>', methods=['DELETE'])
def adoptDog(dog_name):
dogsFound = [dogname for dogname in dogs if dogname['name'] == dog_name]
if len(dogsFound) > 0:
dogs.remove(dogsFound[0])
return jsonify({
'message': 'We have given a puppy up for adoption',
'Our dogs': dogs
})
#---------->>>>>Cats
@petlist.route('/cats/<string:cat_name>', methods=['DELETE'])
def adoptCat(cat_name):
catsFound = [catname for catname in cats if catname['name'] == cat_name]
if len(catsFound) > 0:
cats.remove(catsFound[0])
return jsonify({
'message': 'We have given a kitten up for adoption',
'Our cats': cats
})
# ------------------------ 6. RUN
if __name__ == '__main__':
petlist.run(debug=True,host='0.0.0.0',port=4000)