Created
October 24, 2018 04:15
-
-
Save jtarang/0e6763d0829a1acbcff01418e89a6772 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask import request | |
from flask import jsonify | |
app = Flask(__name__) | |
# Creates a user | |
class UserObject: | |
def __init__(self, name, username, password): | |
self.user = username | |
self.passwd = password | |
self.name = name | |
# instead of creating a big json object | |
# create a class and it will make it for you | |
# repetitve task | |
def create_user(self): | |
return dict(username=self.user, password=self.passwd, name=self.name) | |
@app.route('/') | |
def hello_world(): | |
return 'This is the main web page but lets just focus on api eh!' | |
@app.route('/login', methods=['POST']) | |
def login(): | |
args = request.json | |
for user in list_of_all_users['users']: | |
if args['user'] == user['username'] and args['passwd'] == user['password']: | |
return jsonify(user) | |
else: | |
return jsonify(error_msg) | |
@app.route('/users', methods=['GET']) | |
def list_users(): | |
return jsonify(list_of_all_users) | |
if __name__ == '__main__': | |
# create users start | |
big_bro = UserObject(name="Paramjit", username="pj123", password="123").create_user() | |
me = UserObject(name="Jasmit", username="jt123", password="123").create_user() | |
# creat users end | |
list_of_all_users = dict(users=[big_bro, me]) # group users in a json object | |
error_msg = {"error":"I don't think this is what you meant to do!"} | |
app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment