Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.

Commit fdc3543

Browse files
author
Dan Richelson
committed
api_key->sdk_key. Change logging when stream is reconnecting to be less spammy.
1 parent 16e2cdc commit fdc3543

File tree

8 files changed

+44
-58
lines changed

8 files changed

+44
-58
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ Quick setup
1818

1919
import com.launchdarkly.client.*;
2020

21-
2. Create a new LDClient with your API key:
21+
2. Create a new LDClient with your SDK key:
2222

23-
LDClient ldClient = new LDClient("YOUR_API_KEY");
23+
LDClient ldClient = new LDClient("YOUR_SDK_KEY");
2424

2525
Your first feature flag
2626
-----------------------

src/main/java/com/launchdarkly/client/EventProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class EventProcessor implements Closeable {
2121
private final ScheduledExecutorService scheduler;
2222
private final Random random = new Random();
2323
private final BlockingQueue<Event> queue;
24-
private final String apiKey;
24+
private final String sdkKey;
2525
private final LDConfig config;
2626
private final Consumer consumer;
2727

28-
EventProcessor(String apiKey, LDConfig config) {
29-
this.apiKey = apiKey;
28+
EventProcessor(String sdkKey, LDConfig config) {
29+
this.sdkKey = sdkKey;
3030
this.queue = new ArrayBlockingQueue<>(config.capacity);
3131
this.consumer = new Consumer(config);
3232
this.config = config;
@@ -85,7 +85,7 @@ private void postEvents(List<Event> events) {
8585
Gson gson = new Gson();
8686
String json = gson.toJson(events);
8787

88-
HttpPost request = config.postEventsRequest(apiKey, "/bulk");
88+
HttpPost request = config.postEventsRequest(sdkKey, "/bulk");
8989
StringEntity entity = new StringEntity(json, "UTF-8");
9090
entity.setContentType("application/json");
9191
request.setEntity(entity);

src/main/java/com/launchdarkly/client/FeatureRequestor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
class FeatureRequestor {
2020

2121
public static final String GET_LATEST_FLAGS_PATH = "/sdk/latest-flags";
22-
private final String apiKey;
22+
private final String sdkKey;
2323
private final LDConfig config;
2424
private final CloseableHttpClient client;
2525
private static final Logger logger = LoggerFactory.getLogger(FeatureRequestor.class);
2626

27-
FeatureRequestor(String apiKey, LDConfig config) {
28-
this.apiKey = apiKey;
27+
FeatureRequestor(String sdkKey, LDConfig config) {
28+
this.sdkKey = sdkKey;
2929
this.config = config;
3030
this.client = createClient();
3131
}
@@ -58,7 +58,7 @@ protected CloseableHttpClient createClient() {
5858
Map<String, FeatureFlag> makeAllRequest() throws IOException {
5959
HttpCacheContext context = HttpCacheContext.create();
6060

61-
HttpGet request = config.getRequest(apiKey, GET_LATEST_FLAGS_PATH);
61+
HttpGet request = config.getRequest(sdkKey, GET_LATEST_FLAGS_PATH);
6262

6363
CloseableHttpResponse response = null;
6464
try {
@@ -107,7 +107,7 @@ FeatureFlag makeRequest(String featureKey, boolean latest) throws IOException {
107107

108108
String resource = latest ? "/api/eval/latest-features/" : "/api/eval/features/";
109109

110-
HttpGet request = config.getRequest(apiKey,resource + featureKey);
110+
HttpGet request = config.getRequest(sdkKey,resource + featureKey);
111111

112112
CloseableHttpResponse response = null;
113113
try {

src/main/java/com/launchdarkly/client/LDClient.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class LDClient implements Closeable {
3636
protected static final String CLIENT_VERSION = getClientVersion();
3737

3838
private final LDConfig config;
39-
private final String apiKey;
39+
private final String sdkKey;
4040
private final FeatureRequestor requestor;
4141
private final EventProcessor eventProcessor;
4242
private UpdateProcessor updateProcessor;
@@ -45,24 +45,24 @@ public class LDClient implements Closeable {
4545
* Creates a new client instance that connects to LaunchDarkly with the default configuration. In most
4646
* cases, you should use this constructor.
4747
*
48-
* @param apiKey the API key for your account
48+
* @param sdkKey the SDK key for your LaunchDarkly environment
4949
*/
50-
public LDClient(String apiKey) {
51-
this(apiKey, LDConfig.DEFAULT);
50+
public LDClient(String sdkKey) {
51+
this(sdkKey, LDConfig.DEFAULT);
5252
}
5353

5454
/**
5555
* Creates a new client to connect to LaunchDarkly with a custom configuration. This constructor
5656
* can be used to configure advanced client features, such as customizing the LaunchDarkly base URL.
5757
*
58-
* @param apiKey the API key for your account
58+
* @param sdkKey the SDK key for your LaunchDarkly environment
5959
* @param config a client configuration object
6060
*/
61-
public LDClient(String apiKey, LDConfig config) {
61+
public LDClient(String sdkKey, LDConfig config) {
6262
this.config = config;
63-
this.apiKey = apiKey;
64-
this.requestor = createFeatureRequestor(apiKey, config);
65-
this.eventProcessor = createEventProcessor(apiKey, config);
63+
this.sdkKey = sdkKey;
64+
this.requestor = createFeatureRequestor(sdkKey, config);
65+
this.eventProcessor = createEventProcessor(sdkKey, config);
6666

6767
if (config.offline) {
6868
logger.info("Starting LaunchDarkly client in offline mode");
@@ -76,7 +76,7 @@ public LDClient(String apiKey, LDConfig config) {
7676

7777
if (config.stream) {
7878
logger.info("Enabling streaming API");
79-
this.updateProcessor = createStreamProcessor(apiKey, config, requestor);
79+
this.updateProcessor = createStreamProcessor(sdkKey, config, requestor);
8080
} else {
8181
logger.info("Disabling streaming API");
8282
this.updateProcessor = createPollingProcessor(config);
@@ -101,18 +101,18 @@ public boolean initialized() {
101101
}
102102

103103
@VisibleForTesting
104-
protected FeatureRequestor createFeatureRequestor(String apiKey, LDConfig config) {
105-
return new FeatureRequestor(apiKey, config);
104+
protected FeatureRequestor createFeatureRequestor(String sdkKey, LDConfig config) {
105+
return new FeatureRequestor(sdkKey, config);
106106
}
107107

108108
@VisibleForTesting
109-
protected EventProcessor createEventProcessor(String apiKey, LDConfig config) {
110-
return new EventProcessor(apiKey, config);
109+
protected EventProcessor createEventProcessor(String sdkKey, LDConfig config) {
110+
return new EventProcessor(sdkKey, config);
111111
}
112112

113113
@VisibleForTesting
114-
protected StreamProcessor createStreamProcessor(String apiKey, LDConfig config, FeatureRequestor requestor) {
115-
return new StreamProcessor(apiKey, config, requestor);
114+
protected StreamProcessor createStreamProcessor(String sdkKey, LDConfig config, FeatureRequestor requestor) {
115+
return new StreamProcessor(sdkKey, config, requestor);
116116
}
117117

118118
@VisibleForTesting
@@ -382,7 +382,7 @@ public boolean isOffline() {
382382

383383
/**
384384
* For more info: <a href=https://github.com/launchdarkly/js-client#secure-mode>https://github.com/launchdarkly/js-client#secure-mode</a>
385-
* @param user The User to be hashed along with the api key
385+
* @param user The User to be hashed along with the sdk key
386386
* @return the hash, or null if the hash could not be calculated.
387387
*/
388388
public String secureModeHash(LDUser user) {
@@ -391,7 +391,7 @@ public String secureModeHash(LDUser user) {
391391
}
392392
try {
393393
Mac mac = Mac.getInstance(HMAC_ALGORITHM);
394-
mac.init(new SecretKeySpec(apiKey.getBytes(), HMAC_ALGORITHM));
394+
mac.init(new SecretKeySpec(sdkKey.getBytes(), HMAC_ALGORITHM));
395395
return Hex.encodeHexString(mac.doFinal(user.getKeyAsString().getBytes("UTF8")));
396396
} catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException e) {
397397
logger.error("Could not generate secure mode hash", e);

src/main/java/com/launchdarkly/client/LDConfig.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,12 @@ private URIBuilder getEventsBuilder() {
376376
.setPort(eventsURI.getPort());
377377
}
378378

379-
HttpGet getRequest(String apiKey, String path) {
379+
HttpGet getRequest(String sdkKey, String path) {
380380
URIBuilder builder = this.getBuilder().setPath(path);
381381

382382
try {
383383
HttpGet request = new HttpGet(builder.build());
384-
request.addHeader("Authorization", "api_key " + apiKey);
384+
request.addHeader("Authorization", sdkKey);
385385
request.addHeader("User-Agent", "JavaClient/" + LDClient.CLIENT_VERSION);
386386

387387
return request;
@@ -391,27 +391,12 @@ HttpGet getRequest(String apiKey, String path) {
391391
}
392392
}
393393

394-
HttpPost postRequest(String apiKey, String path) {
395-
URIBuilder builder = this.getBuilder().setPath(path);
396-
397-
try {
398-
HttpPost request = new HttpPost(builder.build());
399-
request.addHeader("Authorization", "api_key " + apiKey);
400-
request.addHeader("User-Agent", "JavaClient/" + LDClient.CLIENT_VERSION);
401-
402-
return request;
403-
} catch (Exception e) {
404-
logger.error("Unhandled exception in LaunchDarkly client", e);
405-
return null;
406-
}
407-
}
408-
409-
HttpPost postEventsRequest(String apiKey, String path) {
394+
HttpPost postEventsRequest(String sdkKey, String path) {
410395
URIBuilder builder = this.getEventsBuilder().setPath(eventsURI.getPath() + path);
411396

412397
try {
413398
HttpPost request = new HttpPost(builder.build());
414-
request.addHeader("Authorization", "api_key " + apiKey);
399+
request.addHeader("Authorization", sdkKey);
415400
request.addHeader("User-Agent", "JavaClient/" + LDClient.CLIENT_VERSION);
416401

417402
return request;

src/main/java/com/launchdarkly/client/StreamProcessor.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ class StreamProcessor implements UpdateProcessor {
2323

2424
private final FeatureStore store;
2525
private final LDConfig config;
26-
private final String apiKey;
26+
private final String sdkKey;
2727
private final FeatureRequestor requestor;
2828
private EventSource es;
2929
private AtomicBoolean initialized = new AtomicBoolean(false);
3030

3131

32-
StreamProcessor(String apiKey, LDConfig config, FeatureRequestor requestor) {
32+
StreamProcessor(String sdkKey, LDConfig config, FeatureRequestor requestor) {
3333
this.store = config.featureStore;
3434
this.config = config;
35-
this.apiKey = apiKey;
35+
this.sdkKey = sdkKey;
3636
this.requestor = requestor;
3737
}
3838

@@ -41,7 +41,7 @@ public Future<Void> start() {
4141
final VeryBasicFuture initFuture = new VeryBasicFuture();
4242

4343
Headers headers = new Headers.Builder()
44-
.add("Authorization", "api_key " + this.apiKey)
44+
.add("Authorization", this.sdkKey)
4545
.add("User-Agent", "JavaClient/" + LDClient.CLIENT_VERSION)
4646
.add("Accept", "text/event-stream")
4747
.build();
@@ -102,7 +102,8 @@ public void onMessage(String name, MessageEvent event) throws Exception {
102102

103103
@Override
104104
public void onError(Throwable throwable) {
105-
logger.warn("Encountered EventSource error", throwable);
105+
logger.error("Encountered EventSource error: " + throwable.getMessage());
106+
logger.debug("", throwable);
106107
}
107108
};
108109

src/main/java/com/launchdarkly/client/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ static boolean handleResponse(Logger logger, HttpRequestBase request, CloseableH
4343
return true;
4444
}
4545
if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
46-
logger.error("[401] Invalid API key when accessing URI: " + request.getURI().toString());
46+
logger.error("[401] Invalid SDK key when accessing URI: " + request.getURI().toString());
4747
} else {
4848
logger.error("[" + statusCode + "] " + response.getStatusLine().getReasonPhrase() + " When accessing URI: " + request.getURI().toString());
4949
}

src/test/java/com/launchdarkly/client/LDClientTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,14 @@ private void assertDefaultValueIsReturned() {
357357
}
358358

359359
private LDClient createMockClient(LDConfig config) {
360-
return new LDClient("API_KEY", config) {
360+
return new LDClient("SDK_KEY", config) {
361361
@Override
362-
protected FeatureRequestor createFeatureRequestor(String apiKey, LDConfig config) {
362+
protected FeatureRequestor createFeatureRequestor(String sdkKey, LDConfig config) {
363363
return requestor;
364364
}
365365

366366
@Override
367-
protected StreamProcessor createStreamProcessor(String apiKey, LDConfig config, FeatureRequestor requestor) {
367+
protected StreamProcessor createStreamProcessor(String sdkKey, LDConfig config, FeatureRequestor requestor) {
368368
return streamProcessor;
369369
}
370370

@@ -374,7 +374,7 @@ protected PollingProcessor createPollingProcessor(LDConfig config) {
374374
}
375375

376376
@Override
377-
protected EventProcessor createEventProcessor(String apiKey, LDConfig config) {
377+
protected EventProcessor createEventProcessor(String sdkKey, LDConfig config) {
378378
return eventProcessor;
379379
}
380380
};

0 commit comments

Comments
 (0)