Last active
May 25, 2021 11:36
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source for structure is here
launchdarkly/ld-openapi#87 (comment)