diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py index 9f3c35277e1e1f6478888593f0f9402153263f56..0069a77ed758a34560063c3bdbff0c048d48964d 100644 --- a/inventory_provider/routes/classifier.py +++ b/inventory_provider/routes/classifier.py @@ -1,7 +1,6 @@ -import functools - -from flask import Blueprint, request, Response, current_app, jsonify import json + +from flask import Blueprint, Response, current_app, jsonify import jsonschema from inventory_provider.routes import common @@ -9,27 +8,8 @@ from inventory_provider.routes import common routes = Blueprint("inventory-data-classifier-support-routes", __name__) -def require_accepts_json(f): - """ - used as a route handler decorator to return an error - unless the request allows responses with type "application/json" - :param f: the function to be decorated - :return: the decorated function - """ - @functools.wraps(f) - def decorated_function(*args, **kwargs): - # TODO: use best_match to disallow */* ...? - if not request.accept_mimetypes.accept_json: - return Response( - response="response will be json", - status=406, - mimetype="text/html") - return f(*args, **kwargs) - return decorated_function - - @routes.route("/infinera-dna-addresses", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def infinera_addresses(): infinera_config = current_app.config[ "INVENTORY_PROVIDER_CONFIG"]["infinera-dna"] @@ -37,7 +17,7 @@ def infinera_addresses(): @routes.route("/coriant-tnms-addresses", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def coriant_addresses(): coriant_config = current_app.config[ "INVENTORY_PROVIDER_CONFIG"]["coriant-tnms"] @@ -45,7 +25,7 @@ def coriant_addresses(): @routes.route("/juniper-server-addresses", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def juniper_addresses(): backend_data_schema = { "$schema": "http://json-schema.org/draft-07/schema#", @@ -73,7 +53,7 @@ def juniper_addresses(): @routes.route("/trap-metadata/<trap_type>/<source_equipment>/<path:interface>", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def get_trap_metadata(trap_type, source_equipment, interface): # todo - Move this to config interface_info_key = "interface_services" diff --git a/inventory_provider/routes/data.py b/inventory_provider/routes/data.py index ce73e476af52f131cdbf139a6f3c38d7bfebebfd..85fb5f010cdbf51324bcf02d068bb2c36214ac8b 100644 --- a/inventory_provider/routes/data.py +++ b/inventory_provider/routes/data.py @@ -1,8 +1,7 @@ -import functools import json import pkg_resources -from flask import Blueprint, jsonify, request, Response, current_app +from flask import Blueprint, jsonify, Response, current_app from lxml import etree import redis @@ -14,27 +13,8 @@ routes = Blueprint("inventory-data-query-routes", __name__) API_VERSION = '0.1' -def require_accepts_json(f): - """ - used as a route handler decorator to return an error - unless the request allows responses with type "application/json" - :param f: the function to be decorated - :return: the decorated function - """ - @functools.wraps(f) - def decorated_function(*args, **kwargs): - # TODO: use best_match to disallow */* ...? - if not request.accept_mimetypes.accept_json: - return Response( - response="response will be json", - status=406, - mimetype="text/html") - return f(*args, **kwargs) - return decorated_function - - @routes.route("/version", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def version(): return Response( json.dumps({ @@ -47,7 +27,7 @@ def version(): @routes.route("/routers", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def routers(): redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"] r = redis.StrictRedis( @@ -59,7 +39,7 @@ def routers(): @routes.route("/interfaces/<hostname>", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def router_interfaces(hostname): redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"] r = redis.StrictRedis( @@ -87,7 +67,7 @@ def router_interfaces(hostname): @routes.route("/snmp/<hostname>", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def snmp_ids(hostname): redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"] r = redis.StrictRedis( @@ -117,7 +97,7 @@ def snmp_ids(hostname): @routes.route("/debug-dump/<hostname>", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def debug_dump_router_info(hostname): redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"] r = redis.StrictRedis( @@ -138,7 +118,7 @@ def debug_dump_router_info(hostname): @routes.route("/bgp/<hostname>", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def bgp_configs(hostname): redis_config = current_app.config["INVENTORY_PROVIDER_CONFIG"]["redis"] r = redis.StrictRedis( @@ -167,7 +147,7 @@ def bgp_configs(hostname): @routes.route("/interfaces/status/<hostname>/<path:interface>", methods=['GET', 'POST']) -@require_accepts_json +@common.require_accepts_json def interface_statuses(hostname, interface): r = common.get_redis() result = r.hget("interface_statuses", @@ -182,6 +162,7 @@ def interface_statuses(hostname, interface): @routes.route("/services/<hostname>/<path:interface>", methods=['GET', 'POST']) +@common.require_accepts_json def services_for_interface(hostname, interface): r = common.get_redis() result = r.hget("interface_services", diff --git a/inventory_provider/routes/opsdb.py b/inventory_provider/routes/opsdb.py index 84faab3d11dafb639906079c4086949f56e320db..0cb54efb54d0b1a6f8e8959730f819e736cb4c3a 100644 --- a/inventory_provider/routes/opsdb.py +++ b/inventory_provider/routes/opsdb.py @@ -1,7 +1,6 @@ -import functools import json -from flask import Blueprint, request, Response +from flask import Blueprint, Response from inventory_provider.routes import common routes = Blueprint("inventory-opsdb-query-routes", __name__) @@ -13,25 +12,6 @@ service_parent_to_children_key = "parent_to_children_circuit_relations" interface_status_key = "interface_statuses" -def require_accepts_json(f): - """ - used as a route handler decorator to return an error - unless the request allows responses with type "application/json" - :param f: the function to be decorated - :return: the decorated function - """ - @functools.wraps(f) - def decorated_function(*args, **kwargs): - # TODO: use best_match to disallow */* ...? - if not request.accept_mimetypes.accept_json: - return Response( - response="response will be json", - status=406, - mimetype="text/html") - return f(*args, **kwargs) - return decorated_function - - def _decode_utf8_dict(d): return {k.decode('utf8'): json.loads(v) for k, v in d.items()}