from django.http import JsonResponse


class ApiBase(object):
    @classmethod
    def success(cls, data, status=200):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data, status=status)

    @classmethod
    def error(cls, error, status=400):
        """
        :type error: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'error': error}, status=status)

from functools import wraps
from json import loads as load_json

from django.http import JsonResponse


class ApiBase(object):
    @classmethod
    def validation_failure(cls, message, status=400):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'validation_failure': message}, status=status)

    @classmethod
    def success(cls, data, status=200):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data, status=status)

    @classmethod
    def error(cls, error, status=400):
        """
        :type error: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'error': error}, status=status)


def json_request(expected_keys=None):
    """Decorator to validate that a request is in JSON and (optionally) has some specific keys in the JSON object.

    """
    expected_keys = expected_keys or []

    def decorator(func):
        @wraps(func)
        def wrapper(cls, request, *args, **kwargs):
            data = load_json(request.body.decode('utf8'))

            for expected_key in expected_keys:
                value = data.get(expected_key)
                if not value:
                    return ApiBase.validation_failure('missing key: {}'.format(expected_key))

            return func(cls, data, request, *args, **kwargs)

        return wrapper
    return decorator

Implement JSON request validation decorator
Implement JSON request validation decorator

with @NerdyProjects

+ from functools import wraps
+ from json import loads as load_json
+ 
  from django.http import JsonResponse
  
  
  class ApiBase(object):
+     @classmethod
+     def validation_failure(cls, message, status=400):
+         """
+         :type message: str
+         :type status: int
+         :rtype JsonResponse
+ 
+         """
+         return JsonResponse({'validation_failure': message}, status=status)
+ 
      @classmethod
      def success(cls, data, status=200):
          """
          :type data: dict
          :type status: int
          :rtype JsonResponse
  
          """
          return JsonResponse(data, status=status)
  
      @classmethod
      def error(cls, error, status=400):
          """
          :type error: str
          :type status: int
          :rtype JsonResponse
  
          """
          return JsonResponse({'error': error}, status=status)
  
+ 
+ def json_request(expected_keys=None):
+     """Decorator to validate that a request is in JSON and (optionally) has some specific keys in the JSON object.
+ 
+     """
+     expected_keys = expected_keys or []
+ 
+     def decorator(func):
+         @wraps(func)
+         def wrapper(cls, request, *args, **kwargs):
+             data = load_json(request.body.decode('utf8'))
+ 
+             for expected_key in expected_keys:
+                 value = data.get(expected_key)
+                 if not value:
+                     return ApiBase.validation_failure('missing key: {}'.format(expected_key))
+ 
+             return func(cls, data, request, *args, **kwargs)
+ 
+         return wrapper
+     return decorator
+ 
Implement JSON request validation decorator
## Code Before:
from django.http import JsonResponse


class ApiBase(object):
    @classmethod
    def success(cls, data, status=200):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data, status=status)

    @classmethod
    def error(cls, error, status=400):
        """
        :type error: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'error': error}, status=status)

## Instruction:
Implement JSON request validation decorator
## Code After:
from functools import wraps
from json import loads as load_json

from django.http import JsonResponse


class ApiBase(object):
    @classmethod
    def validation_failure(cls, message, status=400):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'validation_failure': message}, status=status)

    @classmethod
    def success(cls, data, status=200):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data, status=status)

    @classmethod
    def error(cls, error, status=400):
        """
        :type error: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'error': error}, status=status)


def json_request(expected_keys=None):
    """Decorator to validate that a request is in JSON and (optionally) has some specific keys in the JSON object.

    """
    expected_keys = expected_keys or []

    def decorator(func):
        @wraps(func)
        def wrapper(cls, request, *args, **kwargs):
            data = load_json(request.body.decode('utf8'))

            for expected_key in expected_keys:
                value = data.get(expected_key)
                if not value:
                    return ApiBase.validation_failure('missing key: {}'.format(expected_key))

            return func(cls, data, request, *args, **kwargs)

        return wrapper
    return decorator
