Last active
May 5, 2020 13:28
-
-
Save thomaxxl/8558210e3064cc205f290259493292fb to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env python | |
# run: | |
# $ FLASK_APP=mini_app flask run | |
from flask import Flask | |
from flask_sqlalchemy import SQLAlchemy | |
from safrs import SAFRSBase, SAFRSAPI, jsonapi_rpc | |
import datetime | |
db = SQLAlchemy() | |
class User(SAFRSBase, db.Model): | |
""" | |
description: User description | |
""" | |
__tablename__ = "Users" | |
id = db.Column(db.Integer, primary_key=True) | |
name = db.Column(db.String) | |
email = db.Column(db.String) | |
@classmethod | |
@jsonapi_rpc(http_methods=['POST','GET']) | |
def get_dept_info(cls, *args, **kwargs): | |
""" | |
description: Find different type of dept present in employee_info table | |
parameters: | |
- name : my_query_string_param | |
default : my_value | |
args: | |
some_arg: test val | |
""" | |
print(args) | |
print(kwargs) | |
return { "result" : "OK" } | |
try: | |
with get_session() as session: | |
# employee_info | |
dept_details_list = session.query(EmployeeInfo.dept_id, EmployeeInfo.dept_name).distinct().all() | |
if dept_details_list: | |
# Adding an option of 'Other' for 'dept_name'. | |
# This option will be used for scenarios where visitors does not know about | |
# department of employee (contact person) | |
dept_details_list.append({'dept_name': 'Other', 'dept_id': 999}) | |
dept_details_schema_list = DeptDetailsSchema(many=True) | |
response = dept_details_schema_list.dump(dept_details_list) # python object [List, Dictionary] | |
# response = users_schema.dumps(all_users) # json encoded string object | |
return make_response(jsonify(response), 200) | |
else: | |
message = 'No departments found in EmployeeInfo table' | |
return make_400_response(message, 400) | |
except Exception as e: | |
print(e) | |
message = 'Exception Occurred : Failed to fetch department details' | |
message = utils.generate_exception_message(message, e) | |
return make_400_response(message, 400) | |
def create_api(app, HOST="localhost", PORT=5000, API_PREFIX=""): | |
api = SAFRSAPI(app, host=HOST, port=PORT, prefix=API_PREFIX) | |
api.expose_object(User) | |
user = User(name="test", email="email@x.org") | |
print("Starting API: http://{}:{}/{}".format(HOST, PORT, API_PREFIX)) | |
def create_app(config_filename=None, host="localhost"): | |
app = Flask("demo_app") | |
app.config.update(SQLALCHEMY_DATABASE_URI="sqlite://") | |
db.init_app(app) | |
with app.app_context(): | |
db.create_all() | |
create_api(app, host) | |
return app | |
host = "192.168.235.136" | |
app = create_app(host=host) | |
if __name__ == "__main__": | |
app.run(host=host) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment