Skip to content

Commit 150b121

Browse files
Add parameters to Remote Config template (#479)
- Introduce Response Types and Public Types - Implement toResponseType() and toPublicType() - Add parameters to RemoteConfigTemplate - Add unit tests
1 parent 936043b commit 150b121

File tree

8 files changed

+505
-5
lines changed

8 files changed

+505
-5
lines changed

src/main/java/com/google/firebase/remoteconfig/FirebaseRemoteConfigClientImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.firebase.internal.HttpRequestInfo;
3737
import com.google.firebase.internal.SdkUtils;
3838
import com.google.firebase.remoteconfig.internal.RemoteConfigServiceErrorResponse;
39+
import com.google.firebase.remoteconfig.internal.TemplateResponse;
3940

4041
import java.io.IOException;
4142
import java.util.List;
@@ -94,8 +95,9 @@ public RemoteConfigTemplate getTemplate() throws FirebaseRemoteConfigException {
9495
HttpRequestInfo request = HttpRequestInfo.buildGetRequest(remoteConfigUrl)
9596
.addAllHeaders(COMMON_HEADERS);
9697
IncomingHttpResponse response = httpClient.send(request);
97-
RemoteConfigTemplate parsed = httpClient.parse(response, RemoteConfigTemplate.class);
98-
return parsed.setETag(getETag(response));
98+
TemplateResponse templateResponse = httpClient.parse(response, TemplateResponse.class);
99+
RemoteConfigTemplate template = templateResponse.toRemoteConfigTemplate();
100+
return template.setETag(getETag(response));
99101
}
100102

101103
private String getETag(IncomingHttpResponse response) {
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.remoteconfig;
18+
19+
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.common.base.Preconditions.checkNotNull;
21+
22+
import com.google.firebase.internal.NonNull;
23+
import com.google.firebase.internal.Nullable;
24+
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterResponse;
25+
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse;
26+
27+
import java.util.HashMap;
28+
import java.util.Map;
29+
30+
/**
31+
* Represents a Remote Config parameter that can be included in a {@link RemoteConfigTemplate}.
32+
* At minimum, a default value or a conditional value must be present for the
33+
* parameter to have any effect.
34+
*/
35+
public final class RemoteConfigParameter {
36+
37+
private RemoteConfigParameterValue defaultValue;
38+
private String description;
39+
private Map<String, RemoteConfigParameterValue> conditionalValues;
40+
41+
/**
42+
* Creates a new {@link RemoteConfigParameter}.
43+
*/
44+
public RemoteConfigParameter() {
45+
conditionalValues = new HashMap<>();
46+
}
47+
48+
/**
49+
* Gets the default value of the parameter.
50+
*
51+
* @return A {@link RemoteConfigParameterValue} instance or null.
52+
*/
53+
@Nullable
54+
public RemoteConfigParameterValue getDefaultValue() {
55+
return defaultValue;
56+
}
57+
58+
/**
59+
* Gets the description of the parameter.
60+
*
61+
* @return The {@link String} description of the parameter or null.
62+
*/
63+
@Nullable
64+
public String getDescription() {
65+
return description;
66+
}
67+
68+
/**
69+
* Gets the conditional values of the parameter.
70+
* The condition name of the highest priority (the one listed first in the
71+
* {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter.
72+
*
73+
* @return A non-null map of conditional values.
74+
*/
75+
@NonNull
76+
public Map<String, RemoteConfigParameterValue> getConditionalValues() {
77+
return conditionalValues;
78+
}
79+
80+
/**
81+
* Sets the default value of the parameter.
82+
* This is the value to set the parameter to, when none of the named conditions
83+
* evaluate to true.
84+
*
85+
* @param value An {@link RemoteConfigParameterValue} instance.
86+
* @return This {@link RemoteConfigParameter}.
87+
*/
88+
public RemoteConfigParameter setDefaultValue(@Nullable RemoteConfigParameterValue value) {
89+
defaultValue = value;
90+
return this;
91+
}
92+
93+
/**
94+
* Sets the description of the parameter.
95+
* Should not be over 100 characters and may contain any Unicode characters.
96+
*
97+
* @param description The description of the parameter.
98+
* @return This {@link RemoteConfigParameter}.
99+
*/
100+
public RemoteConfigParameter setDescription(@Nullable String description) {
101+
this.description = description;
102+
return this;
103+
}
104+
105+
/**
106+
* Sets the conditional values of the parameter.
107+
* The condition name of the highest priority (the one listed first in the
108+
* {@link RemoteConfigTemplate}'s conditions list) determines the value of this parameter.
109+
*
110+
* @param conditionalValues A non-null map of conditional values.
111+
* @return This {@link RemoteConfigParameter}.
112+
*/
113+
public RemoteConfigParameter setConditionalValues(
114+
@NonNull Map<String, RemoteConfigParameterValue> conditionalValues) {
115+
checkNotNull(conditionalValues, "conditional values must not be null.");
116+
this.conditionalValues = conditionalValues;
117+
return this;
118+
}
119+
120+
ParameterResponse toParameterResponse() {
121+
Map<String, ParameterValueResponse> conditionalResponseValues = new HashMap<>();
122+
for (Map.Entry<String, RemoteConfigParameterValue> entry : conditionalValues.entrySet()) {
123+
conditionalResponseValues.put(entry.getKey(), entry.getValue().toParameterValueResponse());
124+
}
125+
ParameterValueResponse parameterValueResponse = (defaultValue == null) ? null : defaultValue
126+
.toParameterValueResponse();
127+
return new ParameterResponse(parameterValueResponse, description,
128+
conditionalResponseValues);
129+
}
130+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2020 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.remoteconfig;
18+
19+
import com.google.firebase.remoteconfig.internal.TemplateResponse.ParameterValueResponse;
20+
21+
/**
22+
* Represents a Remote Config parameter value that can be used in a {@link RemoteConfigTemplate}.
23+
*/
24+
public abstract class RemoteConfigParameterValue {
25+
26+
/**
27+
* Creates a new {@link RemoteConfigParameterValue.Explicit} instance with the given value.
28+
*
29+
* @param value The value of the {@link RemoteConfigParameterValue.Explicit}.
30+
* @return A {@link RemoteConfigParameterValue.Explicit} instance.
31+
*/
32+
public static Explicit of(String value) {
33+
return new Explicit(value);
34+
}
35+
36+
/**
37+
* Creates a new {@link RemoteConfigParameterValue.InAppDefault} instance.
38+
*
39+
* @return A {@link RemoteConfigParameterValue.InAppDefault} instance.
40+
*/
41+
public static InAppDefault inAppDefault() {
42+
return new InAppDefault();
43+
}
44+
45+
abstract ParameterValueResponse toParameterValueResponse();
46+
47+
/**
48+
* Represents an explicit Remote Config parameter value with a {@link String} value that the
49+
* parameter is set to.
50+
*/
51+
public static final class Explicit extends RemoteConfigParameterValue {
52+
53+
private final String value;
54+
55+
private Explicit(String value) {
56+
this.value = value;
57+
}
58+
59+
/**
60+
* Gets the value of {@link RemoteConfigParameterValue.Explicit}.
61+
*
62+
* @return The {@link String} value.
63+
*/
64+
public String getValue() {
65+
return this.value;
66+
}
67+
68+
@Override
69+
ParameterValueResponse toParameterValueResponse() {
70+
return ParameterValueResponse.ofValue(this.value);
71+
}
72+
}
73+
74+
/**
75+
* Represents an in app default parameter value.
76+
*/
77+
public static final class InAppDefault extends RemoteConfigParameterValue {
78+
79+
@Override
80+
ParameterValueResponse toParameterValueResponse() {
81+
return ParameterValueResponse.ofInAppDefaultValue();
82+
}
83+
}
84+
}

src/main/java/com/google/firebase/remoteconfig/RemoteConfigTemplate.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,73 @@
1616

1717
package com.google.firebase.remoteconfig;
1818

19-
import com.google.api.client.util.Key;
19+
import static com.google.common.base.Preconditions.checkNotNull;
2020

21+
import com.google.firebase.internal.NonNull;
22+
import com.google.firebase.remoteconfig.internal.TemplateResponse;
23+
24+
import java.util.HashMap;
25+
import java.util.Map;
26+
27+
/**
28+
* Represents a Remote Config template.
29+
*/
2130
public final class RemoteConfigTemplate {
2231

23-
@Key("etag")
2432
private String etag;
33+
private Map<String, RemoteConfigParameter> parameters;
2534

35+
/**
36+
* Creates a new {@link RemoteConfigTemplate}.
37+
*/
38+
public RemoteConfigTemplate() {
39+
parameters = new HashMap<>();
40+
}
41+
42+
/**
43+
* Gets the ETag of the template.
44+
*
45+
* @return The ETag of the template.
46+
*/
2647
public String getETag() {
2748
return this.etag;
2849
}
2950

51+
/**
52+
* Gets the map of parameters of the template.
53+
*
54+
* @return A non-null map of parameter keys to their optional default values and optional
55+
* conditional values.
56+
*/
57+
@NonNull
58+
public Map<String, RemoteConfigParameter> getParameters() {
59+
return this.parameters;
60+
}
61+
62+
/**
63+
* Sets the map of parameters of the template.
64+
*
65+
* @param parameters A non-null map of parameter keys to their optional default values and
66+
* optional conditional values.
67+
* @return This {@link RemoteConfigTemplate} instance.
68+
*/
69+
public RemoteConfigTemplate setParameters(
70+
@NonNull Map<String, RemoteConfigParameter> parameters) {
71+
checkNotNull(parameters, "parameters must not be null.");
72+
this.parameters = parameters;
73+
return this;
74+
}
75+
3076
RemoteConfigTemplate setETag(String etag) {
3177
this.etag = etag;
3278
return this;
3379
}
80+
81+
TemplateResponse toTemplateResponse() {
82+
Map<String, TemplateResponse.ParameterResponse> parameterResponses = new HashMap<>();
83+
for (Map.Entry<String, RemoteConfigParameter> entry : parameters.entrySet()) {
84+
parameterResponses.put(entry.getKey(), entry.getValue().toParameterResponse());
85+
}
86+
return new TemplateResponse(parameterResponses);
87+
}
3488
}

0 commit comments

Comments
 (0)