feat(Users): Implemented Kratos CRUD
This commit is contained in:
parent
a208b5f441
commit
a81d14b4f8
10 changed files with 99 additions and 48 deletions
38
api/users.py
38
api/users.py
|
@ -1,38 +0,0 @@
|
||||||
from flask import jsonify
|
|
||||||
from flask_jwt_extended import jwt_required
|
|
||||||
from flask_cors import cross_origin
|
|
||||||
|
|
||||||
from . import api_v1
|
|
||||||
|
|
||||||
|
|
||||||
USER_DATA = [
|
|
||||||
{"id": 1, "email": "john@doe.com", "name": "John Doe", "status": "active", "last_login": "2021-08-03T07:40:51+00:00"}
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
@api_v1.route('/users', methods=['GET'])
|
|
||||||
@jwt_required()
|
|
||||||
@cross_origin()
|
|
||||||
def get_users():
|
|
||||||
return jsonify(USER_DATA)
|
|
||||||
|
|
||||||
|
|
||||||
@api_v1.route('/users', methods=['POST'])
|
|
||||||
@jwt_required()
|
|
||||||
@cross_origin()
|
|
||||||
def post_user():
|
|
||||||
return jsonify(USER_DATA), 201
|
|
||||||
|
|
||||||
|
|
||||||
@api_v1.route('/users/<int:id>', methods=['PUT'])
|
|
||||||
@jwt_required()
|
|
||||||
@cross_origin()
|
|
||||||
def put_user(id):
|
|
||||||
return jsonify(USER_DATA)
|
|
||||||
|
|
||||||
|
|
||||||
@api_v1.route('/users/<int:id>', methods=['DELETE'])
|
|
||||||
@jwt_required()
|
|
||||||
@cross_origin()
|
|
||||||
def delete_user(id):
|
|
||||||
return jsonify(USER_DATA)
|
|
13
app.py
13
app.py
|
@ -1,12 +1,14 @@
|
||||||
from flask import Flask, jsonify
|
from flask import Flask, jsonify
|
||||||
from flask_jwt_extended import JWTManager
|
from flask_jwt_extended import JWTManager
|
||||||
from flask_cors import CORS
|
from flask_cors import CORS
|
||||||
import requests
|
|
||||||
|
|
||||||
|
from areas import api_v1
|
||||||
|
# There imports are required
|
||||||
|
from areas import users
|
||||||
|
from areas import apps
|
||||||
|
from areas import auth
|
||||||
from config import *
|
from config import *
|
||||||
|
|
||||||
from api import api_v1, auth, users, apps
|
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
cors = CORS(app)
|
cors = CORS(app)
|
||||||
app.config['SECRET_KEY'] = SECRET_KEY
|
app.config['SECRET_KEY'] = SECRET_KEY
|
||||||
|
@ -26,8 +28,3 @@ def expired_token_callback(*args):
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
return 'Open App Stack API v1.0'
|
return 'Open App Stack API v1.0'
|
||||||
|
|
||||||
@app.route('/hello')
|
|
||||||
def hello():
|
|
||||||
requests.get('{}/health/ready'.format(KRATOS_URL))
|
|
||||||
return 'Open App Stack API v1.0'
|
|
||||||
|
|
1
areas/apps/__init__.py
Normal file
1
areas/apps/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .apps import *
|
|
@ -2,7 +2,7 @@ from flask import jsonify
|
||||||
from flask_jwt_extended import jwt_required
|
from flask_jwt_extended import jwt_required
|
||||||
from flask_cors import cross_origin
|
from flask_cors import cross_origin
|
||||||
|
|
||||||
from . import api_v1
|
from areas import api_v1
|
||||||
|
|
||||||
CONFIG_DATA = [
|
CONFIG_DATA = [
|
||||||
{
|
{
|
1
areas/auth/__init__.py
Normal file
1
areas/auth/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .auth import *
|
|
@ -2,7 +2,7 @@ from flask import request, jsonify
|
||||||
from flask_jwt_extended import create_access_token
|
from flask_jwt_extended import create_access_token
|
||||||
from flask_cors import cross_origin
|
from flask_cors import cross_origin
|
||||||
|
|
||||||
from . import api_v1
|
from areas import api_v1
|
||||||
|
|
||||||
USERNAME = 'admin'
|
USERNAME = 'admin'
|
||||||
PASSWORD = 'admin'
|
PASSWORD = 'admin'
|
1
areas/users/__init__.py
Normal file
1
areas/users/__init__.py
Normal file
|
@ -0,0 +1 @@
|
||||||
|
from .users import *
|
57
areas/users/users.py
Normal file
57
areas/users/users.py
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
from flask import jsonify, request
|
||||||
|
from flask_jwt_extended import jwt_required
|
||||||
|
from flask_cors import cross_origin
|
||||||
|
|
||||||
|
from areas import api_v1
|
||||||
|
from helpers.kratos_api import KratosApi
|
||||||
|
|
||||||
|
|
||||||
|
@api_v1.route('/users', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
@cross_origin()
|
||||||
|
def get_users():
|
||||||
|
res = KratosApi.get('/identities')
|
||||||
|
return jsonify(res.json())
|
||||||
|
|
||||||
|
@api_v1.route('/users/<string:id>', methods=['GET'])
|
||||||
|
@jwt_required()
|
||||||
|
@cross_origin()
|
||||||
|
def get_user(id):
|
||||||
|
res = KratosApi.get('/identities/{}'.format(id))
|
||||||
|
return jsonify(res.json())
|
||||||
|
|
||||||
|
|
||||||
|
@api_v1.route('/users', methods=['POST'])
|
||||||
|
@jwt_required()
|
||||||
|
@cross_origin()
|
||||||
|
def post_user():
|
||||||
|
data = request.get_json()
|
||||||
|
kratos_data = {
|
||||||
|
"schema_id": "default",
|
||||||
|
"traits": data
|
||||||
|
}
|
||||||
|
res = KratosApi.post('/identities', kratos_data)
|
||||||
|
return jsonify(res.json()), res.status_code
|
||||||
|
|
||||||
|
|
||||||
|
@api_v1.route('/users/<string:id>', methods=['PUT'])
|
||||||
|
@jwt_required()
|
||||||
|
@cross_origin()
|
||||||
|
def put_user(id):
|
||||||
|
data = request.get_json()
|
||||||
|
kratos_data = {
|
||||||
|
"schema_id": "default",
|
||||||
|
"traits": data
|
||||||
|
}
|
||||||
|
res = KratosApi.put('/identities/{}'.format(id), kratos_data)
|
||||||
|
return jsonify(res.json()), res.status_code
|
||||||
|
|
||||||
|
|
||||||
|
@api_v1.route('/users/<string:id>', methods=['DELETE'])
|
||||||
|
@jwt_required()
|
||||||
|
@cross_origin()
|
||||||
|
def delete_user(id):
|
||||||
|
res = KratosApi.delete('/identities/{}'.format(id))
|
||||||
|
if (res.status_code == 204):
|
||||||
|
return jsonify(), res.status_code
|
||||||
|
return jsonify(res.json()), res.status_code
|
32
helpers/kratos_api.py
Normal file
32
helpers/kratos_api.py
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import requests
|
||||||
|
|
||||||
|
from config import *
|
||||||
|
|
||||||
|
class KratosApi():
|
||||||
|
@staticmethod
|
||||||
|
def get(url):
|
||||||
|
try:
|
||||||
|
return requests.get('{}{}'.format(KRATOS_URL, url))
|
||||||
|
except:
|
||||||
|
return "Failed to contact Kratos"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def post(url, data):
|
||||||
|
try:
|
||||||
|
return requests.post('{}{}'.format(KRATOS_URL, url), json=data)
|
||||||
|
except:
|
||||||
|
return "Failed to contact Kratos"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def put(url, data):
|
||||||
|
try:
|
||||||
|
return requests.put('{}{}'.format(KRATOS_URL, url), json=data)
|
||||||
|
except:
|
||||||
|
return "Failed to contact Kratos"
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def delete(url):
|
||||||
|
try:
|
||||||
|
return requests.delete('{}{}'.format(KRATOS_URL, url))
|
||||||
|
except:
|
||||||
|
return "Failed to contact Kratos"
|
Loading…
Reference in a new issue