Skip to content
Merged
Show file tree
Hide file tree
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
@@ -1,58 +1,49 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.actors;

import okhttp3.OkHttpClient;

/**
* Builds an instance of DaprAsyncClient or DaprClient.
*/
class DaprClientBuilder {

/**
* Default port for Dapr after checking environment variable.
*/
private int port = DaprClientBuilder.GetEnvPortOrDefault();

/**
* Builds an async client.
* @return Builds an async client.
*/
public DaprAsyncClient buildAsyncClient() {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
// TODO: Expose configurations for OkHttpClient or com.microsoft.rest.RestClient.
return new DaprHttpAsyncClient(this.port, builder.build());
}

/**
* Overrides the port.
* @param port New port.
* @return This instance.
*/
public DaprClientBuilder withPort(int port) {
this.port = port;
return this;
}

/**
* Tries to get a valid port from environment variable or returns default.
* @return Port defined in env variable or default.
*/
private static int GetEnvPortOrDefault() {
String envPort = System.getenv(Constants.ENV_DAPR_HTTP_PORT);
if (envPort == null) {
return Constants.DEFAULT_PORT;
}

try {
return Integer.parseInt(envPort.trim());
} catch (NumberFormatException e) {
e.printStackTrace();
}

return Constants.DEFAULT_PORT;
}
}
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*/

package io.dapr.actors;

import okhttp3.OkHttpClient;
import io.dapr.actors.*;

/**
* Base class for client builders
*/
public abstract class AbstractClientBuilder {

/**
* Default port for Dapr after checking environment variable.
*/
private int port = AbstractClientBuilder.GetEnvPortOrDefault();

/**
* Overrides the port.
* @param port New port.
* @return This instance.
*/
public AbstractClientBuilder withPort(int port) {
this.port = port;
return this;
}

/**
* Tries to get a valid port from environment variable or returns default.
* @return Port defined in env variable or default.
*/
protected static int GetEnvPortOrDefault() {
String envPort = System.getenv(Constants.ENV_DAPR_HTTP_PORT);
if (envPort == null) {
return Constants.DEFAULT_PORT;
}

try {
return Integer.parseInt(envPort.trim());
} catch (NumberFormatException e) {
e.printStackTrace();
}

return Constants.DEFAULT_PORT;
}
}
Loading