def authenticate(self): """ Does authentication """ headers = { 'X-Storage-User': self.username, 'X-Storage-Pass': self.api_key, 'Content-Length': '0' } response = requests.get(self.auth_url, headers=headers, verify=False) 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 make_request(self, method, url=None, *args, **kwargs): """ Makes a request """ _headers = kwargs.get('headers', {}) headers = self.get_headers() if _headers: headers.update(_headers) kwargs['headers'] = headers if 'verify' not in kwargs: kwargs['verify'] = False formatter = None if 'formatter' in kwargs: formatter = kwargs.get('formatter') del kwargs['formatter'] res = requests.request(method, url, *args, **kwargs) if kwargs.get('return_response', True): res = self._check_success(res) if res.status_code == 401: raise errors.AuthenticationError('Authentication error') if res.status_code == 404: raise errors.NotFound('Not found') if res.error: try: raise res.raise_for_status() except Exception, ex: raise errors.ResponseError(res.status_code, str(ex))
def _authenticate(self, response): 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.get('x-auth-token', 'huh?') self.storage_url = self.get_storage_url(storage_options) if not self.storage_url: self.storage_url = response.headers['x-storage-url'] raise errors.StorageURLNotFound( "Could not find defined storage URL. Using default.") if not self.auth_token or not self.storage_url: raise errors.AuthenticationError('Invalid Authentication Response')