import json
from functools import partial

from . import handlers  # noqa
from .message import send_error
from .registry import HANDLERS

REQUIRED_FIELDS = ['type', 'id']


try:
    DECODE_ERRORS = (json.JSONDecodeError, TypeError)
except AttributeError:
    # Python 3.3 and 3.4 raise a ValueError instead of json.JSONDecodeError
    DECODE_ERRORS = (ValueError, TypeError)


def handle_rohrpost_message(message):
    """
    Handling of a rohrpost message will validate the required format:
    A valid JSON object including at least an "id" and "type" field.
    It then hands off further handling to the registered handler (if any).
    """
    _send_error = partial(send_error, message, None, None)
    if not message.content['text']:
        return _send_error('Received empty message.')

    try:
        request = json.loads(message.content['text'])
    except DECODE_ERRORS as e:
        return _send_error('Could not decode JSON message. Error: {}'.format(str(e)))

    if not isinstance(request, dict):
        return _send_error('Expected a JSON object as message.')

    for field in REQUIRED_FIELDS:
        if field not in request:
            return _send_error("Missing required field '{}'.".format(field))

    if not request['type'] in HANDLERS:
        return send_error(
            message, request['id'], request['type'],
            "Unknown message type '{}'.".format(request['type']),
        )

    HANDLERS[request['type']](message, request)

import json
from functools import partial

from . import handlers  # noqa
from .message import send_error
from .registry import HANDLERS

REQUIRED_FIELDS = ['type', 'id']


try:
    DECODE_ERRORS = (json.JSONDecodeError, TypeError)
except AttributeError:
    # Python 3.3 and 3.4 raise a ValueError instead of json.JSONDecodeError
    DECODE_ERRORS = (ValueError, TypeError)


def handle_rohrpost_message(message):
    """
    Handling of a rohrpost message will validate the required format:
    A valid JSON object including at least an "id" and "type" field.
    It then hands off further handling to the registered handler (if any).
    """
    _send_error = partial(send_error, message=message, message_id=None, handler=None)
    if not message.content['text']:
        return _send_error(error='Received empty message.')

    try:
        request = json.loads(message.content['text'])
    except DECODE_ERRORS as e:
        return _send_error(error='Could not decode JSON message. Error: {}'.format(str(e)))

    if not isinstance(request, dict):
        return _send_error(error='Expected a JSON object as message.')

    for field in REQUIRED_FIELDS:
        if field not in request:
            return _send_error(error="Missing required field '{}'.".format(field))

    if not request['type'] in HANDLERS:
        return send_error(
            message=message, message_id=request['id'], handler=request['type'],
            error="Unknown message type '{}'.".format(request['type']),
        )

    HANDLERS[request['type']](message, request)

Use keyword arguments in code
Use keyword arguments in code

  import json
  from functools import partial
  
  from . import handlers  # noqa
  from .message import send_error
  from .registry import HANDLERS
  
  REQUIRED_FIELDS = ['type', 'id']
  
  
  try:
      DECODE_ERRORS = (json.JSONDecodeError, TypeError)
  except AttributeError:
      # Python 3.3 and 3.4 raise a ValueError instead of json.JSONDecodeError
      DECODE_ERRORS = (ValueError, TypeError)
  
  
  def handle_rohrpost_message(message):
      """
      Handling of a rohrpost message will validate the required format:
      A valid JSON object including at least an "id" and "type" field.
      It then hands off further handling to the registered handler (if any).
      """
-     _send_error = partial(send_error, message, None, None)
+     _send_error = partial(send_error, message=message, message_id=None, handler=None)
      if not message.content['text']:
-         return _send_error('Received empty message.')
+         return _send_error(error='Received empty message.')
  
      try:
          request = json.loads(message.content['text'])
      except DECODE_ERRORS as e:
-         return _send_error('Could not decode JSON message. Error: {}'.format(str(e)))
+         return _send_error(error='Could not decode JSON message. Error: {}'.format(str(e)))
  
      if not isinstance(request, dict):
-         return _send_error('Expected a JSON object as message.')
+         return _send_error(error='Expected a JSON object as message.')
  
      for field in REQUIRED_FIELDS:
          if field not in request:
-             return _send_error("Missing required field '{}'.".format(field))
+             return _send_error(error="Missing required field '{}'.".format(field))
  
      if not request['type'] in HANDLERS:
          return send_error(
-             message, request['id'], request['type'],
+             message=message, message_id=request['id'], handler=request['type'],
-             "Unknown message type '{}'.".format(request['type']),
+             error="Unknown message type '{}'.".format(request['type']),
          )
  
      HANDLERS[request['type']](message, request)
  
Use keyword arguments in code
## Code Before:
import json
from functools import partial

from . import handlers  # noqa
from .message import send_error
from .registry import HANDLERS

REQUIRED_FIELDS = ['type', 'id']


try:
    DECODE_ERRORS = (json.JSONDecodeError, TypeError)
except AttributeError:
    # Python 3.3 and 3.4 raise a ValueError instead of json.JSONDecodeError
    DECODE_ERRORS = (ValueError, TypeError)


def handle_rohrpost_message(message):
    """
    Handling of a rohrpost message will validate the required format:
    A valid JSON object including at least an "id" and "type" field.
    It then hands off further handling to the registered handler (if any).
    """
    _send_error = partial(send_error, message, None, None)
    if not message.content['text']:
        return _send_error('Received empty message.')

    try:
        request = json.loads(message.content['text'])
    except DECODE_ERRORS as e:
        return _send_error('Could not decode JSON message. Error: {}'.format(str(e)))

    if not isinstance(request, dict):
        return _send_error('Expected a JSON object as message.')

    for field in REQUIRED_FIELDS:
        if field not in request:
            return _send_error("Missing required field '{}'.".format(field))

    if not request['type'] in HANDLERS:
        return send_error(
            message, request['id'], request['type'],
            "Unknown message type '{}'.".format(request['type']),
        )

    HANDLERS[request['type']](message, request)

## Instruction:
Use keyword arguments in code
## Code After:
import json
from functools import partial

from . import handlers  # noqa
from .message import send_error
from .registry import HANDLERS

REQUIRED_FIELDS = ['type', 'id']


try:
    DECODE_ERRORS = (json.JSONDecodeError, TypeError)
except AttributeError:
    # Python 3.3 and 3.4 raise a ValueError instead of json.JSONDecodeError
    DECODE_ERRORS = (ValueError, TypeError)


def handle_rohrpost_message(message):
    """
    Handling of a rohrpost message will validate the required format:
    A valid JSON object including at least an "id" and "type" field.
    It then hands off further handling to the registered handler (if any).
    """
    _send_error = partial(send_error, message=message, message_id=None, handler=None)
    if not message.content['text']:
        return _send_error(error='Received empty message.')

    try:
        request = json.loads(message.content['text'])
    except DECODE_ERRORS as e:
        return _send_error(error='Could not decode JSON message. Error: {}'.format(str(e)))

    if not isinstance(request, dict):
        return _send_error(error='Expected a JSON object as message.')

    for field in REQUIRED_FIELDS:
        if field not in request:
            return _send_error(error="Missing required field '{}'.".format(field))

    if not request['type'] in HANDLERS:
        return send_error(
            message=message, message_id=request['id'], handler=request['type'],
            error="Unknown message type '{}'.".format(request['type']),
        )

    HANDLERS[request['type']](message, request)
