import os
from flask import Flask, request, jsonify, make_response
from flask_cors import CORS
from joinnector import SDK

# through the custom helperclient
from src.client.nector_client import NectorClient
client_sdk = NectorClient(os.environ.get("API_KEY"), os.environ.get(
    "API_SECRET"), os.environ.get("API_MODE"))


# through the sdk helper client
sdk = SDK(os.environ.get("API_KEY"), os.environ.get(
    "API_SECRET"), os.environ.get("API_MODE"))
delegate_client = SDK.get_delegate_client()

'''
For security purpose these methods can not be triggered from client calls
To whitelist calls directly from client side, remove the method name from the array
It is requested to call the "not_allowed_controller_method_names" only from other backend functions (idealy they should be called while performing business operations) since they cause quota consumption on nector.
not_allowed_controller_method_names = [
	"reward_deals", "create_leads", "save_leads", "get_subscriptions",
	"create_taskactivities", "create_wallets", "create_wallettransactions"
];

whitelist all the methods by default methods in not_allowed_controller_method_names are blocklisted to be called from frontend app or website directly for security reasons
'''
delegatesdk = delegate_client(sdk, [])


app = Flask(__name__)
CORS(app)


def make_json_response(json_data, status=200):
    response = make_response(
        jsonify(json_data),
        status
    )
    response.headers["Content-Type"] = "application/json"
    return response


@app.route('/', methods=['GET'])
def health():
    return make_json_response({"message": "Server is running"})


@app.route('/nector-delegate', methods=['POST'])
def delegate():
    try:
        response = client_sdk.delegate_method()
        if response.json() is not None:
            return make_json_response(response.json(), response.status_code)
    except Exception as ex:
        print(ex)
    return make_json_response({"message": "Something went wrong, please try after sometime"}, 422)


@app.route('/nector-direct-delegate', methods=['POST'])
def direct_delegate():
    try:
        response = delegatesdk.delegate_method(request.get_json())
        if response.json() is not None:
            return make_json_response(response.json(), response.status_code)
    except Exception as ex:
        print(ex)
    return make_json_response({"message": "Something went wrong, please try after sometime"}, 422)

import os
from flask import Flask, request, jsonify, make_response
from flask_cors import CORS
from joinnector import SDK

# through the custom helperclient
from src.client.nector_client import NectorClient
client_sdk = NectorClient(os.environ.get("API_KEY"), os.environ.get(
    "API_SECRET"), os.environ.get("API_MODE"))


# through the sdk helper client
sdk = SDK(os.environ.get("API_KEY"), os.environ.get(
    "API_SECRET"), os.environ.get("API_MODE"))
delegate_client = SDK.get_delegate_client()

'''
For security purpose these methods can not be triggered from client calls
To whitelist calls directly from client side, remove the method name from the array
It is requested to call the "not_allowed_controller_method_names" only from other backend functions (idealy they should be called while performing business operations) since they cause quota consumption on nector.
not_allowed_controller_method_names = [
	"reward_deals", "create_leads", "save_leads", "get_subscriptions",
	"create_taskactivities", "create_wallets", "create_wallettransactions"
];

whitelist all the methods by default methods in not_allowed_controller_method_names are blocklisted to be called from frontend app or website directly for security reasons
'''
delegatesdk = delegate_client(sdk, [])


app = Flask(__name__)
CORS(app)


def make_json_response(json_data, status=200):
    response = make_response(
        jsonify(json_data),
        status
    )
    response.headers["Content-Type"] = "application/json"
    return response


@app.route('/', methods=['GET'])
def health():
    return make_json_response({"message": "Server is running"})


@app.route('/nector-delegate', methods=['POST'])
def delegate():
    try:
        response = client_sdk.delegate_method()
        if response.json() is not None:
            return make_json_response(response.json(), response.status_code)
    except Exception as ex:
        print(ex)
    return make_json_response({"message": "Something went wrong, please try after sometime"}, 422)


@app.route('/nector-direct-delegate', methods=['POST'])
def direct_delegate():
    try:
        response = delegatesdk.delegate_method(request.get_json())
        if response.json() is not None:
            return make_json_response(response.json(), response.status_code)
    except Exception as ex:
        print(ex)
    return make_json_response({"message": "Something went wrong, please try after sometime"}, 422)

# through the custom helperclient # through the sdk helper client For security purpose these methods can not be triggered from client calls To whitelist calls directly from client side, remove the method name from the array It is requested to call the "not_allowed_controller_method_names" only from other backend functions (idealy they should be called while performing business operations) since they cause quota consumption on nector. not_allowed_controller_method_names = [ 	"reward_deals", "create_leads", "save_leads", "get_subscriptions", 	"create_taskactivities", "create_wallets", "create_wallettransactions" ];  whitelist all the methods by default methods in not_allowed_controller_method_names are blocklisted to be called from frontend app or website directly for security reasons