|
| 1 | +package com.glean.api_client.glean_api_client.hooks; |
| 2 | + |
| 3 | +import com.glean.api_client.glean_api_client.SDKConfiguration; |
| 4 | +import com.glean.api_client.glean_api_client.utils.AsyncHook; |
| 5 | +import com.glean.api_client.glean_api_client.utils.Helpers; |
| 6 | +import com.glean.api_client.glean_api_client.utils.Hook; |
| 7 | + |
| 8 | +import java.net.http.HttpRequest; |
| 9 | +import java.util.Optional; |
| 10 | +import java.util.concurrent.CompletableFuture; |
| 11 | +import java.util.function.Function; |
| 12 | + |
| 13 | +/** |
| 14 | + * Hook that adds X-Glean headers for experimental features and deprecation testing. |
| 15 | + * |
| 16 | + * <p>This hook sets the following headers based on SDK options or environment variables: |
| 17 | + * <ul> |
| 18 | + * <li>{@code X-Glean-Exclude-Deprecated-After} - Exclude API endpoints deprecated after this date</li> |
| 19 | + * <li>{@code X-Glean-Experimental} - Enable experimental API features</li> |
| 20 | + * </ul> |
| 21 | + * |
| 22 | + * <p>Environment variables take precedence over SDK constructor options: |
| 23 | + * <ul> |
| 24 | + * <li>{@code X_GLEAN_EXCLUDE_DEPRECATED_AFTER} - Date in YYYY-MM-DD format</li> |
| 25 | + * <li>{@code X_GLEAN_INCLUDE_EXPERIMENTAL} - "true" to enable experimental features</li> |
| 26 | + * </ul> |
| 27 | + */ |
| 28 | +public final class XGleanHeadersHook { |
| 29 | + |
| 30 | + static final String ENV_EXCLUDE_DEPRECATED_AFTER = "X_GLEAN_EXCLUDE_DEPRECATED_AFTER"; |
| 31 | + static final String ENV_INCLUDE_EXPERIMENTAL = "X_GLEAN_INCLUDE_EXPERIMENTAL"; |
| 32 | + |
| 33 | + static final String HEADER_EXCLUDE_DEPRECATED_AFTER = "X-Glean-Exclude-Deprecated-After"; |
| 34 | + static final String HEADER_EXPERIMENTAL = "X-Glean-Experimental"; |
| 35 | + |
| 36 | + private XGleanHeadersHook() { |
| 37 | + // prevent instantiation |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a synchronous BeforeRequest hook for adding X-Glean headers. |
| 42 | + * |
| 43 | + * @return the sync hook |
| 44 | + */ |
| 45 | + public static Hook.BeforeRequest createSyncHook() { |
| 46 | + return createSyncHook(System::getenv); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Creates a synchronous BeforeRequest hook for adding X-Glean headers. |
| 51 | + * This variant accepts a custom environment variable provider for testing. |
| 52 | + * |
| 53 | + * @param envProvider function to get environment variables |
| 54 | + * @return the sync hook |
| 55 | + */ |
| 56 | + static Hook.BeforeRequest createSyncHook(Function<String, String> envProvider) { |
| 57 | + return (context, request) -> { |
| 58 | + HttpRequest.Builder builder = Helpers.copy(request); |
| 59 | + addHeaders(builder, context.sdkConfiguration(), envProvider); |
| 60 | + return builder.build(); |
| 61 | + }; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Creates an asynchronous BeforeRequest hook for adding X-Glean headers. |
| 66 | + * |
| 67 | + * @return the async hook |
| 68 | + */ |
| 69 | + public static AsyncHook.BeforeRequest createAsyncHook() { |
| 70 | + return createAsyncHook(System::getenv); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates an asynchronous BeforeRequest hook for adding X-Glean headers. |
| 75 | + * This variant accepts a custom environment variable provider for testing. |
| 76 | + * |
| 77 | + * @param envProvider function to get environment variables |
| 78 | + * @return the async hook |
| 79 | + */ |
| 80 | + static AsyncHook.BeforeRequest createAsyncHook(Function<String, String> envProvider) { |
| 81 | + return (context, request) -> { |
| 82 | + HttpRequest.Builder builder = Helpers.copy(request); |
| 83 | + addHeaders(builder, context.sdkConfiguration(), envProvider); |
| 84 | + return CompletableFuture.completedFuture(builder.build()); |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + private static void addHeaders(HttpRequest.Builder builder, SDKConfiguration config, |
| 89 | + Function<String, String> envProvider) { |
| 90 | + // Get deprecated after value - environment variable takes precedence |
| 91 | + Optional<String> deprecatedAfterValue = getFirstNonEmpty( |
| 92 | + getEnv(ENV_EXCLUDE_DEPRECATED_AFTER, envProvider), |
| 93 | + config.excludeDeprecatedAfter() |
| 94 | + ); |
| 95 | + |
| 96 | + deprecatedAfterValue.ifPresent(value -> |
| 97 | + builder.header(HEADER_EXCLUDE_DEPRECATED_AFTER, value) |
| 98 | + ); |
| 99 | + |
| 100 | + // Get experimental value - environment variable takes precedence |
| 101 | + Optional<String> experimentalValue = getFirstNonEmpty( |
| 102 | + getEnvAsBoolean(ENV_INCLUDE_EXPERIMENTAL, envProvider), |
| 103 | + config.includeExperimental().filter(b -> b).map(b -> "true") |
| 104 | + ); |
| 105 | + |
| 106 | + experimentalValue.ifPresent(value -> |
| 107 | + builder.header(HEADER_EXPERIMENTAL, value) |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the first non-empty Optional from the provided arguments. |
| 113 | + */ |
| 114 | + @SafeVarargs |
| 115 | + private static Optional<String> getFirstNonEmpty(Optional<String>... optionals) { |
| 116 | + for (Optional<String> opt : optionals) { |
| 117 | + if (opt.isPresent()) { |
| 118 | + return opt; |
| 119 | + } |
| 120 | + } |
| 121 | + return Optional.empty(); |
| 122 | + } |
| 123 | + |
| 124 | + private static Optional<String> getEnv(String name, Function<String, String> envProvider) { |
| 125 | + String value = envProvider.apply(name); |
| 126 | + if (value != null && !value.isEmpty()) { |
| 127 | + return Optional.of(value); |
| 128 | + } |
| 129 | + return Optional.empty(); |
| 130 | + } |
| 131 | + |
| 132 | + private static Optional<String> getEnvAsBoolean(String name, Function<String, String> envProvider) { |
| 133 | + String value = envProvider.apply(name); |
| 134 | + if ("true".equalsIgnoreCase(value)) { |
| 135 | + return Optional.of("true"); |
| 136 | + } |
| 137 | + return Optional.empty(); |
| 138 | + } |
| 139 | +} |
0 commit comments