
import atexit
from traceback import format_exception_only

from mdk import start


class MDKSessionMiddleware(object):
    """
    Add an MDK session to the Django request, as well as circuit breaker
    support.

    The request object will get a ``mdk_session`` attribute added to it.
    """
    def __init__(self):
        self.mdk = start()
        atexit.register(self.mdk.stop)

    def process_request(self, request):
        request.mdk_session = self.mdk.join(
            request.META.get("HTTP_X_MDK_CONTEXT"))
        request.mdk_session.start_interaction()

    def process_response(self, request, response):
        request.mdk_session.finish_interaction()
        del request.mdk_session
        return response

    def process_exception(self, request, exception):
        request.mdk_session.fail_interaction(
            "".join(format_exception_only(exception.__class__, exception)))


import atexit
from traceback import format_exception_only

from mdk import start

# Django 1.10 new-style middleware compatibility:
try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object


class MDKSessionMiddleware(MiddlewareMixin):
    """
    Add an MDK session to the Django request, as well as circuit breaker
    support.

    The request object will get a ``mdk_session`` attribute added to it.
    """
    def __init__(self, *args, **kwargs):
        MiddlewareMixin.__init__(self, *args, **kwargs)
        self.mdk = start()
        atexit.register(self.mdk.stop)

    def process_request(self, request):
        request.mdk_session = self.mdk.join(
            request.META.get("HTTP_X_MDK_CONTEXT"))
        request.mdk_session.start_interaction()

    def process_response(self, request, response):
        request.mdk_session.finish_interaction()
        del request.mdk_session
        return response

    def process_exception(self, request, exception):
        request.mdk_session.fail_interaction(
            "".join(format_exception_only(exception.__class__, exception)))

Work with Django 1.10 as well.
Work with Django 1.10 as well.

  
  import atexit
  from traceback import format_exception_only
  
  from mdk import start
  
+ # Django 1.10 new-style middleware compatibility:
+ try:
+     from django.utils.deprecation import MiddlewareMixin
+ except ImportError:
+     MiddlewareMixin = object
  
+ 
- class MDKSessionMiddleware(object):
+ class MDKSessionMiddleware(MiddlewareMixin):
      """
      Add an MDK session to the Django request, as well as circuit breaker
      support.
  
      The request object will get a ``mdk_session`` attribute added to it.
      """
-     def __init__(self):
+     def __init__(self, *args, **kwargs):
+         MiddlewareMixin.__init__(self, *args, **kwargs)
          self.mdk = start()
          atexit.register(self.mdk.stop)
  
      def process_request(self, request):
          request.mdk_session = self.mdk.join(
              request.META.get("HTTP_X_MDK_CONTEXT"))
          request.mdk_session.start_interaction()
  
      def process_response(self, request, response):
          request.mdk_session.finish_interaction()
          del request.mdk_session
          return response
  
      def process_exception(self, request, exception):
          request.mdk_session.fail_interaction(
              "".join(format_exception_only(exception.__class__, exception)))
  
Work with Django 1.10 as well.
## Code Before:

import atexit
from traceback import format_exception_only

from mdk import start


class MDKSessionMiddleware(object):
    """
    Add an MDK session to the Django request, as well as circuit breaker
    support.

    The request object will get a ``mdk_session`` attribute added to it.
    """
    def __init__(self):
        self.mdk = start()
        atexit.register(self.mdk.stop)

    def process_request(self, request):
        request.mdk_session = self.mdk.join(
            request.META.get("HTTP_X_MDK_CONTEXT"))
        request.mdk_session.start_interaction()

    def process_response(self, request, response):
        request.mdk_session.finish_interaction()
        del request.mdk_session
        return response

    def process_exception(self, request, exception):
        request.mdk_session.fail_interaction(
            "".join(format_exception_only(exception.__class__, exception)))

## Instruction:
Work with Django 1.10 as well.
## Code After:

import atexit
from traceback import format_exception_only

from mdk import start

# Django 1.10 new-style middleware compatibility:
try:
    from django.utils.deprecation import MiddlewareMixin
except ImportError:
    MiddlewareMixin = object


class MDKSessionMiddleware(MiddlewareMixin):
    """
    Add an MDK session to the Django request, as well as circuit breaker
    support.

    The request object will get a ``mdk_session`` attribute added to it.
    """
    def __init__(self, *args, **kwargs):
        MiddlewareMixin.__init__(self, *args, **kwargs)
        self.mdk = start()
        atexit.register(self.mdk.stop)

    def process_request(self, request):
        request.mdk_session = self.mdk.join(
            request.META.get("HTTP_X_MDK_CONTEXT"))
        request.mdk_session.start_interaction()

    def process_response(self, request, response):
        request.mdk_session.finish_interaction()
        del request.mdk_session
        return response

    def process_exception(self, request, exception):
        request.mdk_session.fail_interaction(
            "".join(format_exception_only(exception.__class__, exception)))
