private <T> T callRegistryEndpoint(RegistryEndpointProvider<T> registryEndpointProvider)
      throws IOException, RegistryException {
    int bearerTokenRefreshes = 0;
    while (true) {
      try {
        return new RegistryEndpointCaller<>(
                eventHandlers,
                getUserAgent(),
                registryEndpointProvider,
                authorization.get(),
                registryEndpointRequestProperties,
                httpClient)
            .call();

      } catch (RegistryUnauthorizedException ex) {
        if (ex.getHttpResponseException().getStatusCode()
                != HttpStatusCodes.STATUS_CODE_UNAUTHORIZED
            || !isBearerAuth(authorization.get())
            || ++bearerTokenRefreshes >= MAX_BEARER_TOKEN_REFRESH_TRIES) {
          throw ex;
        }

        // Because we successfully did bearer authentication initially, getting 401 here probably
        // means the token was expired.
        String wwwAuthenticate = ex.getHttpResponseException().getHeaders().getAuthenticate();
        authorization.set(refreshBearerAuth(wwwAuthenticate));
      }
    }
  }
Calls the registry endpoint.

@param registryEndpointProvider the {@link RegistryEndpointProvider} to the endpoint
@throws IOException if communicating with the endpoint fails
@throws RegistryException if communicating with the endpoint fails
jib-core/src/main/java/com/google/cloud/tools/jib/registry/RegistryClient.java