import simplejson as json
import ordereddict


class ParsedEnviron(object):
    def __init__(self, environ):
        self.environ = environ
        if self.get("CONTENT_LENGTH") != 0:
            content = self.environ['wsgi.input'].read(int(self.get("CONTENT_LENGTH")))
            self.body = json.loads(content, object_pairs_hook=ordereddict.OrderedDict)
        else:
            self.body = ""

    def get(self, name):
        if name == "BODY":
            return self.body
        else:
            try:
                return self.environ.get(name, 0)
            except:
                raise AttributeError(name + " is not set!")
