@Test public void testFetch() throws Exception { final Optional<SortedSet<Notification>> actual = client.fetch("test"); assertThat(actual.isPresent()).isTrue(); final SortedSet<Notification> notifications = actual.get(); assertThat(notifications.size()).isEqualTo(1); assertThat(notifications.first().getId().isPresent()).isTrue(); }
public Optional<SortedSet<Notification>> fetch(final String username) { final URI uri = getTarget(username); final ImmutableSortedSet.Builder<Notification> results = ImmutableSortedSet.naturalOrder(); String nextRange = null; boolean paginate = true; try (Timer.Context context = fetchTimer.time()) { while (paginate) { LOGGER.info("GET {}", uri); final Invocation.Builder builder = client.target(uri).request(APPLICATION_JSON); if (nextRange != null) { builder.header("Range", nextRange); } final Response response = builder.get(); nextRange = response.getHeaderString("Next-Range"); if (nextRange == null) { paginate = false; } if (response.getStatus() == Response.Status.OK.getStatusCode() || response.getStatus() == Response.Status.PARTIAL_CONTENT.getStatusCode()) { results.addAll(response.readEntity(new GenericType<List<Notification>>() {})); } response.close(); } return Optional.of(results.build()); } catch (Exception e) { LOGGER.warn("Unable to fetch notification for {}", username, e); } return Optional.empty(); }
NotificationClient implements Closeable { public Optional<SortedSet<Notification>> fetch(final String username) { final URI uri = getTarget(username); final ImmutableSortedSet.Builder<Notification> results = ImmutableSortedSet.naturalOrder(); String nextRange = null; boolean paginate = true; try (Timer.Context context = fetchTimer.time()) { while (paginate) { LOGGER.info("GET {}", uri); final Invocation.Builder builder = client.target(uri).request(APPLICATION_JSON); if (nextRange != null) { builder.header("Range", nextRange); } final Response response = builder.get(); nextRange = response.getHeaderString("Next-Range"); if (nextRange == null) { paginate = false; } if (response.getStatus() == Response.Status.OK.getStatusCode() || response.getStatus() == Response.Status.PARTIAL_CONTENT.getStatusCode()) { results.addAll(response.readEntity(new GenericType<List<Notification>>() {})); } response.close(); } return Optional.of(results.build()); } catch (Exception e) { LOGGER.warn("Unable to fetch notification for {}", username, e); } return Optional.empty(); } }
NotificationClient implements Closeable { public Optional<SortedSet<Notification>> fetch(final String username) { final URI uri = getTarget(username); final ImmutableSortedSet.Builder<Notification> results = ImmutableSortedSet.naturalOrder(); String nextRange = null; boolean paginate = true; try (Timer.Context context = fetchTimer.time()) { while (paginate) { LOGGER.info("GET {}", uri); final Invocation.Builder builder = client.target(uri).request(APPLICATION_JSON); if (nextRange != null) { builder.header("Range", nextRange); } final Response response = builder.get(); nextRange = response.getHeaderString("Next-Range"); if (nextRange == null) { paginate = false; } if (response.getStatus() == Response.Status.OK.getStatusCode() || response.getStatus() == Response.Status.PARTIAL_CONTENT.getStatusCode()) { results.addAll(response.readEntity(new GenericType<List<Notification>>() {})); } response.close(); } return Optional.of(results.build()); } catch (Exception e) { LOGGER.warn("Unable to fetch notification for {}", username, e); } return Optional.empty(); } NotificationClient(final MetricRegistry registry, final Client client, final URI uri); }
NotificationClient implements Closeable { public Optional<SortedSet<Notification>> fetch(final String username) { final URI uri = getTarget(username); final ImmutableSortedSet.Builder<Notification> results = ImmutableSortedSet.naturalOrder(); String nextRange = null; boolean paginate = true; try (Timer.Context context = fetchTimer.time()) { while (paginate) { LOGGER.info("GET {}", uri); final Invocation.Builder builder = client.target(uri).request(APPLICATION_JSON); if (nextRange != null) { builder.header("Range", nextRange); } final Response response = builder.get(); nextRange = response.getHeaderString("Next-Range"); if (nextRange == null) { paginate = false; } if (response.getStatus() == Response.Status.OK.getStatusCode() || response.getStatus() == Response.Status.PARTIAL_CONTENT.getStatusCode()) { results.addAll(response.readEntity(new GenericType<List<Notification>>() {})); } response.close(); } return Optional.of(results.build()); } catch (Exception e) { LOGGER.warn("Unable to fetch notification for {}", username, e); } return Optional.empty(); } NotificationClient(final MetricRegistry registry, final Client client, final URI uri); Optional<SortedSet<Notification>> fetch(final String username); Optional<Notification> store(final String username, final Notification notification); void delete(final String username, final Collection<String> ids); void delete(final String username); boolean ping(); String version(); @Override void close(); }
NotificationClient implements Closeable { public Optional<SortedSet<Notification>> fetch(final String username) { final URI uri = getTarget(username); final ImmutableSortedSet.Builder<Notification> results = ImmutableSortedSet.naturalOrder(); String nextRange = null; boolean paginate = true; try (Timer.Context context = fetchTimer.time()) { while (paginate) { LOGGER.info("GET {}", uri); final Invocation.Builder builder = client.target(uri).request(APPLICATION_JSON); if (nextRange != null) { builder.header("Range", nextRange); } final Response response = builder.get(); nextRange = response.getHeaderString("Next-Range"); if (nextRange == null) { paginate = false; } if (response.getStatus() == Response.Status.OK.getStatusCode() || response.getStatus() == Response.Status.PARTIAL_CONTENT.getStatusCode()) { results.addAll(response.readEntity(new GenericType<List<Notification>>() {})); } response.close(); } return Optional.of(results.build()); } catch (Exception e) { LOGGER.warn("Unable to fetch notification for {}", username, e); } return Optional.empty(); } NotificationClient(final MetricRegistry registry, final Client client, final URI uri); Optional<SortedSet<Notification>> fetch(final String username); Optional<Notification> store(final String username, final Notification notification); void delete(final String username, final Collection<String> ids); void delete(final String username); boolean ping(); String version(); @Override void close();  }