From 2f2a8a3a5ed371c23a96bb12667d6c39d0d0b352 Mon Sep 17 00:00:00 2001 From: Erik Reid <erik.reid@geant.org> Date: Wed, 9 Jan 2019 14:59:47 +0100 Subject: [PATCH] added /classifier/infinera-dna-addresses route --- inventory_provider/routes/classifier.py | 33 +++++++++++++++++++++++++ test/conftest.py | 7 +++++- test/test_classifier_routes.py | 24 ++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 inventory_provider/routes/classifier.py create mode 100644 test/test_classifier_routes.py diff --git a/inventory_provider/routes/classifier.py b/inventory_provider/routes/classifier.py new file mode 100644 index 00000000..794046af --- /dev/null +++ b/inventory_provider/routes/classifier.py @@ -0,0 +1,33 @@ +import functools + +from flask import Blueprint, request, Response, current_app, jsonify + +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 +def infinera_addresses(): + infinera_config = current_app.config[ + "INVENTORY_PROVIDER_CONFIG"]["infinera-dna"] + return jsonify([dna['address'] for dna in infinera_config]) + diff --git a/test/conftest.py b/test/conftest.py index 1b8bf386..0493186f 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -42,7 +42,12 @@ def data_config_filename(tmp_dir_name): "redis": { "hostname": "xxxxxx", "port": 6379 - } + }, + "infinera-dna": [ + {"name": "name1", "address": "123.456.789.0"}, + {"name": "name2", "address": "012.345.678.9"}, + {"name": "name3", "address": "111.222.333.000"} + ] } shutil.copyfile( diff --git a/test/test_classifier_routes.py b/test/test_classifier_routes.py new file mode 100644 index 00000000..b3b0484f --- /dev/null +++ b/test/test_classifier_routes.py @@ -0,0 +1,24 @@ +import json +import jsonschema + +DEFAULT_REQUEST_HEADERS = { + "Content-type": "application/json", + "Accept": ["application/json"] +} + + +def test_version_request(client): + response_schema = { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "array", + "items": {"type": "string"} + } + + rv = client.post( + "/classifier/infinera-dna-addresses", + headers=DEFAULT_REQUEST_HEADERS) + assert rv.status_code == 200 + jsonschema.validate( + json.loads(rv.data.decode("utf-8")), + response_schema) + -- GitLab