forked from udacity/OAuth2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpointsproject3.py
108 lines (81 loc) · 2.96 KB
/
endpointsproject3.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
from flask import Flask, request, jsonify, abort
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base, Puppy, User
engine = create_engine('sqlite:///puppies.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
app = Flask(__name__)
# Create the appropriate app.route functions,
# test and see if they work
# Make an app.route() decorator here
@app.route("/")
@app.route("/puppies", methods=['GET', 'POST'])
def puppiesFunction():
if request.method == 'GET':
# Call the method to Get all of the puppies
return getAllPuppies()
elif request.method == 'POST':
# Call the method to make a new puppy
print "Making a New puppy"
name = request.args.get('name', '')
description = request.args.get('description', '')
print name
print description
return makeANewPuppy(name, description)
@app.route('/users', methods=['POST'])
def new_user():
username = request.args.get('username')
password = request.args.get('password')
if username is None or password is None:
abort(400)
if session.query(User).filter_by(username=username).one() is not None:
abort(400)
user = User(username=username)
user.hash_password(password)
session.add(user)
session.commit()
return jsonify({'username': user.username}), 201
# Make another app.route() decorator here that takes in an integer id in the URI
@app.route("/puppies/<int:id>", methods=['GET', 'PUT', 'DELETE'])
# Call the method to view a specific puppy
def puppiesFunctionId(id):
if request.method == 'GET':
return getPuppy(id)
# Call the method to edit a specific puppy
elif request.method == 'PUT':
name = request.args.get('name', '')
description = request.args.get('description', '')
return updatePuppy(id, name, description)
# Call the method to remove a puppy
elif request.method == 'DELETE':
return deletePuppy(id)
def getAllPuppies():
puppies = session.query(Puppy).all()
return jsonify(Puppies=[i.serialize for i in puppies])
def getPuppy(id):
puppy = session.query(Puppy).filter_by(id=id).one()
return jsonify(puppy=puppy.serialize)
def makeANewPuppy(name, description):
puppy = Puppy(name=name, description=description)
session.add(puppy)
session.commit()
return jsonify(Puppy=puppy.serialize)
def updatePuppy(id, name, description):
puppy = session.query(Puppy).filter_by(id=id).one()
if not name:
puppy.name = name
if not description:
puppy.description = description
session.add(puppy)
session.commit()
return "Updated a Puppy with id %s" % id
def deletePuppy(id):
puppy = session.query(Puppy).filter_by(id=id).one()
session.delete(puppy)
session.commit()
return "Removed Puppy with id %s" % id
if __name__ == '__main__':
app.debug = False
app.run(host='0.0.0.0', port=5000, threaded=False)