|
22 | 22 | from ....models.campaign.feature_model import FeatureModel |
23 | 23 | from ....models.user.context_model import ContextModel |
24 | 24 | from ....utils.gateway_service_util import get_query_params, get_from_gateway_service |
| 25 | +from ....constants.Constants import Constants |
| 26 | +from ....services.url_service import UrlService |
25 | 27 |
|
26 | 28 |
|
27 | 29 | class SegmentationManager: |
@@ -77,27 +79,32 @@ def set_contextual_data( |
77 | 79 | if not context.get_user_agent() and not context.get_ip_address(): |
78 | 80 | return |
79 | 81 |
|
80 | | - if ( |
81 | | - feature.get_is_gateway_service_required() |
82 | | - ): # Check if gateway service is required |
83 | | - if SettingsManager.get_instance().is_gateway_service_provided and ( |
84 | | - context.get_vwo() is None |
85 | | - ): |
86 | | - query_params = {} |
87 | | - if context.get_user_agent(): |
88 | | - query_params["userAgent"] = context.get_user_agent() |
89 | | - |
90 | | - if context.get_ip_address(): |
91 | | - query_params["ipAddress"] = context.get_ip_address() |
92 | | - |
93 | | - try: |
94 | | - params = get_query_params(query_params) |
95 | | - _vwo = get_from_gateway_service(params, UrlEnum.GET_USER_DATA.value) |
96 | | - context.set_vwo(ContextVWOModel(_vwo)) |
97 | | - except Exception as err: |
98 | | - LogManager.get_instance().error( |
99 | | - f"Error in setting contextual data for segmentation. Got error: {err}" |
100 | | - ) |
| 82 | + # Call gateway service if required for segmentation OR if gateway service is provided and user agent is available |
| 83 | + should_call_gateway_service = ( |
| 84 | + (feature.get_is_gateway_service_required() and Constants.HOST_NAME not in UrlService.get_base_url()) or |
| 85 | + (Constants.HOST_NAME not in UrlService.get_base_url() and |
| 86 | + (context.get_user_agent() or context.get_ip_address())) |
| 87 | + ) |
| 88 | + |
| 89 | + if should_call_gateway_service and context.get_vwo() is None: |
| 90 | + query_params = {} |
| 91 | + if not context.get_user_agent() and not context.get_ip_address(): |
| 92 | + return |
| 93 | + |
| 94 | + if context.get_user_agent(): |
| 95 | + query_params["userAgent"] = context.get_user_agent() |
| 96 | + |
| 97 | + if context.get_ip_address(): |
| 98 | + query_params["ipAddress"] = context.get_ip_address() |
| 99 | + |
| 100 | + try: |
| 101 | + params = get_query_params(query_params) |
| 102 | + _vwo = get_from_gateway_service(params, UrlEnum.GET_USER_DATA.value) |
| 103 | + context.set_vwo(ContextVWOModel(_vwo)) |
| 104 | + except Exception as err: |
| 105 | + LogManager.get_instance().error( |
| 106 | + f"Error in setting contextual data for segmentation. Got error: {err}" |
| 107 | + ) |
101 | 108 |
|
102 | 109 | def validate_segmentation(self, dsl, properties): |
103 | 110 | """ |
|
0 commit comments