{"action":"created","issue":{"url":"https://api.github.com/repos/pallets/flask/issues/2638","repository_url":"https://api.github.com/repos/pallets/flask","labels_url":"https://api.github.com/repos/pallets/flask/issues/2638/labels{/name}","comments_url":"https://api.github.com/repos/pallets/flask/issues/2638/comments","events_url":"https://api.github.com/repos/pallets/flask/issues/2638/events","html_url":"https://github.com/pallets/flask/issues/2638","id":300009961,"number":2638,"title":"How to trigger a REST API based on a condition in Flask?","user":{"login":"Souvikray","id":22216586,"avatar_url":"https://avatars1.githubusercontent.com/u/22216586?v=4","gravatar_id":"","url":"https://api.github.com/users/Souvikray","html_url":"https://github.com/Souvikray","followers_url":"https://api.github.com/users/Souvikray/followers","following_url":"https://api.github.com/users/Souvikray/following{/other_user}","gists_url":"https://api.github.com/users/Souvikray/gists{/gist_id}","starred_url":"https://api.github.com/users/Souvikray/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Souvikray/subscriptions","organizations_url":"https://api.github.com/users/Souvikray/orgs","repos_url":"https://api.github.com/users/Souvikray/repos","events_url":"https://api.github.com/users/Souvikray/events{/privacy}","received_events_url":"https://api.github.com/users/Souvikray/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":5,"created_at":"2018-02-25T08:55:32Z","updated_at":"2018-02-25T09:13:54Z","closed_at":"2018-02-25T08:58:44Z","author_association":"NONE","body":"I have a Flask application where I let users access third party applications and fetch data from them and perform some visualizations.Now the user has to provide the application name and it's credentials in order to fetch the data.Now I want to avoid putting the application name in the url and rather all of the data should be sent as a POST request where I will parse the POST data, connect to the required app with the given credentials and perform some visualizations.This is what the user will send as a POST data\r\n\r\n```json\r\n{\r\n    \"application_name\": \"appdynamics\",\r\n    \"account_id\": \"sdf632sef\",\r\n    \"username\": \"kuhku86tg\",\r\n    \"password\": \"oihsd832\"\r\n}\r\n```\r\n\r\nNow I want to trigger my particular REST API class based on the application name provided by the user.\r\n\r\nThe way I planned was to create a seperate file that involves getting the POST data using request parser and then calling it in the main application where I will trigger my REST API class with a if condition based on the application name.Below is the file parse.py\r\n\r\n```python\r\nfrom flask_restful import reqparse\r\n\r\n# create a parser object\r\nparser = reqparse.RequestParser()\r\n# add agruments to the parser object\r\nparser.add_argument('account_id', type=str, required=False, help=\"Please define 'account_id'\")\r\nparser.add_argument('username', type=str, required=False, help=\"Please define 'username'\")\r\nparser.add_argument('password', type=str, required=False, help=\"Please define 'password'\")\r\nparser.add_argument('application_name', type=str, required=False, help=\"Please define 'application name'\")\r\n\r\ndata = parser.parse_args()\r\n```\r\nNow I call it in the main application app.py\r\n\r\n```python\r\nfrom parser import data\r\nfrom flask import Flask\r\nfrom flask_restful import Api\r\n\r\napp = Flask(__name__)\r\n# create an API for the Flask app\r\napi = Api(app)\r\n\r\n# if the user demands info for appdynamics, trigger the Appdynamics API class\r\nif data['application_name'] == \"appdynamics\":\r\n    api.add_resource(AppdynamicsAPI, \"/<string:name>\")  # the string will contain the metric requirement\r\n\r\nif __name__ == \"__main__\":\r\n    app.run(port=5000, debug=True)\r\n```\r\n\r\nBelow is the section where the logic for the REST API is written\r\n\r\n```python\r\nfrom parser import data\r\nfrom flask_restful import Resource, reqparse\r\nfrom fetch_data.appdynamics import fetch_all_apps, fetch_avg_resp_time, calls_per_min\r\nfrom models.user import *\r\n\r\nclass AppdynamicsAPI(Resource):\r\n    # authenticate users\r\n    def post(self, name):\r\n        first_data = data\r\n        # if the user passes the credentials, insert it into the database otherwise use the last known credentials\r\n        # ensure you only insert valid credentials\r\n        if all([first_data['account_id'], first_data['password'], first_data['username']]):\r\n            users.update(first_data, {i: j for i, j in first_data.items()}, upsert=True)\r\n            print({i: j for i, j in first_data.items()})\r\n        credentials = users.find_one({})\r\n        print(\"Credentials\", credentials)\r\n        account_id = credentials['account_id']\r\n        username = credentials['username']\r\n        password = credentials['password']\r\n        t_duration = first_data['t_duration']\r\n\r\n\r\n        if name == \"allapps\":\r\n            status_code, result = fetch_all_apps(account_id, username, password)\r\n            if status_code == 200:\r\n                return {\"information\": result}, status_code\r\n            return {\"message\": \"Please enter correct credentials\"}, status_code\r\n```\r\n\r\nHowever I receive the below error\r\n\r\n```pythontb\r\nTraceback (most recent call last):\r\n  File \"/home/souvik/PycharmProjects/ServiceHandler/app.py\", line 3, in <module>\r\n    from resource.appdynamics_resource import AppdynamicsAPI\r\n  File \"/home/souvik/PycharmProjects/ServiceHandler/resource/appdynamics_resource.py\", line 4, in <module>\r\n    from authentication.parser import data\r\n  File \"/home/souvik/PycharmProjects/ServiceHandler/authentication/parser.py\", line 14, in <module>\r\n    data = parser.parse_args()\r\n  File \"/home/souvik/utorapp/lib/python3.5/site-packages/flask_restful/reqparse.py\", line 302, in parse_args\r\n    req.unparsed_arguments = dict(self.argument_class('').source(req)) if strict else {}\r\n  File \"/home/souvik/utorapp/lib/python3.5/site-packages/werkzeug/local.py\", line 364, in <lambda>\r\n    __setattr__ = lambda x, n, v: setattr(x._get_current_object(), n, v)\r\n  File \"/home/souvik/utorapp/lib/python3.5/site-packages/werkzeug/local.py\", line 306, in _get_current_object\r\n    return self.__local()\r\n  File \"/home/souvik/utorapp/lib/python3.5/site-packages/flask/globals.py\", line 37, in _lookup_req_object\r\n    raise RuntimeError(_request_ctx_err_msg)\r\nRuntimeError: Working outside of request context.\r\n\r\nThis typically means that you attempted to use functionality that needed\r\nan active HTTP request.  Consult the documentation on testing for\r\ninformation about how to avoid this problem.\r\n```"},"comment":{"url":"https://api.github.com/repos/pallets/flask/issues/comments/368294536","html_url":"https://github.com/pallets/flask/issues/2638#issuecomment-368294536","issue_url":"https://api.github.com/repos/pallets/flask/issues/2638","id":368294536,"user":{"login":"ThiefMaster","id":179599,"avatar_url":"https://avatars1.githubusercontent.com/u/179599?v=4","gravatar_id":"","url":"https://api.github.com/users/ThiefMaster","html_url":"https://github.com/ThiefMaster","followers_url":"https://api.github.com/users/ThiefMaster/followers","following_url":"https://api.github.com/users/ThiefMaster/following{/other_user}","gists_url":"https://api.github.com/users/ThiefMaster/gists{/gist_id}","starred_url":"https://api.github.com/users/ThiefMaster/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ThiefMaster/subscriptions","organizations_url":"https://api.github.com/users/ThiefMaster/orgs","repos_url":"https://api.github.com/users/ThiefMaster/repos","events_url":"https://api.github.com/users/ThiefMaster/events{/privacy}","received_events_url":"https://api.github.com/users/ThiefMaster/received_events","type":"User","site_admin":false},"created_at":"2018-02-25T09:13:54Z","updated_at":"2018-02-25T09:13:54Z","author_association":"OWNER","body":"Oh, and if `oihsd832` is your real password, then you need to change it **RIGHT NOW**."}}
{"actor":{"display_login":"ThiefMaster"}}