swiftly/client/directclient.py
def request(self, method, path, contents, headers, decode_json=False,
                stream=False, query=None, cdn=False):
        """
        See :py:func:`swiftly.client.client.Client.request`
        """
        if query:
            path += '?' + '&'.join(
                ('%s=%s' % (quote(k), quote(v)) if v else quote(k))
                for k, v in sorted(six.iteritems(query)))
        reset_func = self._default_reset_func
        if isinstance(contents, six.string_types):
            contents = StringIO(contents)
        tell = getattr(contents, 'tell', None)
        seek = getattr(contents, 'seek', None)
        if tell and seek:
            try:
                orig_pos = tell()
                reset_func = lambda: seek(orig_pos)
            except Exception:
                tell = seek = None
        elif not contents:
            reset_func = lambda: None
        status = 0
        reason = 'Unknown'
        attempt = 0
        while attempt < self.attempts:
            attempt += 1
            if cdn:
                conn_path = self.cdn_path
            else:
                conn_path = self.storage_path
            titled_headers = dict((k.title(), v) for k, v in six.iteritems({
                'User-Agent': self.user_agent}))
            if headers:
                titled_headers.update(
                    (k.title(), v) for k, v in six.iteritems(headers))
            resp = None
            if not hasattr(contents, 'read'):
                if method not in self.no_content_methods and contents and \
                        'Content-Length' not in titled_headers and \
                        'Transfer-Encoding' not in titled_headers:
                    titled_headers['Content-Length'] = str(
                        len(contents or ''))
                req = self.Request.blank(
                    conn_path + path,
                    environ={'REQUEST_METHOD': method, 'swift_owner': True},
                    headers=titled_headers, body=contents)
                verbose_headers = '  '.join(
                    '%s: %s' % (k, v) for k, v in six.iteritems(titled_headers))
                self.verbose(
                    '> %s %s %s', method, conn_path + path, verbose_headers)
                resp = req.get_response(self.swift_proxy)
            else:
                req = self.Request.blank(
                    conn_path + path,
                    environ={'REQUEST_METHOD': method, 'swift_owner': True},
                    headers=titled_headers)
                content_length = None
                for h, v in six.iteritems(titled_headers):
                    if h.lower() == 'content-length':
                        content_length = int(v)
                    req.headers[h] = v
                if method not in self.no_content_methods and \
                        content_length is None:
                    titled_headers['Transfer-Encoding'] = 'chunked'
                    req.headers['Transfer-Encoding'] = 'chunked'
                else:
                    req.content_length = content_length
                req.body_file = contents
                verbose_headers = '  '.join(
                    '%s: %s' % (k, v) for k, v in six.iteritems(titled_headers))
                self.verbose(
                    '> %s %s %s', method, conn_path + path, verbose_headers)
                resp = req.get_response(self.swift_proxy)
            status = resp.status_int
            reason = resp.status.split(' ', 1)[1]
            hdrs = headers_to_dict(resp.headers.items())
            if stream:
                def iter_reader(size=-1):
                    if size == -1:
                        return ''.join(resp.app_iter)
                    else:
                        try:
                            return next(resp.app_iter)
                        except StopIteration:
                            return ''
                iter_reader.read = iter_reader
                value = iter_reader
            else:
                value = resp.body
            self.verbose('< %s %s', status, reason)
            if status and status // 100 != 5:
                if not stream and decode_json and status // 100 == 2:
                    if value:
                        value = json.loads(value)
                    else:
                        value = None
                return (status, reason, hdrs, value)
            if reset_func:
                reset_func()
            self.sleep(2 ** attempt)
        raise Exception('%s %s failed: %s %s' % (method, path, status, reason))
def request(self, method, path, contents, headers, decode_json=False,
                stream=False, query=None, cdn=False):
        """
        See :py:func:`swiftly.client.client.Client.request`
        """
        if query:
            path += '?' + '&'.join(
                ('%s=%s' % (quote(k), quote(v)) if v else quote(k))
                for k, v in sorted(six.iteritems(query)))
        reset_func = self._default_reset_func
        if isinstance(contents, six.string_types):
            contents = StringIO(contents)
        tell = getattr(contents, 'tell', None)
        seek = getattr(contents, 'seek', None)
        if tell and seek:
            try:
                orig_pos = tell()
                reset_func = lambda: seek(orig_pos)
            except Exception:
                tell = seek = None
        elif not contents:
            reset_func = lambda: None
        status = 0
        reason = 'Unknown'
        attempt = 0
        while attempt < self.attempts:
            attempt += 1
            if cdn:
                conn_path = self.cdn_path
            else:
                conn_path = self.storage_path
            titled_headers = dict((k.title(), v) for k, v in six.iteritems({
                'User-Agent': self.user_agent}))
            if headers:
                titled_headers.update(
                    (k.title(), v) for k, v in six.iteritems(headers))
            resp = None
            if not hasattr(contents, 'read'):
                if method not in self.no_content_methods and contents and \
                        'Content-Length' not in titled_headers and \
                        'Transfer-Encoding' not in titled_headers:
                    titled_headers['Content-Length'] = str(
                        len(contents or ''))
                req = self.Request.blank(
                    conn_path + path,
                    environ={'REQUEST_METHOD': method, 'swift_owner': True},
                    headers=titled_headers, body=contents)
                verbose_headers = '  '.join(
                    '%s: %s' % (k, v) for k, v in six.iteritems(titled_headers))
                self.verbose(
                    '> %s %s %s', method, conn_path + path, verbose_headers)
                resp = req.get_response(self.swift_proxy)
            else:
                req = self.Request.blank(
                    conn_path + path,
                    environ={'REQUEST_METHOD': method, 'swift_owner': True},
                    headers=titled_headers)
                content_length = None
                for h, v in six.iteritems(titled_headers):
                    if h.lower() == 'content-length':
                        content_length = int(v)
                    req.headers[h] = v
                if method not in self.no_content_methods and \
                        content_length is None:
                    titled_headers['Transfer-Encoding'] = 'chunked'
                    req.headers['Transfer-Encoding'] = 'chunked'
                else:
                    req.content_length = content_length
                req.body_file = contents
                verbose_headers = '  '.join(
                    '%s: %s' % (k, v) for k, v in six.iteritems(titled_headers))
                self.verbose(
                    '> %s %s %s', method, conn_path + path, verbose_headers)
                resp = req.get_response(self.swift_proxy)
            status = resp.status_int
            reason = resp.status.split(' ', 1)[1]
            hdrs = headers_to_dict(resp.headers.items())
            if stream:
                def iter_reader(size=-1):
                    if size == -1:
                        return ''.join(resp.app_iter)
                    else:
                        try:
                            return next(resp.app_iter)
                        except StopIteration:
                            return ''
                iter_reader.read = iter_reader
                value = iter_reader
            else:
                value = resp.body
            self.verbose('< %s %s', status, reason)
            if status and status // 100 != 5:
                if not stream and decode_json and status // 100 == 2:
                    if value:
                        value = json.loads(value)
                    else:
                        value = None
                return (status, reason, hdrs, value)
            if reset_func:
                reset_func()
            self.sleep(2 ** attempt)
        raise Exception('%s %s failed: %s %s' % (method, path, status, reason))
See :py:func:`swiftly.client.client.Client.request`