def get_covid_data(): cp = cache.get('covid-protocols') if cp is not None: logging.info("Cached result") return cp else: data_provider = DataProviderService(db_engine) protocols_list = data_provider.get_covid_protocols() data_provider.close_connection() if protocols_list: data = { "covid-protocols": protocols_list, "total": len(protocols_list) } cp = jsonify(data) cache.set('covid-protocols', cp, timeout=int(os.environ.get('timeout', 0))) response = make_response(cp, 200) response.headers["Cache-Control"] = "private" return response else: # # In case we did not find any protocols i.e the server is down # we send HTTP 404 - Not Found error to the client # abort(404)
def log_details(username, validated, op_type): call_time = datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d%H:%M:%S') ip_address = request.remote_addr data_provider = DataProviderService(db_engine) log_details_id = data_provider.log_details(username, ip_address, call_time, validated, op_type) data_provider.close_connection() return log_details_id
from flask import jsonify from flask import abort from flask import make_response from flask import request from flask import url_for from math import ceil from data_provider_service import DataProviderService db_engine = 'mysql+mysqldb://packtuser:secret@localhost/packtwebapi?unix_socket=/opt/lampp/var/mysql/mysql.sock' PAGE_SIZE = 2 DATA_PROVIDER = DataProviderService(db_engine) def candidate(serialize = True): candidates = DATA_PROVIDER.get_candidate(serialize=True) page = request.args.get("page") if page: nr_of_pages = int(ceil(float(len(candidates)) / PAGE_SIZE)) converted_page = int(page) if converted_page > nr_of_pages or converted_page < 0: return make_response("", 404) from_idx = converted_page * PAGE_SIZE - 1 stop_idx = from_idx + PAGE_SIZE
from flask import Flask from flask import jsonify from flask import url_for from flask import abort from flask import make_response from flask import request # Import the service which servers the data. # This could be a service which loads the data # from database or files or some website. from data_provider_service import DataProviderService DATA_PROVIDER = DataProviderService(15) # create the Flask application app = Flask(__name__) # ROUTING: # One way to configure routing is to use the @app.route() decorator # @app.route("/api", methods=["GET"]) def list_routes(): result = [] for rt in app.url_map.iter_rules(): result.append({"methods": list(rt.methods), "route": str(rt)}) return jsonify({"routes": result, "total": len(result)}) def candidate(): candidates = DATA_PROVIDER.get_candidates()
''' Created on Mar 21, 2018 @author: ishank ''' import json, requests from twilio.rest import Client from flask import Flask, request, jsonify, render_template from flask_cors import CORS from twilio.twiml.messaging_response import MessagingResponse from data_provider_service import DataProviderService appJson = json.load(open('app.json')) dataProvider = DataProviderService() app = Flask(__name__) CORS(app) client = Client(appJson['msgConfig']['account_sid'], appJson['msgConfig']['auth_token']) responseMessages = dataProvider.getMsgTemplates() def analyzeSentiment(message): sentiment_api_url = appJson['textAnalyticsConfig']['sentiment_api_url'] documents = {'documents' : [ {'id': '1', 'language': 'en', 'text': message} ]} headers = {"Ocp-Apim-Subscription-Key": appJson['textAnalyticsConfig']['subscription_key']} response = requests.post(sentiment_api_url, headers=headers, json=documents) sentiments = response.json() return sentiments['documents'][0]['score']