-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from user-cube/FlaskAPI
Flask API
- Loading branch information
Showing
13 changed files
with
602 additions
and
4 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.env | ||
venv/ | ||
.idea/ | ||
.idea/ | ||
.cert |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,57 @@ | ||
from flask import Flask | ||
from flask import Flask, request, render_template, jsonify | ||
from flask_cors import CORS | ||
from flask_jwt_extended import JWTManager, jwt_required, get_raw_jwt | ||
from views import schema_blueprint | ||
from views import db | ||
|
||
from settings import CERT | ||
|
||
import jwt as tokenizer | ||
import os | ||
import requests | ||
from dotenv import load_dotenv | ||
|
||
app = Flask(__name__) | ||
app.config.from_object('settings') | ||
|
||
cors = CORS(app, resources={r"/*": {"origins": "*"}}) | ||
jwt = JWTManager(app) | ||
app.config.update(JWT=jwt) | ||
|
||
app.register_blueprint(schema_blueprint) | ||
|
||
db.init_app(app) | ||
|
||
|
||
@app.route('/') | ||
@app.before_first_request | ||
def create_database(): | ||
db.create_all() | ||
|
||
@app.route('/ola') | ||
@jwt_required | ||
def hello_world(): | ||
token = request.headers["Authorization"].split()[1] # Split Bearer from token | ||
return 'Hello World!' | ||
|
||
@app.route('/node/<nodeID>') | ||
def node_info(nodeID): | ||
if nodeID=='1': | ||
r = requests.get('http://192.168.1.141:5000/testi') | ||
if r.status_code != 200: | ||
msg = {'msg': 'Erro 200'} | ||
return msg | ||
return jsonify(r.json()) | ||
|
||
if nodeID=='2': | ||
r = requests.get('http://192.168.1.142:5000/testi') | ||
if r.status_code != 200: | ||
msg = {'msg': 'Erro 200'} | ||
return msg | ||
return jsonify(r.json()) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
app.run() | ||
app.run(host=app.config['HOST'], | ||
port=app.config['PORT'], | ||
debug=app.config['DEBUG']) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
from flask_sqlalchemy import SQLAlchemy | ||
from marshmallow import Schema | ||
|
||
db = SQLAlchemy() | ||
session = None | ||
class CRUD: | ||
def add(self, resource): | ||
db.session.add(resource) | ||
return db.session.commit() | ||
|
||
def update(self): | ||
return db.session.commit() | ||
|
||
def delete(self, resource): | ||
db.session.delete(resource) | ||
return db.session.commit() | ||
|
||
|
||
class Role (db.Model, CRUD): | ||
|
||
__tablename__ = 'role' | ||
id = db.Column(db.Integer, primary_key=True) | ||
role_name = db.Column(db.String(20)) | ||
|
||
def __init__(self, role_name): | ||
self.role_name = role_name | ||
|
||
@property | ||
def serializable(self): | ||
return { | ||
'id': self.id, | ||
'role_name': self.role_name, | ||
} | ||
|
||
|
||
class Profile(db.Model, CRUD): | ||
|
||
__tablename__ = 'profile' | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.String(80), nullable=False) | ||
email = db.Column(db.String(200), nullable=False, unique=True) | ||
num_test = db.Column(db.INTEGER, nullable=True) | ||
register_date = db.Column(db.TIMESTAMP, nullable=False) | ||
picture = db.Column(db.String(10485760), nullable=True) | ||
last_login = db.Column(db.TIMESTAMP, nullable=True) | ||
role = db.Column(db.Integer, db.ForeignKey('role.id')) | ||
|
||
def __init__(self, name, email, register_date, role, num_test=None, picture=None, last_login=None): | ||
self.name = name | ||
self.email = email | ||
self.num_test = num_test | ||
self.register_date = register_date | ||
self.picture = picture | ||
self.last_login = last_login | ||
self.role = role | ||
|
||
def __repr__(self): | ||
return '<user: {}, Email: {}, Role: {}'.format(self.name, self.email, self.role) | ||
|
||
@property | ||
def serializable(self): | ||
return { | ||
"id": self.id, | ||
"name": self.name, | ||
"email": self.email, | ||
"num_test": self.num_test, | ||
"register_date": self.register_date.strftime("%Y-%m-%d %H:%M:%S"), | ||
"picture": self.picture, | ||
"last_login": self.last_login, | ||
"role": self.role | ||
} | ||
|
||
|
||
class Template(db.Model, CRUD): | ||
|
||
__tablename__ = 'template' | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.String(80)) | ||
duration = db.Column(db.BIGINT) | ||
profile = db.Column(db.Integer, db.ForeignKey('profile.id')) | ||
|
||
def __init__(self, id, name, duration, profile): | ||
self.id = id | ||
self.name = name | ||
self.duration = duration | ||
self.profile = profile | ||
|
||
@property | ||
def serializable(self): | ||
return { | ||
"id": self.id, | ||
"name": self.name, | ||
"duration": self.duration, | ||
"profile": self.profile | ||
} | ||
|
||
|
||
class Experience(db.Model, CRUD): | ||
|
||
__tablename__ = 'experience' | ||
id = db.Column(db.Integer, primary_key=True) | ||
name = db.Column(db.String(100), nullable=False) | ||
begin_date = db.Column(db.TIMESTAMP, nullable=False) | ||
end_date = db.Column(db.TIMESTAMP, nullable=False) | ||
num_test = db.Column(db.Integer, nullable=False) | ||
register_date = db.Column(db.TIMESTAMP, nullable=False) | ||
status = db.Column(db.String(20), nullable=False) | ||
profile = db.Column(db.Integer, db.ForeignKey('profile.id')) | ||
template = db.Column(db.Integer, db.ForeignKey('template.id')) | ||
|
||
def __init__(self, name, begin_date, end_date, num_test, register_date, status, profile, template): | ||
self.name = name | ||
self.begin_date = begin_date | ||
self.end_date = end_date | ||
self.num_test = num_test | ||
self.register_date = register_date | ||
self.status = status | ||
self.profile = profile | ||
self.template = template | ||
|
||
@property | ||
def serializable(self): | ||
return { | ||
"id": self.id, | ||
"name": self.name, | ||
"begin_date": self.begin_date.strftime("%Y-%m-%d %H:%M:%S"), | ||
"end_date": self.end_date.strftime("%Y-%m-%d %H:%M:%S"), | ||
"num_test": self.num_test, | ||
"register_date": self.register_date.strftime("%Y-%m-%d %H:%M:%S"), | ||
"status": self.status, | ||
"profile": self.profile, | ||
"template": self.template | ||
} | ||
|
||
class APU(db.Model, CRUD): | ||
|
||
__tablename__ = 'apu' | ||
id = db.Column(db.Integer, primary_key=True) | ||
|
||
def __init__(self, id): | ||
self.id = id | ||
|
||
@property | ||
def serializable(self): | ||
return { | ||
"id":self.id | ||
} | ||
|
||
|
||
class APUConfig(db.Model, CRUD): | ||
|
||
__tablename__ = 'apuconfig' | ||
id = db.Column(db.Integer, primary_key=True) | ||
apu = db.Column(db.Integer, db.ForeignKey('apuconfig.id')) | ||
ip = db.Column(db.String(40)) | ||
protocol = db.Column(db.String(50)) | ||
base_template = db.Column(db.Integer) | ||
|
||
def __init__(self, apu, ip, protocol, base_template): | ||
self.apu = apu | ||
self.ip = ip | ||
self.protocol = protocol | ||
self.base_template = base_template | ||
|
||
def serializable(self): | ||
return { | ||
"id": self.id, | ||
"apu": self.apu, | ||
"ip": self.ip, | ||
"protocol": self.protocol, | ||
"base_template": self.base_template | ||
} | ||
|
||
class APUConfig_Template(db.Model, CRUD): | ||
|
||
__tablename__ = 'apuconfig_template' | ||
id_nonexistent = db.Column(db.Integer, primary_key=True) | ||
apu_config = db.Column(db.Integer, db.ForeignKey('apuconfig.id')) | ||
template = db.Column(db.Integer, db.ForeignKey('template.id')) | ||
|
||
def __init__(self, apu_config, template): | ||
self.apu_config = apu_config | ||
self.template = template | ||
|
||
@property | ||
def serializable(self): | ||
return { | ||
"apu_config": self.apu_config, | ||
"template": self.template | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Flask==1.1.2 | ||
Flask-RESTful==0.3.8 | ||
flask-jwt-extended== 3.24.1 | ||
flask_api==2.0 | ||
marshmallow==3.5.1 | ||
SQLAlchemy==1.3.16 | ||
Flask-Cors==3.0.8 | ||
python-dotenv==0.13.0 | ||
pyjwt[crypto]==1.7.1 | ||
requests==2.23.0 | ||
Flask-SQLAlchemy==2.4.1 | ||
psycopg2==2.8.5 | ||
PyJWT==1.7.1 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import os | ||
|
||
pg_db_hostname = os.getenv('DB_HOST') | ||
pg_db_name = os.getenv('DB_NAME') | ||
pg_db_password = os.getenv('DB_PASS') | ||
pg_db_username = os.getenv('DB_USER') | ||
|
||
DEBUG = True | ||
PORT = 5000 | ||
HOST = "127.0.0.1" | ||
SQLALCHEMY_ECHO = True | ||
SQLALCHEMY_TRACK_MODIFICATIONS = True | ||
SECRET_KEY = "SOME SECRET" | ||
# PostgreSQL | ||
SQLALCHEMY_DATABASE_URI = "postgresql://{DB_USER}:{DB_PASS}@{DB_ADDR}/{DB_NAME}".format(DB_USER=pg_db_username, | ||
DB_PASS=pg_db_password, | ||
DB_ADDR=pg_db_hostname, | ||
DB_NAME=pg_db_name) | ||
|
||
|
||
SQLALCHEMY_TRACK_MODIFICATONS = False | ||
|
||
|
||
|
||
CERT = b'-----BEGIN PUBLIC KEY-----\n' \ | ||
b'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlc6LMgsz5b2m2Q3M/ps2\n' \ | ||
b'XRKIuBRdwOUrY532F9OmkYrrdPsVpDpWTjRsc3Srrc9hUCIWNMQa++Cjq4yMFTHl\n' \ | ||
b'D3Yn8x1kHJnO+DWw7sIJvg1+8PxZWZnslXUHjFRuuxUNUH5wxy5z/C1T6aMqIO93\n' \ | ||
b'tXJty1q2nzVdW9GON0AI0oPHhSdPJalbxC7mo1ExZRa5SoYiBv8xe7ER4e1Neb3K\n' \ | ||
b'sUF+Rfny1t79PQJC6uk0FwnloEQVj5yYvmwAv8HTda0mhFY0GdYqNk5+ks0D3hGg\n' \ | ||
b'3D7FspI98MOX1lUatEJDq6/xE0JlK111uh24i7aZaZD5Bn3dqZl83zK4PdexXE/z\n' \ | ||
b'JwIDAQAB\n' \ | ||
b'-----END PUBLIC KEY-----' | ||
|
||
# JWT Decode Algorithm | ||
JWT_SECRET_KEY = CERT | ||
|
||
JWT_DECODE_ALGORITHMS = ['RS256'] | ||
# JWT Decode CERT | ||
|
||
# JWT Identifier | ||
JWT_IDENTITY_CLAIM = 'email' | ||
|
||
|
Oops, something went wrong.