Beispiel #1
0
    def __init__(self, iterable, status, headers):
        self._text = None

        self._content = b''.join(iterable)
        if hasattr(iterable, 'close'):
            iterable.close()

        self._status = status
        self._status_code = int(status[:3])
        self._headers = CiDict(headers)

        cookies = SimpleCookie()
        for name, value in headers.items():
            if name.lower() == 'set-cookie':
                cookies.load(value)

        self._cookies = dict(
            (morsel.key, Cookie(morsel)) for morsel in cookies.values())

        self._encoding = content_type_encoding(
            self._headers.get('content-type'))
Beispiel #2
0
    def __init__(self,
                 uri,
                 timeout=(2, 8),
                 auth=None,
                 verify=True,
                 cert=None,
                 default_region='default',
                 default_interface='public'):

        self.uri = uri
        self.auth = auth
        self.timeout = timeout
        self.verify = verify
        self.cert = cert
        self.headers = CiDict()
        self.endpoints = Endpoints(default_region=default_region,
                                   default_interface=default_interface)
        try:
            self.collect_endpoints()
        except AttributeError:
            pass
Beispiel #3
0
 def headers(self):
     if self._cached_headers is None:
         self._cached_headers = CiDict()
         self._cached_headers.update(self._result.headers)
     return self._cached_headers
Beispiel #4
0
class Response(object):
    __slots__ = (
        '_result',
        '_cached_json',
        '_cached_headers',
    )

    def __init__(self, requests_response):
        self._cached_json = None
        self._cached_headers = None
        self._result = requests_response

    @property
    def iter_content(self):
        return self._result.iter_content()

    @property
    def iter_lines(self):
        return self._result.iter_lines()

    @property
    def content(self):
        return self._result.content

    @property
    def headers(self):
        if self._cached_headers is None:
            self._cached_headers = CiDict()
            self._cached_headers.update(self._result.headers)
        return self._cached_headers

    @property
    def text(self):
        return if_bytes_to_unicode(self.content, self.encoding)

    @property
    def status_code(self):
        return int(self._result.status_code)

    def __len__(self):
        return len(self.body)

    @property
    def content_type(self):
        try:
            header = self.headers['content-type']
            content_type, params = cgi.parse_header(header)
            if content_type is not None:
                return str(content_type).upper()
            else:
                return None
        except KeyError:
            return None

    @property
    def json(self):
        if self._cached_json is None:
            if self.encoding is not None and self.encoding != 'UTF-8':
                raise HTTPClientContentDecodingError(
                    'JSON requires UTF-8 Encoding') from None

            try:
                if self.status_code != 204:
                    self._cached_json = js.loads(self.content)
                else:
                    self._cached_json = None

            except JSONDecodeError as e:
                raise HTTPClientContentDecodingError('JSON Decode: %s' %
                                                     e) from None

        return self._cached_json

    @property
    def encoding(self):
        try:
            header = self.headers['content_type']
            content_type, params = cgi.parse_header(header)
            if 'charset' in params:
                return params['charset'].strip("'\"").upper()
            if 'text' in content_type:
                return 'ISO-8859-1'
        except KeyError:
            pass

        return self._result.encoding.upper()

    def close(self):
        self._result.close()
Beispiel #5
0
 def headers(self):
     headers = [(name.lower(), value) for name, value in self._headers]
     return CiDict(headers)
Beispiel #6
0
class Result(object):
    """Result of a WSGI request.

    The args provided are the values returned by the WSGI Application. This
    'class' provides a simple interface to access the encapsulated data.

    Args:
        iterable (iterable): Payload returned by WSGI Application.
        status (str): HTTP status string returned by WSGI Application.
        headers (list): List of (name, value) tuples.

    Attributes:
        status (str): HTTP status string.
        status_code (int): HTTP status code.
        headers (CiDict): Case-insensitive dictionary
        cookies (dict): Dictionary of Cookies.
        encoding (str): Encoding type. e.g. UTF-8
        text (str): Decoded response body.
        json (dict): Deserialized JSON body.
        content (bytes): Raw response body
    """
    def __init__(self, iterable, status, headers):
        self._text = None

        self._content = b''.join(iterable)
        if hasattr(iterable, 'close'):
            iterable.close()

        self._status = status
        self._status_code = int(status[:3])
        self._headers = CiDict(headers)

        cookies = SimpleCookie()
        for name, value in headers.items():
            if name.lower() == 'set-cookie':
                cookies.load(value)

        self._cookies = dict(
            (morsel.key, Cookie(morsel))
            for morsel in cookies.values()
        )

        self._encoding = content_type_encoding(
            self._headers.get('content-type'))

    @property
    def status(self):
        return self._status

    @property
    def status_code(self):
        return self._status_code

    @property
    def headers(self):
        return self._headers

    @property
    def cookies(self):
        return self._cookies

    @property
    def encoding(self):
        return self._encoding

    @property
    def content(self):
        return self._content

    @property
    def text(self):
        if self._text is None:
            if not self.content:
                self._text = u''
            else:
                if self.encoding is None:
                    encoding = 'UTF-8'
                else:
                    encoding = self.encoding

                self._text = self.content.decode(encoding)

        return self._text

    @property
    def json(self):
        if not self.text:
            return None

        return json.loads(self.text)