|
| 1 | +package dev.openfeature.contrib.providers.flagsmith; |
| 2 | + |
| 3 | +import com.flagsmith.FlagsmithClient; |
| 4 | +import com.flagsmith.config.FlagsmithCacheConfig; |
| 5 | +import com.flagsmith.config.Retry; |
| 6 | +import dev.openfeature.contrib.providers.flagsmith.exceptions.InvalidCacheOptionsException; |
| 7 | +import dev.openfeature.contrib.providers.flagsmith.exceptions.InvalidOptionsException; |
| 8 | + |
| 9 | +/** |
| 10 | + * FlagsmithClientConfigurer helps set up and validate the options for the FlagsmithClient |
| 11 | + * used by the FlagsmithProvider class. |
| 12 | + */ |
| 13 | +public class FlagsmithClientConfigurer { |
| 14 | + |
| 15 | + /** |
| 16 | + * initializeProvider is initializing the different class element used by the provider. |
| 17 | + * |
| 18 | + * @param options the options used to create the provider |
| 19 | + */ |
| 20 | + static FlagsmithClient initializeProvider(FlagsmithProviderOptions options) { |
| 21 | + |
| 22 | + validateOptions(options); |
| 23 | + |
| 24 | + FlagsmithClient.Builder flagsmithBuilder = FlagsmithClient |
| 25 | + .newBuilder(); |
| 26 | + // Set main configuration settings |
| 27 | + flagsmithBuilder.setApiKey(options.getApiKey()); |
| 28 | + |
| 29 | + if (options.getHeaders() != null && !options.getHeaders().isEmpty()) { |
| 30 | + flagsmithBuilder.withCustomHttpHeaders(options.getHeaders()); |
| 31 | + } |
| 32 | + |
| 33 | + if (options.getEnvFlagsCacheKey() != null) { |
| 34 | + FlagsmithCacheConfig flagsmithCacheConfig = initializeCacheConfig(options); |
| 35 | + flagsmithBuilder.withCache(flagsmithCacheConfig); |
| 36 | + } |
| 37 | + |
| 38 | + com.flagsmith.config.FlagsmithConfig flagsmithConfig = initializeConfig(options); |
| 39 | + flagsmithBuilder.withConfiguration(flagsmithConfig); |
| 40 | + |
| 41 | + return flagsmithBuilder.build(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Sets the cache related configuration for the provider using |
| 46 | + * the FlagsmithCacheConfig builder. |
| 47 | + * |
| 48 | + * @param options the options used to create the provider |
| 49 | + * @return a FlagsmithCacheConfig object containing the FlagsmithClient cache options |
| 50 | + */ |
| 51 | + private static FlagsmithCacheConfig initializeCacheConfig(FlagsmithProviderOptions options) { |
| 52 | + FlagsmithCacheConfig.Builder flagsmithCacheConfig = FlagsmithCacheConfig.newBuilder(); |
| 53 | + |
| 54 | + // Set cache configuration settings |
| 55 | + if (options.getEnvFlagsCacheKey() != null) { |
| 56 | + flagsmithCacheConfig.enableEnvLevelCaching(options.getEnvFlagsCacheKey()); |
| 57 | + } |
| 58 | + |
| 59 | + if (options.getExpireCacheAfterWrite() > -1 |
| 60 | + && options.getExpireCacheAfterWriteTimeUnit() != null) { |
| 61 | + flagsmithCacheConfig.expireAfterAccess( |
| 62 | + options.getExpireCacheAfterWrite(), |
| 63 | + options.getExpireCacheAfterWriteTimeUnit()); |
| 64 | + } |
| 65 | + |
| 66 | + if (options.getExpireCacheAfterAccess() > -1 |
| 67 | + && options.getExpireCacheAfterAccessTimeUnit() != null) { |
| 68 | + flagsmithCacheConfig.expireAfterAccess( |
| 69 | + options.getExpireCacheAfterAccess(), |
| 70 | + options.getExpireCacheAfterAccessTimeUnit()); |
| 71 | + } |
| 72 | + |
| 73 | + if (options.getMaxCacheSize() > -1) { |
| 74 | + flagsmithCacheConfig.maxSize(options.getMaxCacheSize()); |
| 75 | + } |
| 76 | + |
| 77 | + if (options.isRecordCacheStats()) { |
| 78 | + flagsmithCacheConfig.recordStats(); |
| 79 | + } |
| 80 | + |
| 81 | + return flagsmithCacheConfig.build(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Set the configuration options for the FlagsmithClient using |
| 86 | + * the FlagsmithConfig builder. |
| 87 | + * |
| 88 | + * @param options The options used to create the provider |
| 89 | + * @return a FlagsmithConfig object with the FlagsmithClient settings |
| 90 | + */ |
| 91 | + private static com.flagsmith.config.FlagsmithConfig initializeConfig( |
| 92 | + FlagsmithProviderOptions options) { |
| 93 | + com.flagsmith.config.FlagsmithConfig.Builder flagsmithConfig = com.flagsmith.config.FlagsmithConfig |
| 94 | + .newBuilder(); |
| 95 | + |
| 96 | + // Set client level configuration settings |
| 97 | + if (options.getBaseUri() != null) { |
| 98 | + flagsmithConfig.baseUri(options.getBaseUri()); |
| 99 | + } |
| 100 | + |
| 101 | + if (options.getConnectTimeout() > -1) { |
| 102 | + flagsmithConfig.connectTimeout(options.getConnectTimeout()); |
| 103 | + } |
| 104 | + |
| 105 | + if (options.getWriteTimeout() > -1) { |
| 106 | + flagsmithConfig.writeTimeout(options.getWriteTimeout()); |
| 107 | + } |
| 108 | + |
| 109 | + if (options.getReadTimeout() > -1) { |
| 110 | + flagsmithConfig.readTimeout(options.getReadTimeout()); |
| 111 | + } |
| 112 | + |
| 113 | + if (options.getSslSocketFactory() != null && options.getTrustManager() != null) { |
| 114 | + flagsmithConfig |
| 115 | + .sslSocketFactory(options.getSslSocketFactory(), options.getTrustManager()); |
| 116 | + } |
| 117 | + |
| 118 | + if (options.getHttpInterceptor() != null) { |
| 119 | + flagsmithConfig.addHttpInterceptor(options.getHttpInterceptor()); |
| 120 | + } |
| 121 | + |
| 122 | + if (options.getRetries() > -1) { |
| 123 | + flagsmithConfig.retries(new Retry(options.getRetries())); |
| 124 | + } |
| 125 | + |
| 126 | + if (options.isLocalEvaluation()) { |
| 127 | + flagsmithConfig.withLocalEvaluation(options.isLocalEvaluation()); |
| 128 | + } |
| 129 | + |
| 130 | + if (options.getEnvironmentRefreshIntervalSeconds() > -1) { |
| 131 | + flagsmithConfig.withEnvironmentRefreshIntervalSeconds(options |
| 132 | + .getEnvironmentRefreshIntervalSeconds()); |
| 133 | + } |
| 134 | + |
| 135 | + if (options.isEnableAnalytics()) { |
| 136 | + flagsmithConfig.withEnableAnalytics(options.isEnableAnalytics()); |
| 137 | + } |
| 138 | + |
| 139 | + return flagsmithConfig.build(); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Check the options that have been provided to see if there are any issues. |
| 144 | + * Exceptions will be thrown if there are issues found with the options. |
| 145 | + * |
| 146 | + * @param options the options used to create the provider |
| 147 | + */ |
| 148 | + private static void validateOptions(FlagsmithProviderOptions options) { |
| 149 | + if (options == null) { |
| 150 | + throw new InvalidOptionsException("No options provided"); |
| 151 | + } |
| 152 | + |
| 153 | + if (options.getApiKey() == null || options.getApiKey().isEmpty()) { |
| 154 | + throw new InvalidOptionsException("Flagsmith API key has not been set."); |
| 155 | + } |
| 156 | + |
| 157 | + if (options.getEnvFlagsCacheKey() == null |
| 158 | + && (options.getExpireCacheAfterWrite() > -1 |
| 159 | + || options.getExpireCacheAfterAccess() > -1 |
| 160 | + || options.getMaxCacheSize() > -1 |
| 161 | + || options.isRecordCacheStats())) { |
| 162 | + throw new InvalidCacheOptionsException( |
| 163 | + "No Flagsmith cache key provided but other cache settings have been set." |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments