code: func newTenantURLProviderFromTenant(t *app.UserService, token string, proxyURL string) (*tenantURLProvider, error) {
  
  	if t.ID == nil {
  		log.Error(nil, map[string]interface{}{}, "app.UserService is malformed: no ID field")
  		return nil, errs.New("app.UserService is malformed: no ID field")
  	}
  
  	if t.Attributes == nil {
  		log.Error(nil, map[string]interface{}{
  			"tenant": *t.ID,
  		}, "app.UserService is malformed: no Attribute field ID=%s", *t.ID)
  		return nil, errs.Errorf("app.UserService is malformed: no Attribute field (ID=%s)", *t.ID)
  	}
  
  	if len(t.Attributes.Namespaces) == 0 {
  		log.Error(nil, map[string]interface{}{
  			"tenant": *t.ID,
  		}, "this tenant has no namespaces: %s", *t.ID)
  		return nil, errs.Errorf("app.UserService is malformed: no Namespaces (ID=%s)", *t.ID)
  	}
  
  	defaultNamespace := t.Attributes.Namespaces[0]
  	namespaceMap := make(map[string]*app.NamespaceAttributes)
  	for i, namespace := range t.Attributes.Namespaces {
  		namespaceMap[*namespace.Name] = t.Attributes.Namespaces[i]
  		if namespace.Type != nil && *namespace.Type == "user" {
  			defaultNamespace = namespace
  		}
  	}
  
  	defaultClusterURL := *defaultNamespace.ClusterURL
  
  	if len(proxyURL) != 0 {
  		// all non-metric API calls go via the proxy
  		defaultClusterURL = proxyURL
  	}
  
  	provider := &tenantURLProvider{
  		apiURL:     defaultClusterURL,
  		apiToken:   token,
  		tenant:     t,
  		namespaces: namespaceMap,
  	}
  	return provider, nil
  }
docs: // newTenantURLProviderFromTenant create a provider from a UserService object