Skip to content

Instantly share code, notes, and snippets.

@mosheeshel
Last active May 25, 2021 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mosheeshel/188214e51022a50d249898bd1309ad10 to your computer and use it in GitHub Desktop.
Save mosheeshel/188214e51022a50d249898bd1309ad10 to your computer and use it in GitHub Desktop.
LaunchDarkly, Create and Add UserSegmentRules with the Java API based on examples given here
package com.moshe.ldclient;
import com.launchdarkly.api.ApiClient;
import com.launchdarkly.api.ApiException;
import com.launchdarkly.api.Configuration;
import com.launchdarkly.api.api.UserSegmentsApi;
import com.launchdarkly.api.auth.ApiKeyAuth;
import com.launchdarkly.api.model.*;
import com.launchdarkly.sdk.LDUser;
import com.launchdarkly.sdk.server.LDClient;
import com.launchdarkly.sdk.server.LDConfig;
import com.launchdarkly.sdk.server.interfaces.LDClientInterface;
import java.util.List;
/**
* Created by: moshee
* Date: 2021-05-13 2:49 p.m.
**/
public class FeatureToggleClientImpl {
private UserSegmentsApi apiInstance;
public FeatureToggleClientImpl() {
final ApiClient apiClient = Configuration.getDefaultApiClient();
apiClient.setDebugging(true);
ApiKeyAuth Token = (ApiKeyAuth) apiClient.getAuthentication("Token");
Token.setApiKey("launch-darkly-api-key");
this.apiInstance = new UserSegmentsApi(apiClient); // initiate UserSegmentAPI client with debugging
}
public FeatureToggleClientImpl(LDClientInterface ldClient, UserSegmentsApi apiInstance) {
this.apiInstance = apiInstance;
}
public String createSegment(String key, String description) {
String projectKey = "default"; // String | The project key, used to tie the flags together under one project so they can be managed together.
String environmentKey = "production"; // String | The environment key, used to tie together flag configuration and users under one environment so they can be managed together.
UserSegmentBody userSegmentBody = new UserSegmentBody(); // UserSegmentBody | Create a new user segment.
userSegmentBody.setName(key);
userSegmentBody.setUnbounded(false);
userSegmentBody.setKey(key);
userSegmentBody.setDescription(description);
userSegmentBody.setTags(List.of("api", "generated")); // set relevant tags
PatchOperation patchOperation = new PatchOperation();
patchOperation.setOp("add");// OP - add/replace/remove
patchOperation.setPath("/rules/0"); // rules are numbered - got to start from 0
Clause clause = new Clause(); // a clause is the rule filter.
clause.setAttribute("attribute"); // use any attribute in the UserContext
clause.setOp("in"); // OP - in/endsWith/startsWith/matches/contains/greaterThanOrEqual
clause.setValues(List.of(16)); // casts to the relevant type in LaunchDarkly
clause.setNegate(false); // use negate to get opposite of `op` endsWith + negate = true, means does NOT endWith
patchOperation.setValue(new Clauses(List.of(clause))); // include clause in rule
UserSegment result;
String segmentKey = "";
try {
// You have to first create the User Segment, only later can you "Patch" it to add properties such as rules
result = apiInstance.getUserSegment(projectKey, environmentKey, key);
System.out.println(result);
// use the PATCH command to add/update values internal to the object, represented as PatchOperation(s)
UserSegment userSegment = apiInstance.patchUserSegment(projectKey, environmentKey, key, List.of(patchOperation));
System.out.println(userSegment);
} catch (ApiException e) {
System.err.println("Exception when calling UserSegmentsApi#postUserSegment");
e.printStackTrace();
}
return "";
}
// This structure adds the required `clauses` section is required for the API call to work
public class Clauses {
private List<Clause> clauses = null;
public Clauses(List<Clause> clauses) {
this.clauses = clauses;
}
@ApiModelProperty(required = true, value = "")
public List<Clause> getClauses() {
return clauses;
}
}
}
@mosheeshel
Copy link
Author

Source for structure is here
launchdarkly/ld-openapi#87 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment