From 1f2bddf2f56668e75c1cbd046a2994738da7c938 Mon Sep 17 00:00:00 2001 From: David Eder Date: Thu, 17 Jun 2021 19:05:26 +0200 Subject: [PATCH] Add API documentation for other resources on xway --- .../src/app/services/rest.service.ts | 2 +- components/x_way/x_way_server.py | 79 +++++++++++-------- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/components/control_center/src/app/services/rest.service.ts b/components/control_center/src/app/services/rest.service.ts index af42907..54033d2 100644 --- a/components/control_center/src/app/services/rest.service.ts +++ b/components/control_center/src/app/services/rest.service.ts @@ -18,7 +18,7 @@ export class RestService { } getCarEvents(vin) { - return this.http.get(this.currentLocation + 'car_events/?vin=' + vin, {observe: 'response'}); + return this.http.get(this.currentLocation + 'car_events?vin=' + vin, {observe: 'response'}); } getTrafficLights() { diff --git a/components/x_way/x_way_server.py b/components/x_way/x_way_server.py index 794754f..47e1fdb 100644 --- a/components/x_way/x_way_server.py +++ b/components/x_way/x_way_server.py @@ -9,23 +9,22 @@ import json; app = Flask(__name__) CORS(app) -api_bp = flask.Blueprint("api", __name__, url_prefix="/api/v1/resources") +api_bp = flask.Blueprint("api", __name__, url_prefix="/api/v1") API = flask_restx.Api(api_bp) app.register_blueprint(api_bp) - -NAMESPACE = API.namespace("car_events") - +NAMESPACE = API.namespace("resources") ENTITY_IDENT_URL = 'http://entityident:5002/api/v1/resources/' EVENT_STORE_URL = 'http://eventstore:5001/api/keys/' -@NAMESPACE.route('/') +@NAMESPACE.route('/car_events', doc={'description': 'Get most current car event for specific car'}) class CarsEvents(flask_restx.Resource): @NAMESPACE.doc(params={'vin': {'description': 'Vehicle Identifier Number of car', - 'type': 'String', 'default': '5GZCZ43D13S812715'}}) + 'type': 'String', 'default': '5GZCZ43D13S812715'}}) + @NAMESPACE.response(200, 'Success') def get(self): vin = request.args.get('vin') @@ -40,45 +39,57 @@ class CarsEvents(flask_restx.Resource): return jsonify(cars) -@app.route('/api/v1/resources/traffic_light_events', methods=['GET']) -def get_traffic_light_events(): - id = request.args.get('id') +@NAMESPACE.route('/traffic_light_events', doc={'description': 'Get most current traffic light event' + 'for specific traffic light'}) +class TrafficLightEvents(flask_restx.Resource): + @NAMESPACE.doc(params={'id': {'description': 'Unique ID of traffic light', + 'type': 'int', 'default': '1'}}) + @NAMESPACE.response(200, 'Success') + def get(self): + id = request.args.get('id') - try: - response = requests.get(EVENT_STORE_URL + 'TL:' + id + '/0/') - traffic_lights = json.loads(response.text) + try: + response = requests.get(EVENT_STORE_URL + 'TL:' + id + '/0/') + traffic_lights = json.loads(response.text) - except requests.exceptions.ConnectionError as e: - print("Is the EVENT_STORE_URL running and reachable?") - raise e + except requests.exceptions.ConnectionError as e: + print("Is the EVENT_STORE_URL running and reachable?") + raise e - return json_util.dumps(traffic_lights) + return jsonify(traffic_lights) -@app.route('/api/v1/resources/cars', methods=['GET']) -def get_cars(): - try: - response = requests.get(ENTITY_IDENT_URL + 'cars') - cars = response.json()['cursor'] +@NAMESPACE.route('/cars', doc={'description': 'Get entity details for all cars'}) +class Car(flask_restx.Resource): + @NAMESPACE.response(200, 'Success') + def get(self): - except requests.exceptions.ConnectionError as e: - print("Is the entity_ident_server running and reachable?") - raise e + try: + response = requests.get(ENTITY_IDENT_URL + 'cars') + cars = response.json()['cursor'] - return json_util.dumps(cars) + except requests.exceptions.ConnectionError as e: + print("Is the entity_ident_server running and reachable?") + raise e + + return jsonify(cars) -@app.route('/api/v1/resources/traffic_lights', methods=['GET']) -def get_traffic_lights(): - try: - response = requests.get(ENTITY_IDENT_URL + 'traffic_lights') - traffic_lights = response.json()['cursor'] +@NAMESPACE.route('/traffic_lights', doc={'description': 'Get entity details for all traffic lights'}) +class TrafficLights(flask_restx.Resource): - except requests.exceptions.ConnectionError as e: - print("Is the entity_ident_server running and reachable?") - raise e + @NAMESPACE.response(200, 'Success') + def get(self): - return json_util.dumps(traffic_lights) + try: + response = requests.get(ENTITY_IDENT_URL + 'traffic_lights') + traffic_lights = response.json()['cursor'] + + except requests.exceptions.ConnectionError as e: + print("Is the entity_ident_server running and reachable?") + raise e + + return jsonify(traffic_lights) if __name__ == '__main__':