""" Endpoinds to serve location API """ __author__ = '@tormakris' __copyright__ = "Copyright 2021, KMLabz Team" __module_name__ = "resources" __version__text__ = "1" import json import random from flask import request, current_app, abort from flask_restful import Resource from redis_client import redis_client from schemas import ServiceLocatorSchema, ServiceDirectorySchema class ServiceDiscoveryResource(Resource): """ Service Discovery endpoint """ servicedirectoryschema = ServiceDirectorySchema(many=True) def get(self): try: servicelist = self.servicedirectoryschema.load(json.loads(redis_client.get('servicelist').decode('UTF-8'))) except Exception as e: current_app.logger.info(e) abort(404, "not found") return self.servicedirectoryschema.dump(servicelist), 200 class ServiceLocationResource(Resource): """ Service location endpoint """ servicelocatorschema = ServiceLocatorSchema(many=False) def get(self, serviceid: str): try: serviceobject: dict = self.servicelocatorschema.load(json.loads(redis_client.get(serviceid).decode('UTF-8'))) servicelocation: str = random.choice(serviceobject['servicearray'])['location'] except Exception as e: current_app.logger.info(e) abort(404, "not found") return {"location": servicelocation}, 200 class ServiceDatabaseResource(Resource): """ Service database endpoint """ servicelocatorschema = ServiceLocatorSchema(many=False) servicedirectoryschema = ServiceDirectorySchema(many=True) def post(self): try: body = request.get_json() except Exception as e: current_app.logger.info(e) abort(400, "JSON parse error") try: servicelocation = self.servicelocatorschema.load(body) except Exception as e: current_app.logger.info(e) abort(417, "invalid JSON schema") try: redis_client.set(servicelocation['id'], json.dumps(self.servicelocatorschema.dump(servicelocation)).encode('UTF-8')) servicelist = self.servicedirectoryschema.load(json.loads(redis_client.get('servicelist').decode('UTF-8'))) servicelist.append({"name": servicelocation['name'], "id": servicelocation['id']}) redis_client.set('servicelist', json.dumps(self.servicedirectoryschema.dump(servicelist)).encode('UTF-8')) except Exception as e: current_app.logger.info(e) abort(404, "not found") return 200 class ServiceDatabaseItemResource(Resource): """ Service database endpoint interacting with individual items """ servicelocatorschema = ServiceLocatorSchema(many=False) servicedirectoryschema = ServiceDirectorySchema(many=True) def put(self, serviceid: str): try: body = request.get_json() except Exception as e: current_app.logger.info(e) abort(400, "JSON parse error") try: servicelocation = self.servicelocatorschema.load(body) except Exception as e: current_app.logger.info(e) abort(417, "invalid JSON schema") try: redis_client.get(serviceid) redis_client.set(serviceid, json.dumps(self.servicelocatorschema.dump(servicelocation)).encode('UTF-8')) except Exception as e: current_app.logger.info(e) abort(404, "not found") return 200 def get(self, serviceid: str): try: servicelocation = self.servicelocatorschema.load(json.loads(redis_client.get(serviceid).decode('UTF-8'))) except Exception as e: current_app.logger.info(e) abort(404, "not found") return servicelocation, 200 class ResetApplicationResource(Resource): """ Reset the application """ servicedirectoryschema = ServiceDirectorySchema(many=True) servicelocatorschema = ServiceLocatorSchema(many=False) def get(self): try: redis_client.set('servicelist', json.dumps(self.servicedirectoryschema.dump( [{"name": "Test App", "id": "c03b5706-5ea3-46c9-a2cb-e1442f3528e3"}])).encode('UTF-8')) redis_client.set('c03b5706-5ea3-46c9-a2cb-e1442f3528e3', json.dumps(self.servicelocatorschema.dump({"name": "Test App", "id": "c03b5706-5ea3-46c9-a2cb-e1442f3528e3", "servicearray": [ { "location": "198.51.100.42", "zone": "string" } ]})).encode( 'UTF-8')) except Exception as e: current_app.logger.info(e) abort(404, e) return 200