Exemple #1
0
 def innerResourceInstanceMethod(*args,**kwargs) :
     resourceInstance = args[0]
     try :
         FlaskManager.validateArgs(args,requestClass,innerResourceInstanceMethod)
         methodReturn = resourceInstanceMethod(*args,**kwargs)
     except Exception as exception :
         FlaskManager.raiseGlobalException(exception, resourceInstance, resourceInstanceMethod)
     return methodReturn
 def innerResourceInstanceMethod(*args, **kwargs):
     resourceInstance = args[0]
     try:
         FlaskManager.validateArgs(args, requestClass,
                                   innerResourceInstanceMethod)
         args = FlaskManager.getArgsWithResponseClassInstanceAppended(
             args, responseClass)
         methodReturn = resourceInstanceMethod(*args, **kwargs)
     except Exception as exception:
         FlaskManager.raiseAndPersistGlobalException(
             exception, resourceInstance, resourceInstanceMethod)
     return methodReturn
 def __init__(self, *args, **kwargs):
     log.wrapper(
         OuterClass,
         f'in {InnerClass.__name__}.__init__(*{args},**{kwargs})')
     apiInstance = FlaskManager.getApi()
     OuterClass.__init__(self, *args, **kwargs)
     self.globals = apiInstance.globals
 def innerResourceInstanceMethod(*args, **kwargs):
     f'''(*args, {FlaskUtil.KW_HEADERS}={{}}, {FlaskUtil.KW_PARAMETERS}={{}}, **kwargs)'''
     resourceInstance = args[0]
     clientResponse = None
     completeResponse = None
     try :
         FlaskManager.validateKwargs(
             kwargs,
             resourceInstance,
             resourceInstanceMethod,
             requestHeaderClass,
             requestParamClass
         )
         FlaskManager.validateArgs(args, requestClass, resourceInstanceMethod)
         clientResponse = None
         httpClientEvent = getHttpClientEvent(resourceInstanceMethod, *args, **kwargs)
         if isinstance(httpClientEvent, ManualHttpClientEvent):
             completeResponse = httpClientEvent.completeResponse
         elif isinstance(httpClientEvent, HttpClientEvent):
             try :
                 clientResponse = HTTP_CLIENT_RESOLVERS_MAP.get(
                     httpClientEvent.verb,
                     raiseHttpClientEventNotFoundException
                 )(
                     resourceInstance,
                     *httpClientEvent.args,
                     **httpClientEvent.kwargs
                 )
             except Exception as exception:
                 raiseException(clientResponse, exception)
             raiseExceptionIfNeeded(clientResponse)
             completeResponse = getCompleteResponse(clientResponse, responseClass, produces)
             FlaskManager.validateCompleteResponse(responseClass, completeResponse)
         else:
             raise Exception('Unknown http client event')
     except Exception as exception:
         log.log(innerResourceInstanceMethod, 'Failure at client method execution', exception=exception, muteStackTrace=True)
         FlaskManager.raiseAndPersistGlobalException(exception, resourceInstance, resourceInstanceMethod, context=HttpDomain.CLIENT_CONTEXT)
     clientResponseStatus = completeResponse[-1]
     clientResponseHeaders = completeResponse[1]
     clientResponseBody = completeResponse[0] if ObjectHelper.isNotNone(completeResponse[0]) else {'message' : HttpStatus.map(clientResponseStatus).enumName}
     if resourceInstance.logResponse or logResponse :
         log.prettyJson(
             resourceInstanceMethod,
             '[CLIENT    ] Response',
             {
                 'headers': clientResponseHeaders,
                 'body': Serializer.getObjectAsDictionary(clientResponseBody),
                 'status': clientResponseStatus
             },
             condition = True,
             logLevel = log.INFO
         )
     if returnOnlyBody:
         return completeResponse[0]
     else:
         return completeResponse
