Skip to content

Commit 23cf6a8

Browse files
authored
Added proxy facts timeout policy (#33)
1 parent 59a5900 commit 23cf6a8

3 files changed

Lines changed: 109 additions & 6 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package io.permit.sdk;
2+
3+
/**
4+
* The {@code FactsSyncTimeoutPolicy} enum represents the policy options for
5+
* handling facts synchronization timeouts when proxy_facts_via_pdp is enabled.
6+
*/
7+
public enum FactsSyncTimeoutPolicy {
8+
/**
9+
* Continue the request without waiting for facts sync to complete
10+
*/
11+
IGNORE("ignore"),
12+
13+
/**
14+
* Fail the request if facts sync does not complete within the timeout
15+
*/
16+
FAIL("fail");
17+
18+
private final String value;
19+
20+
FactsSyncTimeoutPolicy(String value) {
21+
this.value = value;
22+
}
23+
24+
/**
25+
* Returns the string value of the enum.
26+
*
27+
* @return The string value.
28+
*/
29+
public String getValue() {
30+
return value;
31+
}
32+
33+
/**
34+
* Returns the enum constant of this type with the specified string value.
35+
*
36+
* @param value The string value to convert.
37+
* @return The enum constant with the specified string value.
38+
* @throws IllegalArgumentException If the given value is not a valid policy value.
39+
*/
40+
public static FactsSyncTimeoutPolicy fromString(String value) {
41+
for (FactsSyncTimeoutPolicy policy : FactsSyncTimeoutPolicy.values()) {
42+
if (policy.value.equalsIgnoreCase(value)) {
43+
return policy;
44+
}
45+
}
46+
throw new IllegalArgumentException("Unknown policy value: " + value);
47+
}
48+
}

src/main/java/io/permit/sdk/PermitConfig.java

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class PermitConfig {
1313
private final String apiUrl;
1414
private final Boolean debugMode;
1515
private final Boolean proxyFactsViaPdp;
16-
private final int factsSyncTimeout;
16+
private final Integer factsSyncTimeout;
17+
private final FactsSyncTimeoutPolicy factsSyncTimeoutPolicy;
1718

1819
// logger config
1920
private final String logLevel;
@@ -49,6 +50,7 @@ private PermitConfig(Builder builder) {
4950
this.context = builder.context;
5051
this.proxyFactsViaPdp = builder.proxyFactsViaPdp;
5152
this.factsSyncTimeout = builder.factsSyncTimeout;
53+
this.factsSyncTimeoutPolicy = builder.factsSyncTimeoutPolicy;
5254
String runtimeVersion = Permit.class.getPackage().getImplementationVersion();
5355
this.version = (runtimeVersion == null) ? defaultVersion : runtimeVersion;
5456
}
@@ -113,10 +115,23 @@ public Boolean isProxyFactsViaPdp() {
113115
*
114116
* @return The facts synchronization timeout.
115117
*/
116-
public int getFactsSyncTimeout() {
118+
public Integer getFactsSyncTimeout() {
117119
return factsSyncTimeout;
118120
}
119121

122+
/**
123+
* Returns the timeout policy for facts synchronization.
124+
* When proxy_facts_via_pdp is enabled, this determines what happens when the facts synchronization timeout is reached.
125+
* Values can be:
126+
* * IGNORE - Respond immediately when data update did not apply within the timeout period
127+
* * FAIL - Respond with 424 status code when data update did not apply within the timeout period
128+
*
129+
* @return The facts synchronization timeout policy, or null if not set.
130+
*/
131+
public FactsSyncTimeoutPolicy getFactsSyncTimeoutPolicy() {
132+
return factsSyncTimeoutPolicy;
133+
}
134+
120135
/**
121136
* Returns the log level configured for the Permit SDK Logger.
122137
*
@@ -231,7 +246,8 @@ public static class Builder {
231246
private String apiUrl = "https://api.permit.io";
232247
private Boolean debugMode = false;
233248
private Boolean proxyFactsViaPdp = false;
234-
private int factsSyncTimeout = 0;
249+
private Integer factsSyncTimeout = null;
250+
private FactsSyncTimeoutPolicy factsSyncTimeoutPolicy = null;
235251

236252
// logger config
237253
private String logLevel = "info";
@@ -320,11 +336,45 @@ public Builder withProxyFactsViaPdp(Boolean proxyFactsViaPdp) {
320336
* @param factsSyncTimeout The facts synchronization timeout to be set.
321337
* @return The updated {@code Builder} object.
322338
*/
323-
public Builder withFactsSyncTimeout(int factsSyncTimeout) {
339+
public Builder withFactsSyncTimeout(Integer factsSyncTimeout) {
324340
this.factsSyncTimeout = factsSyncTimeout;
325341
return this;
326342
}
327343

344+
/**
345+
* Sets the timeout policy for facts synchronization.
346+
* This determines what happens when the facts synchronization timeout is reached.
347+
*
348+
* @param factsSyncTimeoutPolicy The facts synchronization timeout policy to be set.
349+
* Must be "ignore" or "fail".
350+
* @return This Builder instance.
351+
* @throws IllegalArgumentException If the policy value is not "ignore" or "fail".
352+
*/
353+
public Builder withFactsSyncTimeoutPolicy(String factsSyncTimeoutPolicy) {
354+
if (factsSyncTimeoutPolicy == null) {
355+
this.factsSyncTimeoutPolicy = null;
356+
} else {
357+
try {
358+
this.factsSyncTimeoutPolicy = FactsSyncTimeoutPolicy.fromString(factsSyncTimeoutPolicy);
359+
} catch (IllegalArgumentException e) {
360+
throw new IllegalArgumentException("factsSyncTimeoutPolicy must be either 'ignore' or 'fail'", e);
361+
}
362+
}
363+
return this;
364+
}
365+
366+
/**
367+
* Sets the timeout policy for facts synchronization.
368+
* This determines what happens when the facts synchronization timeout is reached.
369+
*
370+
* @param policy The facts synchronization timeout policy to be set.
371+
* @return This Builder instance.
372+
*/
373+
public Builder withFactsSyncTimeoutPolicy(FactsSyncTimeoutPolicy policy) {
374+
this.factsSyncTimeoutPolicy = policy;
375+
return this;
376+
}
377+
328378
/**
329379
* Sets the log level configured for the Permit SDK Logger.
330380
*

src/main/java/io/permit/sdk/api/BaseApi.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ protected BaseApi(OkHttpClient client, PermitConfig config, Logger logger) {
3131
.add("Authorization", String.format("Bearer %s", this.config.getToken()))
3232
.add("X-Permit-SDK-Version", String.format("java:%s", this.config.version));
3333

34-
if (config.isProxyFactsViaPdp() && config.getFactsSyncTimeout() > 0) {
35-
headersBuilder.add("X-Wait-Timeout", String.valueOf(config.getFactsSyncTimeout()));
34+
if (config.isProxyFactsViaPdp()) {
35+
if (config.getFactsSyncTimeout() != null) {
36+
headersBuilder.add("X-Wait-Timeout", String.valueOf(config.getFactsSyncTimeout()));
37+
}
38+
if (config.getFactsSyncTimeoutPolicy() != null) {
39+
headersBuilder.add("X-Timeout-Policy", config.getFactsSyncTimeoutPolicy().getValue());
40+
}
3641
}
3742
this.headers = headersBuilder.build();
3843
}

0 commit comments

Comments
 (0)