-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathx_glean.py
More file actions
80 lines (64 loc) · 2.6 KB
/
x_glean.py
File metadata and controls
80 lines (64 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""Hook to set X-Glean headers for experimental features and deprecation testing."""
import os
from typing import Optional, Union
import httpx
from glean.api_client._hooks.types import BeforeRequestContext, BeforeRequestHook
def _get_first_value(
env_value: Optional[str],
config_value: Optional[str],
) -> Optional[str]:
"""Get the first non-empty value from the provided arguments.
Environment variables take precedence over SDK constructor options.
"""
if env_value:
return env_value
if config_value:
return config_value
return None
class XGlean(BeforeRequestHook):
"""
Hook that sets X-Glean headers for experimental features and deprecation testing.
This hook adds the following headers when configured:
- X-Glean-Exclude-Deprecated-After: Excludes API endpoints deprecated after this date
- X-Glean-Experimental: Enables experimental API features
Configuration can be done via environment variables or SDK constructor options.
Environment variables take precedence over SDK constructor options.
"""
def before_request(
self, hook_ctx: BeforeRequestContext, request: httpx.Request
) -> Union[httpx.Request, Exception]:
"""
Add X-Glean headers to the request based on configuration.
Args:
hook_ctx: Context containing SDK configuration
request: The HTTP request being made
Returns:
The modified request with X-Glean headers added
"""
# Get deprecated value - env var takes precedence
deprecated_value = _get_first_value(
os.environ.get("X_GLEAN_EXCLUDE_DEPRECATED_AFTER"),
hook_ctx.config.exclude_deprecated_after,
)
# Get experimental value - env var takes precedence
config_experimental = (
"true" if hook_ctx.config.include_experimental is True else None
)
experimental_value = _get_first_value(
os.environ.get("X_GLEAN_INCLUDE_EXPERIMENTAL"),
config_experimental,
)
# Create new headers dict with existing headers
new_headers = dict(request.headers)
if deprecated_value:
new_headers["X-Glean-Exclude-Deprecated-After"] = deprecated_value
if experimental_value:
new_headers["X-Glean-Experimental"] = experimental_value
# Return new request with updated headers
return httpx.Request(
method=request.method,
url=request.url,
headers=new_headers,
content=request.content,
extensions=request.extensions,
)