Separate services - app, db, and job #1
|
@ -1,6 +1,7 @@
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, jsonify, request
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
import uuid
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tokens.db' # Use SQLite for simplicity
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tokens.db' # Use SQLite for simplicity
|
||||||
|
@ -8,11 +9,13 @@ app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
class Token(db.Model):
|
class Token(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.String(36), primary_key=True, default=str(uuid.uuid4()))
|
||||||
|
email = db.Column(db.String(255), nullable=False)
|
||||||
access_token = db.Column(db.String(255), nullable=False)
|
access_token = db.Column(db.String(255), nullable=False)
|
||||||
refresh_token = db.Column(db.String(255), nullable=False)
|
refresh_token = db.Column(db.String(255), nullable=False)
|
||||||
expiration_seconds = db.Column(db.Integer, nullable=False)
|
expiration_seconds = db.Column(db.Integer, nullable=False)
|
||||||
is_logged_in = db.Column(db.Boolean, default=True)
|
readwise_api_key = db.Column(db.String(255))
|
||||||
|
active = db.Column(db.Boolean, default=True)
|
||||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -26,46 +29,51 @@ with app.app_context():
|
||||||
@app.route('/token', methods=['POST'])
|
@app.route('/token', methods=['POST'])
|
||||||
def create_token():
|
def create_token():
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
|
email = data.get('email')
|
||||||
access_token = data.get('access_token')
|
access_token = data.get('access_token')
|
||||||
refresh_token = data.get('refresh_token')
|
refresh_token = data.get('refresh_token')
|
||||||
expiration_seconds = data.get('expiration_seconds')
|
expiration_seconds = data.get('expiration_seconds')
|
||||||
|
|
||||||
if not access_token or not refresh_token or not expiration_seconds:
|
if not email or access_token or not refresh_token or not expiration_seconds:
|
||||||
return 'Missing required fields', 400
|
return 'Missing required fields', 400
|
||||||
|
|
||||||
new_token = Token(access_token=access_token, refresh_token=refresh_token, expiration_seconds=expiration_seconds)
|
# unique email when active is true
|
||||||
|
existing_token = Token.query.filter_by(email=email, active=True).first()
|
||||||
|
if existing_token:
|
||||||
|
return jsonify({'error': 'An active token with this email already exists'}), 400
|
||||||
|
|
||||||
|
new_token = Token(email=email, access_token=access_token, refresh_token=refresh_token, expiration_seconds=expiration_seconds)
|
||||||
db.session.add(new_token)
|
db.session.add(new_token)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
return ('', 204)
|
return jsonify({'id': new_token.id}), 201
|
||||||
|
|
||||||
# API to get the latest token entry
|
# API to get the token based on the id
|
||||||
@app.route('/token/latest', methods=['GET'])
|
@app.route('/token/<id>', methods=['GET'])
|
||||||
def get_latest_token():
|
def get_token_by_id(id):
|
||||||
latest_token = Token.query.order_by(Token.timestamp.desc()).first()
|
token = Token.query.get_or_404(id)
|
||||||
|
token_info = {
|
||||||
if latest_token:
|
'id': token.id,
|
||||||
token_info = {
|
'email': token.email,
|
||||||
'id': latest_token.id,
|
'access_token': token.access_token,
|
||||||
'access_token': latest_token.access_token,
|
'refresh_token': token.refresh_token,
|
||||||
'refresh_token': latest_token.refresh_token,
|
'expiration_seconds': token.expiration_seconds,
|
||||||
'expiration_seconds': latest_token.expiration_seconds,
|
'readwise_api_key': token.readwise_api_key,
|
||||||
'is_logged_in': latest_token.is_logged_in,
|
'active': token.active,
|
||||||
'timestamp': int(latest_token.timestamp.timestamp())
|
'timestamp': int(token.timestamp.timestamp())
|
||||||
}
|
}
|
||||||
return jsonify({'token': token_info}), 200
|
return jsonify({'token': token_info}), 200
|
||||||
else:
|
|
||||||
return '', 204
|
|
||||||
|
|
||||||
# API to update the token based on the id
|
# API to update the token based on the id
|
||||||
@app.route('/token/<id>', methods=['PUT'])
|
@app.route('/token/<id>', methods=['PUT'])
|
||||||
def update_token(id):
|
def update_token_by_id(id):
|
||||||
token = Token.query.get_or_404(id)
|
token = Token.query.get_or_404(id)
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
token.access_token = data.get('access_token') or token.access_token
|
token.access_token = data.get('access_token', token.access_token)
|
||||||
token.refresh_token = data.get('refresh_token') or token.refresh_token
|
token.refresh_token = data.get('refresh_token', token.refresh_token)
|
||||||
token.expiration_seconds = data.get('expiration_seconds') or token.expiration_seconds
|
token.expiration_seconds = data.get('expiration_seconds', token.expiration_seconds)
|
||||||
token.is_logged_in = data.get('is_logged_in')
|
token.active = data.get('active', token.active)
|
||||||
|
token.readwise_api_key = data.get('readwise_api_key', token.readwise_api_key)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return '', 204
|
return '', 204
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user