@@ -4,20 +4,47 @@ pub mod types;
44
55/// Construct the Semantic Layer API base URL from a dbt Cloud host.
66///
7- /// Multi-tenant hosts (cloud.getdbt.com, emea. dbt.com, au. dbt.com)
8- /// use the pattern `https://semantic-layer.{host}`.
7+ /// Multi-tenant: `https://tk626.us1. dbt.com` → `https://tk626.semantic-layer.us1. dbt.com`
8+ /// Legacy: `https://cloud.getdbt.com` → `https:// semantic-layer.cloud.getdbt.com`
99pub fn semantic_layer_url ( host : & str ) -> String {
10- let host = host
11- . trim_start_matches ( "https://" )
12- . trim_start_matches ( "http://" )
13- . trim_end_matches ( '/' ) ;
14- format ! ( "https://semantic-layer.{host}" )
10+ let host = host. trim_end_matches ( '/' ) ;
11+
12+ let ( scheme, rest) = if let Some ( rest) = host. strip_prefix ( "https://" ) {
13+ ( "https://" , rest)
14+ } else if let Some ( rest) = host. strip_prefix ( "http://" ) {
15+ ( "http://" , rest)
16+ } else {
17+ ( "https://" , host)
18+ } ;
19+
20+ // Multi-tenant pattern: {slug}.{region}.dbt.com → {slug}.semantic-layer.{region}.dbt.com
21+ // Only applies when host has the form {slug}.{region}.dbt.com (two+ segments before .dbt.com)
22+ if let Some ( prefix) = rest. strip_suffix ( ".dbt.com" ) {
23+ if prefix. contains ( '.' ) {
24+ if let Some ( dot) = rest. find ( '.' ) {
25+ let slug = & rest[ ..dot] ;
26+ let remainder = & rest[ dot + 1 ..] ;
27+ return format ! ( "{scheme}{slug}.semantic-layer.{remainder}" ) ;
28+ }
29+ }
30+ }
31+
32+ // Legacy: cloud.getdbt.com → semantic-layer.cloud.getdbt.com
33+ format ! ( "{scheme}semantic-layer.{rest}" )
1534}
1635
1736#[ cfg( test) ]
1837mod tests {
1938 use super :: * ;
2039
40+ #[ test]
41+ fn test_semantic_layer_url_multi_tenant ( ) {
42+ assert_eq ! (
43+ semantic_layer_url( "https://tk626.us1.dbt.com" ) ,
44+ "https://tk626.semantic-layer.us1.dbt.com"
45+ ) ;
46+ }
47+
2148 #[ test]
2249 fn test_semantic_layer_url_plain_host ( ) {
2350 assert_eq ! (
@@ -37,8 +64,8 @@ mod tests {
3764 #[ test]
3865 fn test_semantic_layer_url_with_trailing_slash ( ) {
3966 assert_eq ! (
40- semantic_layer_url( "https://au .dbt.com/" ) ,
41- "https://semantic-layer.au .dbt.com"
67+ semantic_layer_url( "https://tk626.us1 .dbt.com/" ) ,
68+ "https://tk626. semantic-layer.us1 .dbt.com"
4269 ) ;
4370 }
4471}
0 commit comments