Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support cached /health queries #228

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,45 @@ public final class HealthServicesRequest implements ConsulRequest {
private final boolean passing;
private final QueryParams queryParams;
private final String token;
private final boolean cached;

private HealthServicesRequest(
String datacenter,
String near,
String[] tags,
Map<String, String> nodeMeta,
boolean passing,
QueryParams queryParams,
String token
) {
this.datacenter = datacenter;
this.near = near;
this.tags = tags;
this.nodeMeta = nodeMeta;
this.passing = passing;
this.queryParams = queryParams;
this.token = token;
cached = false;
}

private HealthServicesRequest(String datacenter, String near, String[] tags, Map<String, String> nodeMeta, boolean passing, QueryParams queryParams, String token) {
private HealthServicesRequest(
String datacenter,
String near,
String[] tags,
Map<String, String> nodeMeta,
boolean passing,
QueryParams queryParams,
String token,
boolean cached
) {
this.datacenter = datacenter;
this.near = near;
this.tags = tags;
this.nodeMeta = nodeMeta;
this.passing = passing;
this.queryParams = queryParams;
this.token = token;
this.cached = cached;
}

public String getDatacenter() {
Expand Down Expand Up @@ -66,6 +96,10 @@ public String getToken() {
return token;
}

public boolean isCached() {
return cached;
}

public static class Builder {
private String datacenter;
private String near;
Expand All @@ -74,6 +108,7 @@ public static class Builder {
private boolean passing;
private QueryParams queryParams;
private String token;
private boolean cached;

private Builder() {
}
Expand Down Expand Up @@ -118,8 +153,16 @@ public Builder setToken(String token) {
return this;
}

/**
* Use cached queries, see https://www.consul.io/api-docs/features/caching
*/
public Builder setCached(boolean cached) {
this.cached = cached;
return this;
}

public HealthServicesRequest build() {
return new HealthServicesRequest(datacenter, near, tags, nodeMeta, passing, queryParams, token);
return new HealthServicesRequest(datacenter, near, tags, nodeMeta, passing, queryParams, token, cached);
}
}

Expand Down Expand Up @@ -157,6 +200,11 @@ public List<UrlParameters> asUrlParameters() {
params.add(new SingleUrlParameters("token", token));
}

if (cached) {
// any value is true
params.add(new SingleUrlParameters("cached", "1"));
}

return params;
}

Expand All @@ -175,12 +223,13 @@ public boolean equals(Object o) {
Arrays.equals(tags, that.tags) &&
Objects.equals(nodeMeta, that.nodeMeta) &&
Objects.equals(queryParams, that.queryParams) &&
Objects.equals(token, that.token);
Objects.equals(token, that.token) &&
Objects.equals(cached, that.cached);
}

@Override
public int hashCode() {
int result = Objects.hash(datacenter, near, nodeMeta, passing, queryParams, token);
int result = Objects.hash(datacenter, near, nodeMeta, passing, queryParams, token, cached);
result = 31 * result + Arrays.hashCode(tags);
return result;
}
Expand Down