Exemple #5
0
 def __init__(self,*args,**kwargs):
     log.debug(OuterClass,f'in {InnerClass.__name__}.__init__(*{args},**{kwargs})')
     OuterClass.__init__(self,*args,**kwargs)
     apiInstance = FlaskManager.getApi()
     self.service = apiInstance.resource.service
     self.validator = apiInstance.resource.validator
     self.helper = apiInstance.resource.helper
     self.converter = apiInstance.resource.converter
 def __init__(self, *args, **kwargs):
     log.wrapper(
         OuterClass,
         f'in {InnerClass.__name__}.__init__(*{args},**{kwargs})')
     OuterClass.__init__(self, *args, **kwargs)
     apiInstance = FlaskManager.getApi()
     self.helper = apiInstance.resource.helper
     self.converter = apiInstance.resource.converter
Exemple #7
0
 def __init__(self, *args, **kwargs):
     log.debug(
         OuterClass,
         f'in {InnerClass.__name__}.__init__(*{args},**{kwargs})')
     apiInstance = FlaskManager.getApi()
     OuterClass.__init__(self, *args, **kwargs)
     self.globals = apiInstance.globals
     self.service = apiInstance.resource.service
Exemple #8
0
 def __init__(self, *args, **kwargs):
     log.debug(
         OuterClass,
         f'in {InnerClass.__name__}.__init__(*{args},**{kwargs})')
     OuterClass.__init__(self, *args, **kwargs)
     apiInstance = FlaskManager.getApi()
     self.repository = apiInstance.repository
     self.globals = apiInstance.globals
 def innerResourceInstanceMethod(*args, **kwargs):
     resourceInstance = args[0]
     completeResponse = None
     try:
         FlaskManager.validateArgs(args, requestClass,
                                   resourceInstanceMethod)
         completeResponse = resourceInstanceMethod(*args, **kwargs)
         ###- This simple client cannot handle headers or anything this much elegant
         if ObjectHelper.isTuple(
                 completeResponse) and 0 < len(completeResponse):
             completeResponse = completeResponse[0]
     except Exception as exception:
         log.log(innerResourceInstanceMethod,
                 'Failure at client method execution',
                 exception=exception,
                 muteStackTrace=True)
         raise exception
     return completeResponse
Exemple #10
0
 def innerResourceInstanceMethod(*args, **kwargs):
     resourceInstanceName = methodClassName[:-len(
         FlaskManager.KW_SCHEDULER_RESOURCE)]
     resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
     args = FlaskManager.getArgumentInFrontOfArgs(
         args,
         ReflectionHelper.getAttributeOrMethod(
             apiInstance.resource.scheduler, resourceInstanceName))
     resourceInstance = args[0]
     methodReturn = None
     try:
         FlaskManager.validateArgs(args, requestClass,
                                   innerResourceInstanceMethod)
         methodReturn = resourceInstanceMethod(*args, **kwargs)
     except Exception as exception:
         FlaskManager.raiseGlobalException(exception, resourceInstance,
                                           resourceInstanceMethod)
         log.log(innerResourceInstanceMethod,
                 f'Not possible to run {shedulerId} properly',
                 exception=exception)
     return methodReturn
Exemple #11
0
 def __init__(self,*args,**kwargs):
     log.wrapper(OuterClass,f'in {InnerClass.__name__}.__init__(*{args},**{kwargs})')
     apiInstance = FlaskManager.getApi()
     OuterClass.__init__(self,*args,**kwargs)
     self.service = apiInstance.resource.service
     self.client = apiInstance.resource.client
     self.repository = apiInstance.resource.repository
     self.validator = apiInstance.resource.validator
     self.mapper = apiInstance.resource.mapper
     self.helper = apiInstance.resource.helper
     self.converter = apiInstance.resource.converter
     self.globals = apiInstance.globals
