Untitled
unknown
python
4 years ago
1.8 kB
15
Indexable
import re
from flask import Flask, json, request
from werkzeug.user_agent import UserAgent
app = Flask(__name__)
#note- We are parsing everything from our data (database) and retutning it to the user
global users
users = [
{"name": "rosie",
"age": 22,
"brands_of_interest": [
{"name": "hermes"},
{"name": "chanel"}
]
}
]
#/POST a user
@app.route('/users', methods=['POST'])
def create_user():
#parsing the parameters provided by the user
response = request.get_json()
new_user = {
"name": response['name'],
"age": response['age'],
"brands_of_interest": [] #this is empty because we are not trying to add brands of interests first
}
users.append(new_user)
return json.jsonify(new_user)
#/POST brand new of interests
@app.route('/users/<string:name>/brands_of_interest', methods=['POST'])
def create_user_brand_of_interests(name):
#parsing the parameters provided by the user
response = request.get_json()
#check to see if the user exists
for user in users:
if user['name'] == name:
#if user exists, add a new brand of interest for that user
new_brands = {
"name": response['name']
}
users['brands_of_interest'].append(new_brands)
return json.jsonify(new_brands)
return json.jsonify({"message": "this user is not found"})
#/GET a user by ID
@app.route('/users/<string:name>')
def get_user_by_name(name):
#check to see if the user exists
for user in users:
if user['name'] == name:
return json.jsonify(users)
return json.jsonify({"message": "user not found.."})
#/GET all users
@app.route('/users')
def get_all_users():
return json.jsonify({"users": users})
app.run(port=5000)Editor is loading...