/** * Waits with a timeout for RESPONSES of a commercetools client wrapped in a {@link CompletionStage}. * This method should not be used for other {@link CompletionStage}s since it is throwing {@link io.sphere.sdk.models.SphereException}s. * * @param completionStage the future monad to wait for * @param timeout the maximum time to wait for this single request * @param unit the time unit of the timeout argument * @param <T> type of the result for the request * @return the wrapped value of {@code completionStage} * @throws SphereTimeoutException if a timeout occurs */ public static <T> T blockingWait(final CompletionStage<T> completionStage, final long timeout, final TimeUnit unit) { return blockingWait(completionStage, null, timeout, unit); }
public static <T> List<T> blockingWaitForEach(final Stream<? extends CompletionStage<T>> stream, final Duration duration) { return blockingWaitForEach(stream, duration.toMillis(), TimeUnit.MILLISECONDS); }
public static <S extends CompletionStage<T>, T> Collector<S, ?, List<T>> blockingWaitForEachCollector(final Duration duration) { return blockingWaitForEachCollector(duration.toMillis(), TimeUnit.MILLISECONDS); } }
/** * Waits with a timeout for RESPONSES of a commercetools client wrapped in a {@link CompletionStage}. * This method should not be used for other {@link CompletionStage}s since it is throwing {@link io.sphere.sdk.models.SphereException}s. * * @param completionStage the future monad to wait for * @param duration the maximum duration to wait for this single request * @param <T> type of the result for the request * @return the wrapped value of {@code completionStage} * @throws SphereTimeoutException if a timeout occurs */ public static <T> T blockingWait(final CompletionStage<T> completionStage, final Duration duration) { return blockingWait(completionStage, duration.toMillis(), TimeUnit.MILLISECONDS); }
public static <T> List<T> blockingWaitForEach(final List<? extends CompletionStage<T>> list, final long timeout, final TimeUnit unit) { return blockingWaitForEach(list.stream(), timeout, unit); }
/** * Waits with a timeout for RESPONSES of a commercetools client wrapped in a {@link CompletionStage}. * This method should not be used for other {@link CompletionStage}s since it is throwing {@link io.sphere.sdk.models.SphereException}s. * * @param completionStage the future monad to wait for * @param sphereRequest the request belonging to the completionStage * @param duration the maximum duration to wait for this single request * @param <T> type of the result for the request * @return the wrapped value of {@code completionStage} * @throws SphereTimeoutException if a timeout occurs */ public static <T> T blockingWait(final CompletionStage<T> completionStage, final SphereRequest<T> sphereRequest, final Duration duration) { return blockingWait(completionStage, sphereRequest, duration.toMillis(), TimeUnit.MILLISECONDS); }
public static <T> List<T> blockingWaitForEach(final List<? extends CompletionStage<T>> list, final Duration duration) { return blockingWaitForEach(list, duration.toMillis(), TimeUnit.MILLISECONDS); }
public static <T> List<T> blockingWaitForEach(final Stream<? extends CompletionStage<T>> stream, final long timeout, final TimeUnit unit) { return stream .map(stage -> blockingWait(stage, timeout, unit)) .collect(toList()); }
@Override public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long timeout, final TimeUnit unit) { final CompletionStage<T> completionStage = execute(sphereRequest); return blockingWait(completionStage, timeout, unit); } }
protected Optional<Zone> getZone(final Cart cart) { // Need to do this since zones are not expanded in shipping methods yet (but will be soon) // Rather this (even though it's expensive -one request per shipping method-) but it will mean less breaking changes in the future final ZoneFinderByCart zoneFinder = injector.getInstance(ZoneFinderByCart.class); return blockingWait(zoneFinder.findZone(cart), Duration.ofMinutes(1)); }
@Override public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long timeout, final TimeUnit unit) { final CompletionStage<T> completionStage = execute(sphereRequest); return blockingWait(completionStage, sphereRequest, timeout, unit); }
@Override public <T> T executeBlocking(final SphereRequest<T> sphereRequest, final long timeout, final TimeUnit unit) { final CompletionStage<T> completionStage = execute(sphereRequest); return blockingWait(completionStage, sphereRequest, timeout, unit); }
public static <S extends CompletionStage<T>, T> Collector<S, ?, List<T>> blockingWaitForEachCollector(final long timeout, final TimeUnit unit) { final Function<CompletionStage<T>, T> mapper = stage -> blockingWait(stage, timeout, unit); return Collectors.mapping(mapper, toList()); }
@Inject protected DefaultProjectContext(final Configuration configuration, @Named("global") final SphereClient client) { try { final SphereRequest<Project> request = ProjectGet.of(); final Project project = blockingWait(client.execute(request), Duration.ofMinutes(1)); this.locales = projectLocales(configuration, project); this.countryCodes = projectCountries(configuration, project); this.currencies = projectCurrencies(configuration, project); logger.debug("Provide ProjectContext: Languages {}, Countries {}, Currencies {}", locales, countryCodes, currencies); } catch (SphereTimeoutException e) { throw new RuntimeException("Could not fetch project information", e); } }
private MetaProductType getMetaProductType() { final List<ProductType> productTypes = blockingWait(queryAll(client, ProductTypeQuery.of()), 30, TimeUnit.SECONDS); return MetaProductType.of(productTypes); } }
private static List<Category> fetchCategories(final SphereClient client) { final List<Category> categories = blockingWait(queryAll(client, CategoryQuery.of()), 30, TimeUnit.SECONDS); return sortCategories(categories); }
@Test public void fetchAllAsJson_withMapper() throws Exception { final CompletionStage<List<JsonNode>> categoryPagesStage = QueryExecutionUtils .queryAll(client(), JsonNodeQuery.of("/categories"), category -> category, 500); final List<JsonNode> categoryNodes = SphereClientUtils .blockingWait(categoryPagesStage, Duration.ofMinutes(1)); assertThat(categoryNodes) .hasSize(15) .matches(cats -> cats.parallelStream() .anyMatch(cat -> cat.get("slug").get("en").asText().equals("boots-women"))); }
@Test public void fetchAllAsJson() throws Exception { final CompletionStage<List<JsonNode>> categoriesStage = QueryExecutionUtils.queryAll(client(), JsonNodeQuery.of("/categories"), 500); final List<JsonNode> categories = SphereClientUtils.blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(categories) .hasSize(15) .matches(cats -> cats.parallelStream().anyMatch(cat -> cat.get("slug").get("en").asText().equals("boots-women"))); }
@Test public void collectAllExternalIds() throws Exception { final List<String> externalIds = new ArrayList<>(); final Consumer<Category> categoryConsumer = category -> externalIds.add(category.getExternalId()); final CompletionStage<Void> categoriesStage = QueryExecutionUtils .queryAll(client(), CategoryQuery.of(), categoryConsumer, 500); SphereClientUtils.blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(externalIds).hasSize(15); IntStream.range(0, externalIds.size()).forEach(index -> assertThat(externalIds).contains(index + "")); }
@Test public void fetchAllExternalIdsWithNonUniformPageSizes() throws Exception { final CompletionStage<List<String>> categoriesStage = QueryExecutionUtils .queryAll(client(), CategoryQuery.of(), Category::getExternalId, 4); final List<String> externalIds = SphereClientUtils .blockingWait(categoriesStage, Duration.ofMinutes(1)); assertThat(externalIds).hasSize(15); IntStream.range(0, externalIds.size()).forEach(index -> assertThat(externalIds).contains(index + "")); }