import StringIO
import email
import json
import urllib
from collections import defaultdict
from django.http import MultiPartParser
from django.utils.translation import ugettext as _
from django.core.cache import get_cache
from lrs.util import etag, convert_to_dict, convert_post_body_to_dict
from lrs.util.jws import JWS, JWSException
from lrs.exceptions import OauthUnauthorized, ParamError, BadRequest
from oauth_provider.oauth.oauth import OAuthError
from oauth_provider.utils import send_oauth_error
from oauth_provider.decorators import CheckOAuth

att_cache = get_cache('attachment_cache')

def parse(request, more_id=None):
    r_dict = {}
    # Build headers from request in request dict
    r_dict['headers'] = get_headers(request.META)
    
    # Traditional authorization should be passed in headers
    r_dict['auth'] = {}
    if 'Authorization' in r_dict['headers']:
        # OAuth will always be dict, not http auth. Set required fields for oauth module and type for authentication
        # module
        set_authorization(r_dict, request)     
    elif 'Authorization' in request.body or 'HTTP_AUTHORIZATION' in request.body:
        # Authorization could be passed into body if cross origin request
        r_dict['auth']['type'] = 'http'