2020-07-04 11:41:31 +02:00
|
|
|
from flask import jsonify, g, request
|
2020-01-05 22:48:38 +01:00
|
|
|
|
2020-04-24 14:08:00 +02:00
|
|
|
from app.api.base import api_bp, require_api_auth
|
2020-07-04 11:41:31 +02:00
|
|
|
from app.extensions import db
|
|
|
|
from app.models import ApiKey
|
2020-01-05 22:48:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/user_info")
|
2020-04-24 14:08:00 +02:00
|
|
|
@require_api_auth
|
2020-01-05 22:48:38 +01:00
|
|
|
def user_info():
|
|
|
|
"""
|
|
|
|
Return user info given the api-key
|
|
|
|
"""
|
|
|
|
user = g.user
|
|
|
|
|
2020-03-18 18:34:37 +01:00
|
|
|
return jsonify(
|
2020-03-18 19:08:16 +01:00
|
|
|
{
|
|
|
|
"name": user.name,
|
|
|
|
"is_premium": user.is_premium(),
|
|
|
|
"email": user.email,
|
|
|
|
"in_trial": user.in_trial(),
|
|
|
|
}
|
2020-03-18 18:34:37 +01:00
|
|
|
)
|
2020-07-04 11:41:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.route("/api_key", methods=["POST"])
|
|
|
|
@require_api_auth
|
|
|
|
def create_api_key():
|
|
|
|
"""Used to create a new api key
|
|
|
|
Input:
|
|
|
|
- device
|
|
|
|
|
|
|
|
Output:
|
|
|
|
- api_key
|
|
|
|
"""
|
|
|
|
data = request.get_json()
|
|
|
|
if not data:
|
|
|
|
return jsonify(error="request body cannot be empty"), 400
|
|
|
|
|
|
|
|
device = data.get("device")
|
|
|
|
|
|
|
|
api_key = ApiKey.create(user_id=g.user.id, name=device)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return jsonify(api_key=api_key.code), 201
|