Exemple #12
0
 def innerResourceInstanceMethod(*args, **kwargs):
     resourceInstanceName = methodClassName[:-len(
         FlaskManager.KW_SCHEDULER_RESOURCE)]
     resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
     args = FlaskManager.getArgumentInFrontOfArgs(
         args,
         ReflectionHelper.getAttributeOrMethod(
             apiInstance.resource.scheduler, resourceInstanceName))
     resourceInstance = args[0]
     muteLogs = resourceInstance.muteLogs or resourceMethod.muteLogs
     if resourceInstance.enabled and not resourceInstance.disabled and not resourceMethod.disabled:
         if not muteLogs:
             log.debug(
                 resourceMethod,
                 f'{resourceMethod.shedulerId} scheduler started with args={methodArgs} and kwargs={methodKwargs}'
             )
         methodReturn = None
         try:
             FlaskManager.validateArgs(args, requestClass,
                                       innerResourceInstanceMethod)
             methodReturn = resourceMethod(*args, **kwargs)
         except Exception as exception:
             if not muteLogs:
                 log.warning(
                     resourceMethod,
                     f'Not possible to run {resourceMethod.shedulerId} properly',
                     exception=exception,
                     muteStackTrace=True)
             FlaskManager.raiseAndPersistGlobalException(
                 exception, resourceInstance, resourceMethod)
         if not muteLogs:
             log.debug(
                 resourceMethod,
                 f'{resourceMethod.shedulerId} scheduler finished')
         return methodReturn
     if not muteLogs:
         log.warning(
             resourceMethod,
             f'{resourceMethod.shedulerId} scheduler didn{c.SINGLE_QUOTE}t started. {"Schedulers are disabled" if not resourceInstance.enabled else "This scheduler is disabled" if resourceInstance.disabled else "This scheduler method is disabled"}'
         )
 def innerResourceInstanceMethod(*args,**kwargs) :
     resourceInstance = args[0]
     completeResponse = None
     if logRequest :
         log.prettyJson(
             resourceInstanceMethod,
             'bodyRequest',
             json.loads(Serializer.jsonifyIt(args[1:])),
             condition = logRequest,
             logLevel = log.DEBUG
         )
     try :
         FlaskManager.validateKwargs(
             kwargs,
             resourceInstance,
             innerResourceInstanceMethod,
             requestHeaderClass = requestHeaderClass,
             requestParamClass = requestParamClass
         )
         FlaskManager.validateArgs(args, requestClass, innerResourceInstanceMethod)
         completeResponse = resourceInstanceMethod(*args,**kwargs)
         FlaskManager.validateResponseClass(responseClass, completeResponse)
     except Exception as exception :
         log.warning(innerResourceInstanceMethod, 'Not posssible to complete request', exception=exception)
         raise exception
     controllerResponse = completeResponse[0] if ObjectHelper.isNotNone(completeResponse[0]) else {'message' : completeResponse[1].enumName}
     if logResponse :
         log.prettyJson(
             resourceInstanceMethod,
             'bodyResponse',
             json.loads(Serializer.jsonifyIt(controllerResponse)),
             condition = logResponse,
             logLevel = log.DEBUG
         )
     return completeResponse[0]
Exemple #14
0
 def __init__(self, *args, **kwargs):
     log.wrapper(
         OuterClass,
         f'in {InnerClass.__name__}.__init__(*{args},**{kwargs})')
     apiInstance = FlaskManager.getApi()
     OuterClass.__init__(self, *args, **kwargs)
     self.globals = apiInstance.globals
     self.service = apiInstance.resource.service
     self.enabled = self.globals.getApiSetting(
         ConfigurationKeyConstant.API_SCHEDULER_ENABLE)
     self.disabled = disable
     self.muteLogs = muteLogs or ConverterStatic.getValueOrDefault(
         self.globals.getApiSetting(
             ConfigurationKeyConstant.API_SCHEDULER_MUTE_LOGS),
         DEFAUTL_MUTE_LOGS)
