Lazily initialise Http client

This commit is contained in:
Luck 2018-02-15 22:03:53 +00:00
parent 620b0c898c
commit df5240a67c
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -38,12 +38,21 @@ import java.io.IOException;
*/ */
public class HttpClient { public class HttpClient {
private static final OkHttpClient CLIENT = new OkHttpClient.Builder() private static OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new LuckPermsUserAgentInterceptor()) .addInterceptor(new LuckPermsUserAgentInterceptor())
.build(); .build();
private synchronized static OkHttpClient getClient() {
if (client == null) {
client = new OkHttpClient.Builder()
.addInterceptor(new LuckPermsUserAgentInterceptor())
.build();
}
return client;
}
public static Response makeCall(Request request) throws IOException { public static Response makeCall(Request request) throws IOException {
Response response = CLIENT.newCall(request).execute(); Response response = getClient().newCall(request).execute();
if (!response.isSuccessful()) { if (!response.isSuccessful()) {
throw exceptionForUnsuccessfulResponse(response); throw exceptionForUnsuccessfulResponse(response);
} }