def prepare_body(self, data, files, json=None):
<EOL>body = None<EOL>content_type = None<EOL>if not data and json is not None:<EOL><INDENT>content_type = '<STR_LIT:application/json>'<EOL>body = complexjson.dumps(json)<EOL>if not isinstance(body, bytes):<EOL><INDENT>body = body.encode('<STR_LIT:utf-8>')<EOL><DEDENT><DEDENT>is_stream = all([<EOL>hasattr(data, '<STR_LIT>'),<EOL>not isinstance(data, (basestring, list, tuple, Mapping))<EOL>])<EOL>try:<EOL><INDENT>length = super_len(data)<EOL><DEDENT>except (TypeError, AttributeError, UnsupportedOperation):<EOL><INDENT>length = None<EOL><DEDENT>if is_stream:<EOL><INDENT>body = data<EOL>if getattr(body, '<STR_LIT>', None) is not None:<EOL><INDENT>try:<EOL><INDENT>self._body_position = body.tell()<EOL><DEDENT>except (IOError, OSError):<EOL><INDENT>self._body_position = object()<EOL><DEDENT><DEDENT>if files:<EOL><INDENT>raise NotImplementedError('<STR_LIT>')<EOL><DEDENT>if length:<EOL><INDENT>self.headers['<STR_LIT>'] = builtin_str(length)<EOL><DEDENT>else:<EOL><INDENT>self.headers['<STR_LIT>'] = '<STR_LIT>'<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if files:<EOL><INDENT>(body, content_type) = self._encode_files(files, data)<EOL><DEDENT>else:<EOL><INDENT>if data:<EOL><INDENT>body = self._encode_params(data)<EOL>if isinstance(data, basestring) or hasattr(data, '<STR_LIT>'):<EOL><INDENT>content_type = None<EOL><DEDENT>else:<EOL><INDENT>content_type = '<STR_LIT>'<EOL><DEDENT><DEDENT><DEDENT>self.prepare_content_length(body)<EOL>if content_type and ('<STR_LIT>' not in self.headers):<EOL><INDENT>self.headers['<STR_LIT:Content-Type>'] = content_type<EOL><DEDENT><DEDENT>self.body = body<EOL>
Prepares the given HTTP body data.