Skip to content

Pavani #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
a6eb6e0
Update README.md
tko22 Feb 11, 2018
9bffa66
Update app.py
shreyshrey1 Feb 11, 2018
adbadd5
Update app.py
shreyshrey1 Feb 11, 2018
4db5b46
Update README.md
shreyshrey1 Feb 11, 2018
aecd38e
Update README.md
tko22 Feb 11, 2018
cd0832b
Merge pull request #3 from shreyshrey1/patch-3
shreyshrey1 Feb 11, 2018
f8f80c8
Merge pull request #2 from shreyshrey1/patch-2
shreyshrey1 Feb 11, 2018
54cc015
Merge pull request #1 from shreyshrey1/patch-1
shreyshrey1 Feb 11, 2018
0341007
Update README.md
tko22 Feb 11, 2018
9ee7457
create_response wrapper and cleaned up code
vrunjeti Feb 12, 2018
0d345e5
added gitignore
vrunjeti Feb 12, 2018
bf9be4a
mock db and interface
vrunjeti Feb 14, 2018
a38b477
dont allow update to change id
vrunjeti Feb 14, 2018
5a6b496
readme for new exercises
vrunjeti Feb 14, 2018
dc5d2c7
Merge pull request #4 from hack4impact-uiuc/exercise-update
vrunjeti Feb 14, 2018
03ea690
fixed postman get pictures
vrunjeti Feb 14, 2018
a389101
fixed git clone url
vrunjeti Feb 15, 2018
234e128
...and then fixed the line RIGHT AFTER
vrunjeti Feb 15, 2018
a82b337
fixed readme images
vrunjeti Feb 15, 2018
d6e0f66
Merge pull request #5 from hack4impact-uiuc/readme-images-fix
vrunjeti Feb 15, 2018
aba2dcf
Clarified part 4
alex-wuu Feb 16, 2018
afebd77
Replacing picture with json post
alex-wuu Feb 16, 2018
cca18c0
Update postman_post for readme
alex-wuu Feb 16, 2018
5a53dc6
pr instructions
vrunjeti Feb 17, 2018
836ad85
Merge pull request #7 from hack4impact-uiuc/pr-instructions
vrunjeti Feb 17, 2018
43483cd
Part 1 Complete
Feb 18, 2018
de4de6c
Part 2 and 3 Complete
Feb 18, 2018
7c225f0
Part 4 5 6 Complete
Feb 18, 2018
03cce11
updated POST method
Feb 23, 2018
61651b8
updated POST method
Feb 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.pyc
.vscode/
venv/
__pycache__
.DS_Store/
migrations/
303 changes: 205 additions & 98 deletions README.md

Large diffs are not rendered by default.

113 changes: 95 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,99 @@
from flask import Flask
from flask import render_template
from flask import jsonify
from flask import Flask, jsonify, request
import mockdb.mockdb_interface as db

app = Flask(__name__)

def create_response(data={}, status=200, message=''):
"""
Wraps response in a consistent format throughout the API
Format inspired by https://medium.com/@shazow/how-i-design-json-api-responses-71900f00f2db
Modifications included:
- make success a boolean since there's only 2 values
- make message a single string since we will only use one message per response

IMPORTANT: data must be a dictionary where:
- the key is the name of the type of data
- the value is the data itself
"""
response = {
'success': 200 <= status < 300,
'code': status,
'message': message,
'result': data
}
return jsonify(response), status

"""
~~~~~~~~~~~~ API ~~~~~~~~~~~~
"""

@app.route('/')
def my_first_route():
return "<h1> Hello World! </h1>"

@app.route('/route/<name>')
def my_second_route(name):
return name

@app.route('/get_users', methods=['GET'])
def get_all_users():
if request.method == 'GET':
return jsonify({'status': 'success', 'data': ['aria', 'tim', 'varun', 'alex']})
else:
return jsonify({"status": "failed")

def hello_world():
return create_response('hello world!')

@app.route('/mirror/<name>')
def mirror(name):
data = {
'name': name
}
return create_response(data)

# TODO: Implement the rest of the API here!

@app.route('/users', methods=['GET'])
def users():
if request.method == 'GET':
if request.args.get('team') is None:
data = {
'users':db.get('users')
}
else:
usersOnTeam = [i for i in db.get('users') if i['team'] == request.args.get('team')]
data = {
'users': usersOnTeam
}

return create_response(data)

@app.route('/users', methods=['POST'])
def post_users():
input = request.get_json()
try:
name, age, team = input["name"], input["age"], input ["team"]
except:
return create_response(None,422,"Missing User Information")
entries = {'name': name,
'age': age,
'team': team}

data = db.create('users',entries)
return create_response(data,status=201)

@app.route('/users/<id>', methods = ['GET','PUT','DELETE'])
def userId(id):
if request.method == 'GET':
if db.getById('users',int(id) is None):
return create_response(None, 404, "User can't be found")
else:
data = {
'user': db.getById('users',int(id))
}
return create_response(data)
elif request.method == 'PUT':
entries = {'name': request.form['name'], 'age': request.form['age'], 'team': request.form['team']}
return create_response(db.updateById('users',id,entries))


elif request.method == 'DELETE':
if db.getById('users',int(id)) is None:
return create_response(None,404,"User not found")
else:
return create_response({},200,"User deleted")



"""
~~~~~~~~~~~~ END API ~~~~~~~~~~~~
"""
if __name__ == '__main__':
app.run(debug=True)
app.run(debug=True)
Binary file added docs/postman_get.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/postman_get_mirror.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/postman_post.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/postman_querystring.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/pr1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/pr2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions mockdb/dummy_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
initial_db_state = {
'users': [
{
"id": 1,
"name": "Aria",
"age": 19,
"team": "LWB"
},
{
"id": 2,
"name": "Tim",
"age": 20,
"team": "LWB"
},
{
"id": 3,
"name": "Varun",
"age": 23,
"team": "NNB"
},
{
"id": 4,
"name": "Alex",
"age": 24,
"team": "C2TC"
}
]
}
32 changes: 32 additions & 0 deletions mockdb/mockdb_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from mockdb.dummy_data import initial_db_state
import json

db_state = initial_db_state

def get(type):
return db_state[type]

def getById(type, id):
queried = [i for i in get(type) if i['id'] == id]
if (len(queried)):
return queried[0]
return None

def create(type, payload):
last_id = max([i['id'] for i in get(type)])
new_id = last_id + 1
payload['id'] = new_id
db_state[type].append(payload)
return payload

def updateById(type, id, update_values):
item = getById(type, id)
if not item:
return None
for k, v in update_values.items():
if k is not 'id':
item[k] = v
return item

def deleteById(type, id):
db_state[type] = [i for i in get(type) if i['id'] != id]