All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cloud.prefab.client.internal.ConnectivityTester Maven / Gradle / Ivy

Go to download

API Client for https://prefab.cloud: rate limits, feature flags and semaphores as a service

There is a newer version: 0.3.23
Show newest version
package cloud.prefab.client.internal;

import cloud.prefab.client.Options;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ConnectivityTester {

  private static final Logger LOG = LoggerFactory.getLogger(ConnectivityTester.class);
  private final HttpClient httpClient;
  private final Options options;

  ConnectivityTester(HttpClient httpClient, Options options) {
    this.httpClient = httpClient;
    this.options = options;
  }

  public boolean testHttps() {
    HttpRequest request = HttpRequest
      .newBuilder()
      .uri(URI.create(options.getPrefabApiUrl() + "/hello"))
      .build();
    try {
      HttpResponse response = httpClient.send(
        request,
        HttpResponse.BodyHandlers.discarding()
      );
      if (PrefabHttpClient.isSuccess(response.statusCode())) {
        LOG.info("HTTP connection check succeeded");
        return true;
      } else {
        LOG.info(
          "HTTP connection to {} failed with response code {}",
          request.uri(),
          response.statusCode()
        );
      }
    } catch (IOException e) {
      LOG.info(
        "HTTP connection to {} failed with IO exception {}",
        request.uri(),
        e.getMessage()
      );
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
    return false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy