def _api_request(self, uri, method, **kwargs): """ Manages the request by adding any auth information, and retries the request after authenticating if the initial request returned and Unauthorized exception. """ id_svc = pyrax.identity if not all((self.management_url, id_svc.token, id_svc.tenant_id)): id_svc.authenticate() if not self.management_url: # We've authenticated but no management_url has been set. This # indicates that the service is not available. raise exc.ServiceNotAvailable("The '%s' service is not available." % self) # Perform the request once. If we get a 401 back then it # might be because the auth token expired, so try to # re-authenticate and try again. If it still fails, bail. try: kwargs.setdefault("headers", {})["X-Auth-Token"] = id_svc.token if id_svc.tenant_id: kwargs["headers"]["X-Auth-Project-Id"] = id_svc.tenant_id resp, body = self._time_request(self.management_url + quote(uri), method, **kwargs) return resp, body except exc.Unauthorized as ex: try: id_svc.authenticate() kwargs["headers"]["X-Auth-Token"] = id_svc.token resp, body = self._time_request(self.management_url + uri, method, **kwargs) return resp, body except exc.Unauthorized: raise ex
def _api_request(self, uri, method, **kwargs): """ Manages the request by adding any auth information, and retries the request after authenticating if the initial request returned and Unauthorized exception. """ id_svc = pyrax.identity if not all((self.management_url, id_svc.token, id_svc.tenant_id)): id_svc.authenticate() if not self.management_url: # We've authenticated but no management_url has been set. This # indicates that the service is not available. raise exc.ServiceNotAvailable( "The '%s' service is not available." % self) if uri.startswith("http"): parsed = list(urlparse.urlparse(uri)) for pos, item in enumerate(parsed): if pos < 2: # Don't escape the scheme or netloc continue parsed[pos] = urllib.quote(parsed[pos], safe="/.?&=,") safe_uri = urlparse.urlunparse(parsed) else: safe_uri = "%s%s" % (self.management_url, urllib.quote(uri, safe="/.?&=,")) # Perform the request once. If we get a 401 back then it # might be because the auth token expired, so try to # re-authenticate and try again. If it still fails, bail. try: kwargs.setdefault("headers", {})["X-Auth-Token"] = id_svc.token if id_svc.tenant_id: kwargs["headers"]["X-Auth-Project-Id"] = id_svc.tenant_id resp, body = self._time_request(safe_uri, method, **kwargs) return resp, body except exc.Unauthorized as ex: try: id_svc.authenticate() kwargs["headers"]["X-Auth-Token"] = id_svc.token resp, body = self._time_request(safe_uri, method, **kwargs) return resp, body except exc.Unauthorized: raise ex