import json

from rbtools.api.utils import parse_mimetype


DECODER_MAP = {}


def DefaultDecoder(payload):
    """Default decoder for API payloads.

    The default decoder is used when a decoder is not found in the
    DECODER_MAP. This is a last resort which should only be used when
    something has gone wrong.
    """
    return {
        'resource': {
            'data': payload,
        },
    }

DEFAULT_DECODER = DefaultDecoder


def JsonDecoder(payload):
    return json.loads(payload)

DECODER_MAP['application/json'] = JsonDecoder


def PlainTextDecoder(payload):
    return {
        'resource': {
            'text': payload,
        },
    }

DECODER_MAP['text/plain'] = PlainTextDecoder


def PatchDecoder(payload):
    return {
        'resource': {
            'diff': payload,
        },
    }

DECODER_MAP['text/x-patch'] = PatchDecoder


def decode_response(payload, mime_type):
    """Decode a Web API response.

    The body of a Web API response will be decoded into a dictionary,
    according to the provided mime_type.
    """
    if not payload:
        return {}

    mime = parse_mimetype(mime_type)

    format = '%s/%s' % (mime['main_type'], mime['format'])

    if format in DECODER_MAP:
        decoder = DECODER_MAP[format]
    else:
        decoder = DEFAULT_DECODER

    return decoder(payload)

import json

from rbtools.api.utils import parse_mimetype


DECODER_MAP = {}


def DefaultDecoder(payload):
    """Default decoder for API payloads.

    The default decoder is used when a decoder is not found in the
    DECODER_MAP. This is a last resort which should only be used when
    something has gone wrong.
    """
    return {
        'resource': {
            'data': payload,
        },
    }

DEFAULT_DECODER = DefaultDecoder


def JsonDecoder(payload):
    return json.loads(payload)

DECODER_MAP['application/json'] = JsonDecoder


def PlainTextDecoder(payload):
    return {
        'resource': {
            'text': payload,
        },
    }

DECODER_MAP['text/plain'] = PlainTextDecoder


def PatchDecoder(payload):
    return {
        'resource': {
            'diff': payload,
        },
    }

DECODER_MAP['text/x-patch'] = PatchDecoder


def decode_response(payload, mime_type):
    """Decode a Web API response.

    The body of a Web API response will be decoded into a dictionary,
    according to the provided mime_type.
    """
    mime = parse_mimetype(mime_type)

    format = '%s/%s' % (mime['main_type'], mime['format'])

    if format in DECODER_MAP:
        decoder = DECODER_MAP[format]
    else:
        decoder = DEFAULT_DECODER

    return decoder(payload)

Fix decoding of empty responses.
Fix decoding of empty responses.

When an empty response was given by the web server, we were
decoding to '{}', which is problematic when you are expecting
an empty response for things like empty text/plain files. This
fixes the problematic assumption we were making.

Reviewed at http://reviews.reviewboard.org/r/3680/

  import json
  
  from rbtools.api.utils import parse_mimetype
  
  
  DECODER_MAP = {}
  
  
  def DefaultDecoder(payload):
      """Default decoder for API payloads.
  
      The default decoder is used when a decoder is not found in the
      DECODER_MAP. This is a last resort which should only be used when
      something has gone wrong.
      """
      return {
          'resource': {
              'data': payload,
          },
      }
  
  DEFAULT_DECODER = DefaultDecoder
  
  
  def JsonDecoder(payload):
      return json.loads(payload)
  
  DECODER_MAP['application/json'] = JsonDecoder
  
  
  def PlainTextDecoder(payload):
      return {
          'resource': {
              'text': payload,
          },
      }
  
  DECODER_MAP['text/plain'] = PlainTextDecoder
  
  
  def PatchDecoder(payload):
      return {
          'resource': {
              'diff': payload,
          },
      }
  
  DECODER_MAP['text/x-patch'] = PatchDecoder
  
  
  def decode_response(payload, mime_type):
      """Decode a Web API response.
  
      The body of a Web API response will be decoded into a dictionary,
      according to the provided mime_type.
      """
-     if not payload:
-         return {}
- 
      mime = parse_mimetype(mime_type)
  
      format = '%s/%s' % (mime['main_type'], mime['format'])
  
      if format in DECODER_MAP:
          decoder = DECODER_MAP[format]
      else:
          decoder = DEFAULT_DECODER
  
      return decoder(payload)
  
Fix decoding of empty responses.
## Code Before:
import json

from rbtools.api.utils import parse_mimetype


DECODER_MAP = {}


def DefaultDecoder(payload):
    """Default decoder for API payloads.

    The default decoder is used when a decoder is not found in the
    DECODER_MAP. This is a last resort which should only be used when
    something has gone wrong.
    """
    return {
        'resource': {
            'data': payload,
        },
    }

DEFAULT_DECODER = DefaultDecoder


def JsonDecoder(payload):
    return json.loads(payload)

DECODER_MAP['application/json'] = JsonDecoder


def PlainTextDecoder(payload):
    return {
        'resource': {
            'text': payload,
        },
    }

DECODER_MAP['text/plain'] = PlainTextDecoder


def PatchDecoder(payload):
    return {
        'resource': {
            'diff': payload,
        },
    }

DECODER_MAP['text/x-patch'] = PatchDecoder


def decode_response(payload, mime_type):
    """Decode a Web API response.

    The body of a Web API response will be decoded into a dictionary,
    according to the provided mime_type.
    """
    if not payload:
        return {}

    mime = parse_mimetype(mime_type)

    format = '%s/%s' % (mime['main_type'], mime['format'])

    if format in DECODER_MAP:
        decoder = DECODER_MAP[format]
    else:
        decoder = DEFAULT_DECODER

    return decoder(payload)

## Instruction:
Fix decoding of empty responses.
## Code After:
import json

from rbtools.api.utils import parse_mimetype


DECODER_MAP = {}


def DefaultDecoder(payload):
    """Default decoder for API payloads.

    The default decoder is used when a decoder is not found in the
    DECODER_MAP. This is a last resort which should only be used when
    something has gone wrong.
    """
    return {
        'resource': {
            'data': payload,
        },
    }

DEFAULT_DECODER = DefaultDecoder


def JsonDecoder(payload):
    return json.loads(payload)

DECODER_MAP['application/json'] = JsonDecoder


def PlainTextDecoder(payload):
    return {
        'resource': {
            'text': payload,
        },
    }

DECODER_MAP['text/plain'] = PlainTextDecoder


def PatchDecoder(payload):
    return {
        'resource': {
            'diff': payload,
        },
    }

DECODER_MAP['text/x-patch'] = PatchDecoder


def decode_response(payload, mime_type):
    """Decode a Web API response.

    The body of a Web API response will be decoded into a dictionary,
    according to the provided mime_type.
    """
    mime = parse_mimetype(mime_type)

    format = '%s/%s' % (mime['main_type'], mime['format'])

    if format in DECODER_MAP:
        decoder = DECODER_MAP[format]
    else:
        decoder = DEFAULT_DECODER

    return decoder(payload)
