def addFlaskApiResources( apiInstance, appInstance, controllerList, schedulerList, serviceList, clientList, repositoryList, validatorList, mapperList, helperList, converterList ) : addResourceAttibutes(apiInstance) addRepositoryTo(apiInstance, repositoryList) addSchedulerListTo(apiInstance, schedulerList) addClientListTo(apiInstance, clientList) addServiceListTo(apiInstance, serviceList) addControllerListTo(apiInstance, controllerList) addValidatorListTo(apiInstance, validatorList) addMapperListTo(apiInstance, mapperList) addHelperListTo(apiInstance, helperList) addConverterListTo(apiInstance, converterList) SqlAlchemyProxy.initialize(apiInstance, appInstance) SchedulerManager.initialize(apiInstance, appInstance) SecurityManager.initialize(apiInstance, appInstance) ApiKeyManager.initialize(apiInstance, appInstance) SessionManager.initialize(apiInstance, appInstance) OpenApiManager.addSwagger(apiInstance, appInstance)
class DateTimeTest(MODEL) : __tablename__ = DATE_TIME_TEST id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) beginAtDatetime = sap.Column(sap.DateTime) endAtDatetime = sap.Column(sap.DateTime) beginAtDate = sap.Column(sap.Date) endAtDate = sap.Column(sap.Date) beginAtTime = sap.Column(sap.Time) endAtTime = sap.Column(sap.Time) intervalTime = sap.Column(sap.Interval) def __init__(self, id = None, beginAtDatetime = None, endAtDatetime = None, beginAtDate = None, endAtDate = None, beginAtTime = None, endAtTime = None, intervalTime = None, timedelta = None ): self.id = id self.beginAtDatetime = forcedlyGetDateTime(beginAtDatetime) self.endAtDatetime = forcedlyGetDateTime(endAtDatetime) self.beginAtDate = forcedlyGetDate(beginAtDate) self.endAtDate = forcedlyGetDate(endAtDate) self.beginAtTime = forcedlyGetTime(beginAtTime) self.endAtTime = forcedlyGetTime(endAtTime) self.intervalTime = forcedlyGetInterval(intervalTime) self.timedelta = forcedlyGetInterval(timedelta)
def initialize( rootName, refferenceModel, staticPackage = 'static', viewsPackage = 'views' ) : app = Flask( rootName, static_folder = staticPackage, template_folder = viewsPackage ) api = Api(app) api.app = app api.app.api = api api.cors = CORS(app) api.cors.api = api addGlobalsTo(api) OpenApiManager.newDocumentation(api, app) SqlAlchemyProxy.addResource(api, app, baseModel=refferenceModel, echo=False) SchedulerManager.addResource(api, app) SessionManager.addResource(api, app) ApiKeyManager.addResource(api, app) SecurityManager.addResource(api, app) addFlaskApiResources(*[api, app, *[getResourceList(api, resourceType) for resourceType in FlaskManager.KW_RESOURCE_LIST]]) SessionManager.onHttpRequestCompletion(api, app) ApiKeyManager.onHttpRequestCompletion(api, app) SecurityManager.onHttpRequestCompletion(api, app) SchedulerManager.onHttpRequestCompletion(api, app) SqlAlchemyProxy.onHttpRequestCompletion(api, app) return app
class TestContact(MODEL): __tablename__ = 'TestContact' id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) key = sap.Column(sap.String(128), nullable=False) def __init__(self, id = None, key = None ): self.id = id self.key = key def __repr__(self): return f'{self.__tablename__}(id: {self.id}, key: {self.key})'
def runApi(*args, api=None, **kwargs): if ObjectHelper.isNone(api): api = FlaskUtil.getApi() muteLogs(api) if 'host' not in kwargs and api.host: kwargs['host'] = api.host if not 'localhost' == api.host else '0.0.0.0' if 'port' not in kwargs and api.port: kwargs['port'] = api.port apiUrl = getApiUrl(api) documentationUrl = OpenApiManager.getDocumentationUrl(api) healthCheckUrl = f'{documentationUrl[:-len(OpenApiManager.DOCUMENTATION_ENDPOINT)]}{HealthCheckConstant.URI}' log.success(runApi, f'Api will run at {apiUrl}') log.success(runApi, f'Health check will be available at {healthCheckUrl}') log.success(runApi, f'Documentation will be available at {documentationUrl}') api.app.run(*args, **kwargs) SessionManager.onShutdown(api, api.app) ApiKeyManager.onShutdown(api, api.app) SecurityManager.onShutdown(api, api.app) SchedulerManager.onShutdown(api, api.app) SqlAlchemyProxy.onShutdown(api, api.app) log.success(runApi, f'{api.globals.apiName} successfully shutdown')
def addRepositoryTo(apiInstance, repositoryList, model, databaseEnvironmentVariable=None, localStorageName=None): apiInstance.repository = SqlAlchemyProxy.SqlAlchemyProxy( databaseEnvironmentVariable=databaseEnvironmentVariable, localName=localStorageName, model=model, globals=apiInstance.globals, echo=False, checkSameThread=False) for repository in repositoryList: apiInstance.bindResource(apiInstance, repository())
class ActuatorHealth(MODEL): __tablename__ = ACTUATOR_HEALTH laskCheck = sap.Column(sap.DateTime(), default=datetime.datetime.utcnow) status = sap.Column(sap.String(MAX_STATUS_SIZE), default=ActuatorHealthStatus.DOWN) id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) def __init__(self, status=ActuatorHealthStatus.DOWN, laskCheck=None, id=None): self.status = status self.laskCheck = laskCheck self.id = id def __repr__(self): return f'{self.__tablename__}(id={self.id}, laskCheck={self.laskCheck}, status={self.status})'
def addRepositoryTo(apiInstance, repositoryList, model): apiInstance.repository = SqlAlchemyProxy.SqlAlchemyProxy( model, apiInstance.globals, echo=False) for repository in repositoryList: apiInstance.bindResource(apiInstance, repository())
class Child(MODEL) : __tablename__ = SELF_REFERENCE_CHILD_NAME id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) father, fatherId = sap.getManyToOne(__tablename__, SELF_REFERENCE_FATHER_NAME, MODEL) brother, brotherId = sap.getOneToOneChild(__tablename__, BROTHER_NAME, MODEL)
class Father(MODEL) : __tablename__ = SELF_REFERENCE_FATHER_NAME id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) childList = sap.getOneToMany(__tablename__, SELF_REFERENCE_CHILD_NAME, MODEL) brotherList = sap.getOneToMany(__tablename__, BROTHER_NAME, MODEL)
class MyEntityClass(MODEL) : __tablename__ = 'MyEntityClass' id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True)
def __init__(self, myAttribute=None): self.myAttribute = myAttribute self.myNeutralAttribute = 'someString' class MyAttributeClass : def __init__(self, myClass=None): self.myClass = myClass self.myNeutralClassAttribute = 'someOtherString' MY_DICTIONARY = { 'myString' : 'myValue', 'myInteger' : 0, 'myFloat' : 0.099 } MODEL = sap.getNewModel() class MyEntityClass(MODEL) : __tablename__ = 'MyEntityClass' id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) SELF_REFERENCE_FATHER_NAME = 'Father' SELF_REFERENCE_CHILD_NAME = 'Child' BROTHER_NAME = 'Brother' DATE_TIME_TEST = 'DateTimeTest' class Father(MODEL) : __tablename__ = SELF_REFERENCE_FATHER_NAME id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) childList = sap.getOneToMany(__tablename__, SELF_REFERENCE_CHILD_NAME, MODEL) brotherList = sap.getOneToMany(__tablename__, BROTHER_NAME, MODEL)
class ErrorLog(MODEL): __tablename__ = ERROR_LOG status = sap.Column(sap.Integer()) verb = sap.Column(sap.String(MAX_VERB_SIZE)) url = sap.Column(sap.String(MAX_URL_SIZE)) message = sap.Column(sap.String(MAX_MESSAGE_SIZE)) logMessage = sap.Column(sap.String(MAX_MESSAGE_SIZE)) logPayload = sap.Column(sap.String(MAX_HTTP_ERROR_LOG_PAYLOAD_SIZE)) logResource = sap.Column(sap.String(MAX_RESOURCE_NAME_SIZE)) logResourceMethod = sap.Column(sap.String(MAX_RESOURCE_METHOD_NAME_SIZE)) timeStamp = sap.Column(sap.DateTime(), default=datetime.datetime.utcnow) id = sap.Column(sap.Integer(), sap.Sequence(f'{__tablename__}{sap.ID}{sap.SEQ}'), primary_key=True) def __init__(self, status = None, verb = None, url = None, message = None, logMessage = None, logPayload = None, logResource = None, logResourceMethod = None, timeStamp = None, id = None ): self.status = status self.verb = str(verb)[:MAX_VERB_SIZE-1] self.url = str(url)[:MAX_URL_SIZE-1] self.message = str(message)[:MAX_MESSAGE_SIZE-1] self.logMessage = str(logMessage)[:MAX_MESSAGE_SIZE-1] self.logPayload = str(logPayload)[:MAX_HTTP_ERROR_LOG_PAYLOAD_SIZE-1] self.logResource = str(logResource)[:MAX_RESOURCE_NAME_SIZE-1] self.logResourceMethod = str(logResourceMethod)[:MAX_RESOURCE_METHOD_NAME_SIZE-1] self.timeStamp = timeStamp self.id = id def override(self, globalException): self.status = globalException.status self.verb = str(globalException.verb)[:MAX_VERB_SIZE-1] self.url = str(globalException.url)[:MAX_URL_SIZE-1] self.message = str(globalException.message)[:MAX_MESSAGE_SIZE-1] self.logMessage = str(globalException.logMessage)[:MAX_MESSAGE_SIZE-1] self.logPayload = str(globalException.logPayload)[:MAX_HTTP_ERROR_LOG_PAYLOAD_SIZE-1] self.logResource = str(globalException.logResource)[:MAX_RESOURCE_NAME_SIZE-1] self.logResourceMethod = str(globalException.logResourceMethod)[:MAX_RESOURCE_METHOD_NAME_SIZE-1] self.timeStamp = globalException.timeStamp def __repr__(self): return f'{self.__tablename__}(verb={self.verb}, url={self.url}, status={self.status}, message={self.message}, id={self.id})'