Skip to content

Commit

Permalink
feat: use decorator for audit backend
Browse files Browse the repository at this point in the history
  • Loading branch information
johanseto committed Jul 12, 2024
1 parent 531c386 commit ef9a5b4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
51 changes: 51 additions & 0 deletions eox_nelp/pearson_vue/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
Module to add decorators related Pearson Vue Integration
"""
import logging

from eox_nelp.utils import camel_to_snake

try:
from eox_audit_model.decorators import audit_method, rename_function
except ImportError:
def rename_function(name): # pylint: disable=unused-argument
"""Identity rename_function"""
return lambda x: x

def audit_method(action): # pylint: disable=unused-argument
"""Identity audit_method"""
return lambda x: x

logger = logging.getLogger(__name__)


def audit_backend(func):
"""Decorator that wraps a class method with a try-finally block.
Args:
func: The method to be decorated.
Returns:
A wrapper function that executes the decorated method with a try-finally block.
Finally if there is backend_data, is logged after the execution.
"""
def wrapper(self, *args, **kwargs):

backend_name = self.__class__.__name__

@audit_method(action=f"Backend Execution: {backend_name}")
@rename_function(name=f"audit_backend_{camel_to_snake(backend_name)}")
def audit_backend_manager(self, *args, **kwargs):
try:
return func(self, *args, **kwargs)
finally:
if hasattr(self, "backend_data"):
logger.info(
"Backend %s executed. \n backend_data: %s",
backend_name,
self.backend_data,
)

return audit_backend_manager(self, *args, **kwargs)

return wrapper
4 changes: 2 additions & 2 deletions eox_nelp/pearson_vue/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def import_candidate_demographics(cdd_request, **kwargs): # pylint: disable=unu
response = api_client.import_candidate_demographics(payload)

if response.get("status", "error") == "accepted":
return response
return {"cdd_import": response}

raise PearsonImportError(
exception_reason=f"Import candidate demographics pipeline has failed with the following response: {response}",
Expand Down Expand Up @@ -276,7 +276,7 @@ def import_exam_authorization(ead_request, **kwargs): # pylint: disable=unused-
response = api_client.import_exam_authorization(payload)

if response.get("status", "error") == "accepted":
return response
return {"ead_import": response}

raise PearsonImportError(
exception_reason=f"Import exam authorization pipeline has failed with the following response: {response}",
Expand Down
4 changes: 4 additions & 0 deletions eox_nelp/pearson_vue/rti_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import importlib
from abc import ABC, abstractmethod

from eox_nelp.pearson_vue.decorators import audit_backend
from eox_nelp.pearson_vue.exceptions import PearsonBaseError
from eox_nelp.pearson_vue.pipeline import (
audit_pearson_error,
Expand Down Expand Up @@ -140,6 +141,9 @@ class RealTimeImport(AbstractBackend):
run_pipeline(): Executes the RTI pipeline by iterating through the pipeline functions.
get_pipeline(): Returns the RTI pipeline, which is a list of functions to be executed.
"""
@audit_backend
def run_pipeline(self):
return super().run_pipeline()

def handle_error(self, exception, failed_step_pipeline):
"""
Expand Down

0 comments on commit ef9a5b4

Please sign in to comment.