@@ -43,6 +43,8 @@ public class FlagsmithClient {
4343 private FlagsmithSdk flagsmithSdk ;
4444 private EnvironmentModel environment ;
4545 private PollingManager pollingManager ;
46+ private static final String UNABLE_TO_UPDATE_ENVIRONMENT_MESSAGE =
47+ "Unable to update environment from API. Continuing to use previous copy." ;
4648
4749 private FlagsmithClient () { }
4850
@@ -54,7 +56,29 @@ public static FlagsmithClient.Builder newBuilder() {
5456 * Load the environment flags in the environment variable from the API.
5557 */
5658 public void updateEnvironment () {
57- this .environment = flagsmithSdk .getEnvironment ();
59+ try {
60+ EnvironmentModel updatedEnvironment = flagsmithSdk .getEnvironment ();
61+
62+ // if we didn't get an environment from the API,
63+ // then don't overwrite the copy we already have.
64+ if (updatedEnvironment != null ) {
65+ this .environment = updatedEnvironment ;
66+ } else {
67+ logger .error (UNABLE_TO_UPDATE_ENVIRONMENT_MESSAGE );
68+ }
69+ } catch (RuntimeException e ) {
70+ // if we already have a copy of the environment, don't throw an error,
71+ // just continue using that one and log an error as it's likely just a
72+ // temporary network issue.
73+ if (this .environment != null ) {
74+ logger .error (UNABLE_TO_UPDATE_ENVIRONMENT_MESSAGE );
75+ } else {
76+ // if we don't already have an environment, then we should still throw
77+ // the error since it's likely on client start up and there might be
78+ // something more sinister going on.
79+ throw e ;
80+ }
81+ }
5882 }
5983
6084 /**
@@ -248,6 +272,7 @@ public static class Builder {
248272 private String apiKey ;
249273 private FlagsmithCacheConfig cacheConfig ;
250274 private PollingManager pollingManager ;
275+ private FlagsmithApiWrapper flagsmithApiWrapper ;
251276
252277 private Builder () {
253278 client = new FlagsmithClient ();
@@ -374,6 +399,17 @@ public Builder withPollingManager(PollingManager manager) {
374399 return this ;
375400 }
376401
402+ /**
403+ * Set the api wrapper.
404+ *
405+ * @param flagsmithApiWrapper FlagsmithAPIWrapper object
406+ * @return the Builder
407+ */
408+ public Builder withFlagsmithApiWrapper (FlagsmithApiWrapper flagsmithApiWrapper ) {
409+ this .flagsmithApiWrapper = flagsmithApiWrapper ;
410+ return this ;
411+ }
412+
377413 /**
378414 * Builds a FlagsmithClient.
379415 *
@@ -382,20 +418,22 @@ public Builder withPollingManager(PollingManager manager) {
382418 public FlagsmithClient build () {
383419 final FlagsmithApiWrapper flagsmithApiWrapper ;
384420
385- if (cacheConfig != null ) {
421+ if (this .flagsmithApiWrapper != null ) {
422+ flagsmithApiWrapper = this .flagsmithApiWrapper ;
423+ } else if (cacheConfig != null ) {
386424 flagsmithApiWrapper = new FlagsmithApiWrapper (
387- cacheConfig .getCache (),
388- this .configuration ,
389- this .customHeaders ,
390- client .logger ,
391- apiKey
425+ cacheConfig .getCache (),
426+ this .configuration ,
427+ this .customHeaders ,
428+ client .logger ,
429+ apiKey
392430 );
393431 } else {
394432 flagsmithApiWrapper = new FlagsmithApiWrapper (
395- this .configuration ,
396- this .customHeaders ,
397- client .logger ,
398- apiKey
433+ this .configuration ,
434+ this .customHeaders ,
435+ client .logger ,
436+ apiKey
399437 );
400438 }
401439
0 commit comments