/** * Creates a blocking client with a configured default timeout for blocking requests. * The asynchronous calls won't have a timeout by default. * @param delegate underlying sphere client which may be initialized with the {@link SphereClientFactory}. * @param defaultTimeout the default maximum duration to wait (to block the thread) which should be greater than the timeout of the underlying HTTP client * @return wrapped client which can perform blocking calls. */ static BlockingSphereClient of(final SphereClient delegate, final Duration defaultTimeout) { return of(delegate, defaultTimeout.toMillis(), TimeUnit.MILLISECONDS); } }
public static void withChannel(final BlockingSphereClient client, final ChannelDraft channelDraft, final Consumer<Channel> f) { final Channel channel = client.executeBlocking(ChannelCreateCommand.of(channelDraft)); f.accept(channel); client.executeBlocking(ChannelDeleteCommand.of(channel)); }
public synchronized static void shutdownClient() { if (client != null) { client.close(); client = null; } }
@Test public void shouldRetrieveCorrelationId() throws ExecutionException, InterruptedException { final CorrelationIdRequestDecorator correlationIdRequestDecorator = new CorrelationIdRequestDecorator(ProjectGet.of()); client().executeBlocking(correlationIdRequestDecorator); assertThat(correlationId.isPresent()).isTrue(); final List<String> correlationIdParts = Arrays.asList(correlationId.get().split("/")); assertThat(correlationIdParts).hasSize(2); assertThat(correlationIdParts.get(0)).isEqualTo(client().getConfig().getProjectKey()); final UUID uuid = UUID.fromString(correlationIdParts.get(1)); assertThat(uuid).isNotNull(); }
public static void destroy(final BlockingSphereClient client, final Data data) { final List<CompletionStage<Product>> productDeletions = data.getAllProducts().stream() .map(product -> client.execute(ProductDeleteCommand.of(product))) .collect(toList()); productDeletions.forEach(stage -> SphereClientUtils.blockingWait(stage, 2, TimeUnit.SECONDS)); client.executeBlocking(ProductTypeDeleteCommand.of(data.getProductType())); }
@Test public void integrationTest() { final HttpClient httpClient = AsyncHttpClientAdapter.of(new AsyncHttpClient()); final SphereClientConfig sphereClientConfig = IntegrationTest.getSphereClientConfig(); final SphereClientFactory factory = SphereClientFactory.of(() -> httpClient); try (final BlockingSphereClient sphereClient = BlockingSphereClient.of(factory.createClient(sphereClientConfig), 10, TimeUnit.SECONDS)) { final Project project = sphereClient.executeBlocking(ProjectGet.of()); assertThat(project.getKey()).isEqualTo(sphereClientConfig.getProjectKey()); } } }
@Test public void alsoServesTheBaseInterface() { final BlockingSphereClient blockingSphereClient = blockingClientOf(new SphereClient() { @SuppressWarnings("unchecked") @Override public <T> CompletionStage<T> execute(final SphereRequest<T> sphereRequest) { return CompletableFutureUtils.successful((T) "hello"); } @Override public void close() { } @Override public SphereApiConfig getConfig() { return null; } }); assertThat(blockingSphereClient) .isInstanceOf(BlockingSphereClient.class) .as("also usable as classic sphere client") .isInstanceOf(SphereClient.class); assertThat(blockingSphereClient.execute(DummySphereRequest.of()).toCompletableFuture().join()).isEqualTo("hello"); }
public static void withApiClient(final BlockingSphereClient client, final List<SphereScope> scopes, final Consumer<ApiClient> consumer){ final SphereApiConfig config = client.getConfig(); final ApiClientDraft apiClientDraft = ApiClientDraftBuilder.of(GENERATED_CLIENT_NAME, config.getProjectKey(), scopes).build(); withApiClient(client,apiClientDraft,consumer); }
}); final SimpleMetricsSphereClient simpleMetricsSphereClient = SimpleMetricsSphereClient.of(asyncClient); final BlockingSphereClient blockingSphereClient = BlockingSphereClient.of(simpleMetricsSphereClient, 10, TimeUnit.SECONDS); final Observable metricObservable = simpleMetricsSphereClient.getMetricObservable(); final MetricObserver metricObserver = new MetricObserver(); blockingSphereClient.executeBlocking(sphereRequest);//warm up metricObservable.addObserver(metricObserver); blockingSphereClient.executeBlocking(sphereRequest);
public static void deleteProductsProductTypeAndProductDiscounts(final BlockingSphereClient client, final ProductType productType) { client.executeBlocking(ProductDiscountQuery.of().withLimit(500L)).getResults() .forEach(discount -> client.executeBlocking(ProductDiscountDeleteCommand.of(discount))); if (productType != null) { QueryPredicate<Product> ofProductType = ProductQueryModel.of().productType().is(productType); ProductQuery productsOfProductTypeQuery = ProductQuery.of().withPredicates(ofProductType).withLimit(500L); do { final List<Product> products = client.executeBlocking(productsOfProductTypeQuery).getResults(); final List<Product> unpublishedProducts = products.stream().map( product -> { if (product.getMasterData().isPublished()) { return client.executeBlocking(ProductUpdateCommand.of(product, Unpublish.of())); } else { return product; } } ).collect(toList()); final List<CompletionStage<Product>> stages = new LinkedList<>(); unpublishedProducts.forEach( product -> { final CompletionStage<Product> completionStage = client.execute(ProductDeleteCommand.of(product)); stages.add(completionStage); } ); stages.forEach(stage -> SphereClientUtils.blockingWait(stage, 30, TimeUnit.SECONDS)); deleteProductType(client, productType); } while (client.executeBlocking(productsOfProductTypeQuery).getCount() > 0); } }
public static void withState(final BlockingSphereClient client, final StateDraft stateDraft, final Consumer<State> consumer) { final State state = client.executeBlocking(StateCreateCommand.of(stateDraft)); consumer.accept(state); client.executeBlocking(StateDeleteCommand.of(state)); }
/** * Creates a blocking client with a configured default timeout for blocking requests. * The asynchronous calls won't have a timeout by default. * @param delegate underlying sphere client which may be initialized with the {@link SphereClientFactory}. * @param defaultTimeout the default maximum duration to wait (to block the thread) which should be greater than the timeout of the underlying HTTP client * @return wrapped client which can perform blocking calls. */ static BlockingSphereClient of(final SphereClient delegate, final Duration defaultTimeout) { return of(delegate, defaultTimeout.toMillis(), TimeUnit.MILLISECONDS); } }
cleanUp(); final ProductTypeDraft productTypeDraft = ProductTypeDraft.of(PRODUCT_TYPE_KEY, PRODUCT_TYPE_KEY, randomKey(), emptyList()); final ProductType productType = client().executeBlocking(ProductTypeCreateCommand.of(productTypeDraft)); final List<CompletionStage<Product>> productStages = new LinkedList<>(); for(int productIteration = 0; productIteration < productCount; productIteration++) { final ProductDraft draft = ProductDraftBuilder.of(productType, randomSlug(), randomSlug(), ProductVariantDraftBuilder.of().build()).build(); final CompletionStage<Product> productCompletionStage = client().execute(ProductCreateCommand.of(draft)); productStages.add(productCompletionStage); final Product productWithoutReview = client().executeBlocking(ProductCreateCommand.of(draft)); final int rating = random.nextInt(6); final ReviewDraft reviewDraft = ReviewDraftBuilder.ofRating(rating).target(product).build(); final CompletionStage<Review> reviewCompletionStage = client().execute(ReviewCreateCommand.of(reviewDraft)); reviewStages.add(reviewCompletionStage);
public synchronized static void shutdownClient() { if (client != null) { client.close(); client = null; } }
public static void withType(final BlockingSphereClient client, final TypeDraft draft, final Consumer<Type> consumer) { final Type type = client.executeBlocking(TypeCreateCommand.of(draft)); consumer.accept(type); client.executeBlocking(TypeDeleteCommand.of(type)); }
private BlockingSphereClient blockingClientOf(final SphereClient sphereClient) { return BlockingSphereClient.of(sphereClient, 500, TimeUnit.MILLISECONDS); } }
public static void withUpdateableReview(final BlockingSphereClient client, final UnaryOperator<ReviewDraftBuilder> builderUnaryOperator, final UnaryOperator<Review> f) { final ReviewDraft draft = builderUnaryOperator.apply(createReviewDraftBuilder()).build(); final Review review = client.executeBlocking(ReviewCreateCommand.of(draft)); final Review reviewToDelete = f.apply(review); client.executeBlocking(ReviewDeleteCommand.of(reviewToDelete)); }
public static BlockingSphereClient clientForStatusCode(final int statusCode) { final SphereClient sphereClient = TestDoubleSphereClientFactory .createHttpTestDouble(intent -> HttpResponse.of(statusCode)); return BlockingSphereClient.of(sphereClient, Duration.ofMillis(100)); }
public static void withCartDiscount(final BlockingSphereClient client, final Function<CartDiscount, CartDiscount> consumer) { final CartDiscountDraft draft = newCartDiscountDraftBuilder().build(); final CartDiscount cartDiscount = client.executeBlocking(CartDiscountCreateCommand.of(draft)); client.executeBlocking(CartDiscountDeleteCommand.of(consumer.apply(cartDiscount))); } }
private BlockingSphereClient createSphereClient() { final SphereClientFactory factory = SphereClientFactory.of(); final SphereClient asyncSphereClient = factory.createClient( "jvm-sdk-dev-1", //replace with your project key "ELqF0rykXD2fyS8s-IhIPKfQ", //replace with your client id "222222222222222222222222222222226"); //replace with your client secret return BlockingSphereClient.of(asyncSphereClient, 20, TimeUnit.SECONDS); } }