def _make_request(headers):
     logger.debug("%s %s %s" % (method, url, headers))
     res, content = self.http.request(url, method, headers=headers, body=data)
     response = Response()
     response.headers = res
     response.status_code = int(res.status)
     response.content = content
     return response
 def _make_request(headers):
     logger.debug("%s %s %s" % (method, url, headers))
     res, content = self.http.request(url, method,
                                      headers=headers,
                                      body=data)
     response = Response()
     response.headers = res
     response.status_code = int(res.status)
     response.content = content
     return response
def complete_request(resp, callback=None, load_body=True):
    r = Response()
    r.status_code = resp.code
    r.version = resp.version
    r.phrase = resp.phrase

    for k, v in resp.headers.getAllRawHeaders():
        r.headers[k.lower()] = v.pop()

    if r.status_code == 404:
        raise NotFound('Not found')
    r.raise_for_status()

    if not load_body:
        if callback:
            return callback(r)
        return r

    def build_response(body):
        r.content = body
        if callback:
            return callback(r)
        return r

    finished = Deferred()
    resp.deliverBody(FullBodyReader(finished))

    finished.addCallback(build_response)
    return finished
    def authenticate(self):
        """ Does authentication """
        headers = {
            'X-Storage-User': self.username,
            'X-Storage-Pass': self.api_key,
            'Content-Length': '0'
        }
        http = httplib2.Http()
        http.disable_ssl_certificate_validation = True
        res, content = http.request(self.auth_url, 'GET', headers=headers)
        response = Response()
        response.headers = res
        response.status_code = int(res.status)
        response.content = content

        if response.status_code == 401:
            raise errors.AuthenticationError('Invalid Credentials')
        response.raise_for_status()
        try:
            storage_options = json.loads(response.content)['storage']
        except ValueError:
            raise errors.StorageURLNotFound("Could not parse services JSON.")

        self.auth_token = response.headers['x-auth-token']
        self.storage_url = self.get_storage_url(storage_options)
        if not self.storage_url:
            self.storage_url = response.headers['x-storage-url']
        if not self.auth_token or not self.storage_url:
            raise errors.AuthenticationError('Invalid Authentication Response')
    def authenticate(self):
        """ Does authentication """
        headers = {'X-Storage-User': self.username,
                   'X-Storage-Pass': self.api_key,
                   'Content-Length': '0'}
        http = httplib2.Http()
        http.disable_ssl_certificate_validation = True
        res, content = http.request(self.auth_url, 'GET', headers=headers)
        response = Response()
        response.headers = res
        response.status_code = int(res.status)
        response.content = content

        if response.status_code == 401:
            raise errors.AuthenticationError('Invalid Credentials')
        response.raise_for_status()
        try:
            storage_options = json.loads(response.content)['storage']
        except ValueError:
            raise errors.StorageURLNotFound("Could not parse services JSON.")

        self.auth_token = response.headers['x-auth-token']
        self.storage_url = self.get_storage_url(storage_options)
        if not self.storage_url:
            self.storage_url = response.headers['x-storage-url']
        if not self.auth_token or not self.storage_url:
            raise errors.AuthenticationError('Invalid Authentication Response')
def complete_request(resp, callback=None, load_body=True):
    r = Response()
    r.status_code = resp.code
    r.version = resp.version
    r.phrase = resp.phrase

    for k, v in resp.headers.getAllRawHeaders():
        r.headers[k.lower()] = v.pop()

    if r.status_code == 404:
        raise NotFound('Not found')
    r.raise_for_status()

    if not load_body:
        if callback:
            return callback(r)
        return r

    def build_response(body):
        r.content = body
        if callback:
            return callback(r)
        return r

    finished = Deferred()
    resp.deliverBody(FullBodyReader(finished))

    finished.addCallback(build_response)
    return finished
    def authenticate(self):
        """ Does authentication """
        headers = {'X-Storage-User': self.username,
                   'X-Storage-Pass': self.api_key,
                   'Content-Length': '0'}
        http = httplib2.Http()
        http.disable_ssl_certificate_validation = True
        if self.bluemix:
            userAndPass = b64encode(bytes(self.username + ':' +
                                    self.api_key,
                                    'utf-8')).decode("ISO-8859-1")
            bluemix_headers = {'Authorization': 'Basic %s' % userAndPass}
            res, content = http.request(self.auth_url + '/' +
                                        self.bluemixappname,
                                        'GET', headers=bluemix_headers)
        else:
            res, content = http.request(self.auth_url, 'GET', headers=headers)

        response = Response()
        response.headers = res
        response.status_code = int(res.status)
        response.content = content

        if response.status_code == 401:
            raise errors.AuthenticationError('Invalid Credentials')
        response.raise_for_status()
        if not self.bluemix:
            try:
                storage_options = json.loads(response.content)['storage']
            except ValueError:
                raise errors.StorageURLNotFound("Could not parse "
                                                "services JSON.")
            self.storage_url = self.get_storage_url(storage_options)

        self.auth_token = response.headers['x-auth-token']
        if not self.storage_url:
            self.storage_url = response.headers['x-storage-url']
        if not self.auth_token or not self.storage_url:
            raise errors.AuthenticationError('Invalid Authentication Response')