Exemple #15
0
    def innerMethodWrapper(resourceInstanceMethod, *innerMethodArgs,
                           **innerMethodKwargs):
        log.debug(SchedulerMethod,
                  f'''wrapping {resourceInstanceMethod.__name__}''')
        apiInstance = FlaskManager.getApi()
        methodClassName = ReflectionHelper.getMethodClassName(
            resourceInstanceMethod)
        methodName = ReflectionHelper.getName(resourceInstanceMethod)
        methodKwargs['id'] = methodKwargs.get(
            'id', f'{methodClassName}{c.DOT}{methodName}')
        instancesUpTo = methodKwargs.pop('instancesUpTo', 1)
        weekDays = methodKwargs.pop('weekDays', None)
        if ObjectHelper.isNotEmpty(
                methodArgs
        ) and SchedulerType.CRON == methodArgs[0] and ObjectHelper.isNotNone(
                weekDays) and StringHelper.isNotBlank(weekDays):
            methodKwargs['day_of_week'] = weekDays
        if ObjectHelper.isNotNone(instancesUpTo):
            methodKwargs['max_instances'] = instancesUpTo
        shedulerArgs = [*methodArgs]
        shedulerKwargs = {**methodKwargs}

        @apiInstance.scheduler.task(*shedulerArgs, **shedulerKwargs)
        def innerResourceInstanceMethod(*args, **kwargs):
            resourceInstanceName = methodClassName[:-len(
                FlaskManager.KW_SCHEDULER_RESOURCE)]
            resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
            args = FlaskManager.getArgumentInFrontOfArgs(
                args,
                ReflectionHelper.getAttributeOrMethod(
                    apiInstance.resource.scheduler, resourceInstanceName))
            resourceInstance = args[0]
            methodReturn = None
            try:
                FlaskManager.validateArgs(args, requestClass,
                                          innerResourceInstanceMethod)
                methodReturn = resourceInstanceMethod(*args, **kwargs)
            except Exception as exception:
                FlaskManager.raiseGlobalException(exception, resourceInstance,
                                                  resourceInstanceMethod)
                log.log(innerResourceInstanceMethod,
                        f'Not possible to run {shedulerId} properly',
                        exception=exception)
            return methodReturn

        ReflectionHelper.overrideSignatures(innerResourceInstanceMethod,
                                            resourceInstanceMethod)
        return innerResourceInstanceMethod
def addControllerListTo(apiInstance, controllerList):
    for controller in controllerList:
        OpenApiManager.addControllerDocumentation(controller, apiInstance)
        mainUrl = f'{apiInstance.baseUrl}{controller.url}'
        urlList = [mainUrl]
        controllerMethodList = FlaskManager.getAttributePointerList(controller)
        for controllerMethod in controllerMethodList:
            if hasattr(controllerMethod,
                       FlaskManager.KW_URL) and controllerMethod.url:
                controllerUrl = f'{mainUrl}{controllerMethod.url}'
                if controllerUrl not in urlList:
                    urlList.append(controllerUrl)
                # subUrlList = controllerMethod.url.split(c.SLASH)
                # concatenatedSubUrl = c.NOTHING
                # for subUrl in subUrlList :
                #     if subUrl :
                #         concatenatedSubUrl += f'{c.SLASH}{subUrl}'
                #         if c.LESSER == subUrl[0] and c.BIGGER == subUrl[-1] :
                #             newUrl = f'{apiInstance.baseUrl}{controller.url}{concatenatedSubUrl}'
                #             if not newUrl in urlList :
                #                 urlList.append(newUrl)
                OpenApiManager.addEndPointDocumentation(
                    controllerUrl, controllerMethod, controller, apiInstance)
        apiInstance.add_resource(controller, *urlList)
Exemple #17
0
def addGlobalsTo(apiInstance):
    FlaskManager.validateFlaskApi(apiInstance)
    apiInstance.globals = FlaskManager.getGlobals()
    apiInstance.globals.api = apiInstance
    apiInstance.bindResource = FlaskManager.bindResource
