File tree Expand file tree Collapse file tree 3 files changed +69
-1
lines changed
Expand file tree Collapse file tree 3 files changed +69
-1
lines changed Original file line number Diff line number Diff line change 3535 SDK_NAME ,
3636 SDK_VERSION ,
3737)
38+ from UnleashClient .environment_resolver import extract_environment_from_headers
3839from UnleashClient .events import (
3940 BaseEvent ,
4041 UnleashEvent ,
@@ -206,8 +207,14 @@ def __init__(
206207 self .fl_job : Job = None
207208 self .metric_job : Job = None
208209 self .engine = UnleashEngine ()
210+
211+ impact_metrics_environment = self .unleash_environment
212+ extracted_env = extract_environment_from_headers (self .unleash_custom_headers )
213+ if extracted_env :
214+ impact_metrics_environment = extracted_env
215+
209216 self ._impact_metrics = ImpactMetrics (
210- self .engine , self .unleash_app_name , self . unleash_environment
217+ self .engine , self .unleash_app_name , impact_metrics_environment
211218 )
212219
213220 self .cache = cache or FileCache (
Original file line number Diff line number Diff line change 1+ from typing import Dict , Optional
2+
3+
4+ def extract_environment_from_headers (
5+ headers : Optional [Dict [str , str ]],
6+ ) -> Optional [str ]:
7+ if not headers :
8+ return None
9+
10+ auth_key = next (
11+ (key for key in headers if key .lower () == "authorization" ),
12+ None ,
13+ )
14+ if not auth_key :
15+ return None
16+
17+ auth_value = headers .get (auth_key )
18+ if not auth_value :
19+ return None
20+
21+ _ , sep , after_colon = auth_value .partition (":" )
22+ if not sep :
23+ return None
24+
25+ environment , _ , _ = after_colon .partition ("." )
26+ return environment or None
Original file line number Diff line number Diff line change 1+ from UnleashClient .environment_resolver import extract_environment_from_headers
2+
3+
4+ def test_valid_headers ():
5+ custom_headers = {
6+ "Authorization" : "project:environment.hash" ,
7+ "Content-Type" : "application/json" ,
8+ }
9+
10+ result = extract_environment_from_headers (custom_headers )
11+ assert result == "environment"
12+
13+
14+ def test_case_insensitive_header_keys ():
15+ custom_headers = {
16+ "AUTHORIZATION" : "project:environment.hash" ,
17+ "Content-Type" : "application/json" ,
18+ }
19+
20+ result = extract_environment_from_headers (custom_headers )
21+ assert result == "environment"
22+
23+
24+ def test_authorization_header_not_present ():
25+ result = extract_environment_from_headers ({})
26+ assert result is None
27+
28+
29+ def test_environment_part_is_empty ():
30+ custom_headers = {
31+ "Authorization" : "project:.hash" ,
32+ }
33+
34+ result = extract_environment_from_headers (custom_headers )
35+ assert result is None
You can’t perform that action at this time.
0 commit comments