Exemple #18
0
    def innerMethodWrapper(resourceMethod, *innerMethodArgs,
                           **innerMethodKwargs):
        log.wrapper(SchedulerMethod, f'''wrapping {resourceMethod.__name__}''')
        apiInstance = FlaskManager.getApi()
        methodClassName = ReflectionHelper.getMethodClassName(resourceMethod)
        methodName = ReflectionHelper.getName(resourceMethod)
        methodKwargs['id'] = methodKwargs.get(
            'id', f'{methodClassName}{c.DOT}{methodName}')
        instancesUpTo = methodKwargs.pop('instancesUpTo', 1)
        weekDays = methodKwargs.pop('weekDays', None)
        resourceMethod.disabled = disable
        resourceMethod.shedulerId = methodKwargs['id']
        resourceMethod.muteLogs = muteLogs or ConverterStatic.getValueOrDefault(
            apiInstance.globals.getApiSetting(
                ConfigurationKeyConstant.API_SCHEDULER_MUTE_LOGS),
            DEFAUTL_MUTE_LOGS)
        if ObjectHelper.isNotEmpty(
                methodArgs
        ) and SchedulerType.CRON == methodArgs[0] and ObjectHelper.isNotNone(
                weekDays) and StringHelper.isNotBlank(weekDays):
            methodKwargs['day_of_week'] = weekDays
        if ObjectHelper.isNotNone(instancesUpTo):
            methodKwargs['max_instances'] = instancesUpTo
        shedulerArgs = [*methodArgs]
        shedulerKwargs = {**methodKwargs}

        @apiInstance.schedulerManager.task(*shedulerArgs, **shedulerKwargs)
        def innerResourceInstanceMethod(*args, **kwargs):
            resourceInstanceName = methodClassName[:-len(
                FlaskManager.KW_SCHEDULER_RESOURCE)]
            resourceInstanceName = f'{resourceInstanceName[0].lower()}{resourceInstanceName[1:]}'
            args = FlaskManager.getArgumentInFrontOfArgs(
                args,
                ReflectionHelper.getAttributeOrMethod(
                    apiInstance.resource.scheduler, resourceInstanceName))
            resourceInstance = args[0]
            muteLogs = resourceInstance.muteLogs or resourceMethod.muteLogs
            if resourceInstance.enabled and not resourceInstance.disabled and not resourceMethod.disabled:
                if not muteLogs:
                    log.debug(
                        resourceMethod,
                        f'{resourceMethod.shedulerId} scheduler started with args={methodArgs} and kwargs={methodKwargs}'
                    )
                methodReturn = None
                try:
                    FlaskManager.validateArgs(args, requestClass,
                                              innerResourceInstanceMethod)
                    methodReturn = resourceMethod(*args, **kwargs)
                except Exception as exception:
                    if not muteLogs:
                        log.warning(
                            resourceMethod,
                            f'Not possible to run {resourceMethod.shedulerId} properly',
                            exception=exception,
                            muteStackTrace=True)
                    FlaskManager.raiseAndPersistGlobalException(
                        exception, resourceInstance, resourceMethod)
                if not muteLogs:
                    log.debug(
                        resourceMethod,
                        f'{resourceMethod.shedulerId} scheduler finished')
                return methodReturn
            if not muteLogs:
                log.warning(
                    resourceMethod,
                    f'{resourceMethod.shedulerId} scheduler didn{c.SINGLE_QUOTE}t started. {"Schedulers are disabled" if not resourceInstance.enabled else "This scheduler is disabled" if resourceInstance.disabled else "This scheduler method is disabled"}'
                )

        ReflectionHelper.overrideSignatures(innerResourceInstanceMethod,
                                            resourceMethod)
        resourceMethod.shedulerId = methodKwargs.get('id')
        innerResourceInstanceMethod.disable = resourceMethodDisable
        innerResourceInstanceMethod.muteLogs = resourceMethodMuteLogs
        return innerResourceInstanceMethod
Exemple #19
0
def runFlaskApplication(app=None):
    FlaskManager.runApi(debug=False, use_reloader=False)