Skip to content

Commit

Permalink
Merge pull request #616 from cescoffier/features/update-to-mutiny-0.6.0
Browse files Browse the repository at this point in the history
Update quickstarts to avoid using the deprecated APIS from Mutiny 0.6.0
  • Loading branch information
gsmet authored Jul 20, 2020
2 parents 1f7289d + a7b091d commit 7cb3bcb
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class FruitAsyncService extends AbstractService {

public Uni<List<Fruit>> findAll() {
return Uni.createFrom().completionStage(() -> dynamoDB.scan(scanRequest()))
.onItem().apply(res -> res.items().stream().map(Fruit::from).collect(Collectors.toList()));
.onItem().transform(res -> res.items().stream().map(Fruit::from).collect(Collectors.toList()));
}

public Uni<List<Fruit>> add(Fruit fruit) {
Expand All @@ -27,6 +27,6 @@ public Uni<List<Fruit>> add(Fruit fruit) {

public Uni<Fruit> get(String name) {
return Uni.createFrom().completionStage(() -> dynamoDB.getItem(getRequest(name)))
.onItem().apply(resp -> Fruit.from(resp.item()));
.onItem().transform(resp -> Fruit.from(resp.item()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ public class QuarkusKmsAsyncResource {
@Path("/encrypt")
public Uni<String> encrypt(String data) {
return Uni.createFrom().completionStage(kms.encrypt(req -> req.keyId(keyArn).plaintext(SdkBytes.fromUtf8String(data))))
.onItem().apply(EncryptResponse::ciphertextBlob)
.onItem().apply(blob -> Base64.encodeBase64String(blob.asByteArray()));
.onItem().transform(EncryptResponse::ciphertextBlob)
.onItem().transform(blob -> Base64.encodeBase64String(blob.asByteArray()));
}

@POST
@Path("/decrypt")
public Uni<String> decrypt(String data) {
return Uni.createFrom().item(SdkBytes.fromByteArray(Base64.decodeBase64(data.getBytes())))
.onItem().produceCompletionStage(msg -> kms.decrypt(req -> req.keyId(keyArn).ciphertextBlob(msg)))
.onItem().apply(DecryptResponse::plaintext)
.onItem().apply(SdkBytes::asUtf8String);
.onItem().transformToUni(msg ->
Uni.createFrom().completionStage(kms.decrypt(req -> req.keyId(keyArn).ciphertextBlob(msg)))
)
.onItem().transform(DecryptResponse::plaintext)
.onItem().transform(SdkBytes::asUtf8String);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public Uni<List<FileObject>> listFiles() {
.build();

return Uni.createFrom().completionStage(() -> s3.listObjects(listRequest))
.onItem().apply(result -> toFileItems(result));
.onItem().transform(result -> toFileItems(result));
}

private List<FileObject> toFileItems(ListObjectsResponse objects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public Uni<String> encrypt(Email data) {
.message(msg -> msg
.subject(sub -> sub.data(data.getSubject()))
.body(b -> b.text(txt -> txt.data(data.getBody()))))))
.onItem().apply(SendEmailResponse::messageId);
.onItem().transform(SendEmailResponse::messageId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Uni<Response> publish(Quark quark) throws Exception {
return Uni.createFrom()
.completionStage(sns.publish(p -> p.topicArn(topicArn).message(message)))
.onItem().invoke(item -> LOGGER.infov("Fired Quark[{0}, {1}}]", quark.getFlavor(), quark.getSpin()))
.onItem().apply(PublishResponse::messageId)
.onItem().apply(id -> Response.ok().entity(id).build());
.onItem().transform(PublishResponse::messageId)
.onItem().transform(id -> Response.ok().entity(id).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ public Uni<Response> notificationEndpoint(@HeaderParam("x-amz-sns-message-type")

if (messageType.equals(NOTIFICATION_TYPE)) {
return Uni.createFrom().item(readObject(SnsNotification.class, message))
.onItem().apply(notification -> readObject(Quark.class, notification.getMessage()))
.onItem().transform(notification -> readObject(Quark.class, notification.getMessage()))
.onItem().invoke(quark -> LOGGER.infov("Quark[{0}, {1}] collision with the shield.", quark.getFlavor(), quark.getSpin()))
.onItem().apply(quark -> Response.ok().build());
.onItem().transform(quark -> Response.ok().build());
} else if (messageType.equals(SUBSCRIPTION_CONFIRMATION_TYPE)) {
return Uni.createFrom().item(readObject(SnsSubscriptionConfirmation.class, message))
.onItem().produceCompletionStage(msg -> sns.confirmSubscription(confirm -> confirm.topicArn(topicArn).token(msg.getToken())))
.onItem().transformToUni(msg ->
Uni.createFrom().completionStage(
sns.confirmSubscription(confirm -> confirm.topicArn(topicArn).token(msg.getToken()))
)
)
.onItem().invoke(resp -> LOGGER.info("Subscription confirmed. Ready for quarks collisions."))
.onItem().apply(resp -> Response.ok().build());
.onItem().transform(resp -> Response.ok().build());
} else if (messageType.equals(UNSUBSCRIPTION_CONFIRMATION_TYPE)) {
LOGGER.info("We are unsubscribed");
return Uni.createFrom().item(Response.ok().build());
Expand All @@ -79,10 +83,10 @@ public Uni<Response> notificationEndpoint(@HeaderParam("x-amz-sns-message-type")
public Uni<Response> subscribe() {
return Uni.createFrom()
.completionStage(sns.subscribe(s -> s.topicArn(topicArn).protocol("http").endpoint(notificationEndpoint())))
.onItem().apply(SubscribeResponse::subscriptionArn)
.onItem().transform(SubscribeResponse::subscriptionArn)
.onItem().invoke(this::setSubscriptionArn)
.onItem().invoke(arn -> LOGGER.infov("Subscribed Quarks shield with id = {0} ", arn))
.onItem().apply(arn -> Response.ok().entity(arn).build());
.onItem().transform(arn -> Response.ok().entity(arn).build());
}

@POST
Expand All @@ -93,7 +97,7 @@ public Uni<Response> unsubscribe() {
.completionStage(sns.unsubscribe(s -> s.subscriptionArn(subscriptionArn)))
.onItem().invoke(arn -> LOGGER.infov("Unsubscribed quarks shield for id = {0}", subscriptionArn))
.onItem().invoke(arn -> subscriptionArn = null)
.onItem().apply(arn -> Response.ok().build());
.onItem().transform(arn -> Response.ok().build());
} else {
return Uni.createFrom().item(Response.status(400).entity("Not subscribed yet").build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Uni<Response> sendMessage(Quark quark) throws Exception {
return Uni.createFrom()
.completionStage(sqs.sendMessage(m -> m.queueUrl(queueUrl).messageBody(message)))
.onItem().invoke(item -> LOGGER.infov("Fired Quark[{0}, {1}}]", quark.getFlavor(), quark.getSpin()))
.onItem().apply(SendMessageResponse::messageId)
.onItem().apply(id -> Response.ok().entity(id).build());
.onItem().transform(SendMessageResponse::messageId)
.onItem().transform(id -> Response.ok().entity(id).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public class QuarksShieldAsyncResource {
public Uni<List<Quark>> receive() {
return Uni.createFrom()
.completionStage(sqs.receiveMessage(m -> m.maxNumberOfMessages(10).queueUrl(queueUrl)))
.onItem().apply(ReceiveMessageResponse::messages)
.onItem().apply(m -> m.stream().map(Message::body).map(this::toQuark).collect(Collectors.toList()));
.onItem().transform(ReceiveMessageResponse::messages)
.onItem().transform(m -> m.stream().map(Message::body).map(this::toQuark).collect(Collectors.toList()));
}

private Quark toQuark(String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,29 @@ public Fruit(Long id, String name) {

public static Multi<Fruit> findAll(PgPool client) {
return client.query("SELECT id, name FROM fruits ORDER BY name ASC").execute()
.onItem().produceMulti(set -> Multi.createFrom().iterable(set))
.onItem().apply(Fruit::from);
.onItem().transformToMulti(set -> Multi.createFrom().iterable(set))
.onItem().transform(Fruit::from);
}

public static Uni<Fruit> findById(PgPool client, Long id) {
return client.preparedQuery("SELECT id, name FROM fruits WHERE id = $1").execute(Tuple.of(id))
.onItem().apply(RowSet::iterator)
.onItem().apply(iterator -> iterator.hasNext() ? from(iterator.next()) : null);
.onItem().transform(RowSet::iterator)
.onItem().transform(iterator -> iterator.hasNext() ? from(iterator.next()) : null);
}

public Uni<Long> save(PgPool client) {
return client.preparedQuery("INSERT INTO fruits (name) VALUES ($1) RETURNING (id)").execute(Tuple.of(name))
.onItem().apply(pgRowSet -> pgRowSet.iterator().next().getLong("id"));
.onItem().transform(pgRowSet -> pgRowSet.iterator().next().getLong("id"));
}

public Uni<Boolean> update(PgPool client) {
return client.preparedQuery("UPDATE fruits SET name = $1 WHERE id = $2").execute(Tuple.of(name, id))
.onItem().apply(pgRowSet -> pgRowSet.rowCount() == 1);
.onItem().transform(pgRowSet -> pgRowSet.rowCount() == 1);
}

public static Uni<Boolean> delete(PgPool client, Long id) {
return client.preparedQuery("DELETE FROM fruits WHERE id = $1").execute(Tuple.of(id))
.onItem().apply(pgRowSet -> pgRowSet.rowCount() == 1);
.onItem().transform(pgRowSet -> pgRowSet.rowCount() == 1);
}

private static Fruit from(Row row) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,30 @@ public Multi<Fruit> get() {
@Path("{id}")
public Uni<Response> getSingle(@PathParam Long id) {
return Fruit.findById(client, id)
.onItem().apply(fruit -> fruit != null ? Response.ok(fruit) : Response.status(Status.NOT_FOUND))
.onItem().apply(ResponseBuilder::build);
.onItem().transform(fruit -> fruit != null ? Response.ok(fruit) : Response.status(Status.NOT_FOUND))
.onItem().transform(ResponseBuilder::build);
}

@POST
public Uni<Response> create(Fruit fruit) {
return fruit.save(client)
.onItem().apply(id -> URI.create("/fruits/" + id))
.onItem().apply(uri -> Response.created(uri).build());
.onItem().transform(id -> URI.create("/fruits/" + id))
.onItem().transform(uri -> Response.created(uri).build());
}

@PUT
@Path("{id}")
public Uni<Response> update(@PathParam Long id, Fruit fruit) {
return fruit.update(client)
.onItem().apply(updated -> updated ? Status.OK : Status.NOT_FOUND)
.onItem().apply(status -> Response.status(status).build());
.onItem().transform(updated -> updated ? Status.OK : Status.NOT_FOUND)
.onItem().transform(status -> Response.status(status).build());
}

@DELETE
@Path("{id}")
public Uni<Response> delete(@PathParam Long id) {
return Fruit.delete(client, id)
.onItem().apply(deleted -> deleted ? Status.NO_CONTENT : Status.NOT_FOUND)
.onItem().apply(status -> Response.status(status).build());
.onItem().transform(deleted -> deleted ? Status.NO_CONTENT : Status.NOT_FOUND)
.onItem().transform(status -> Response.status(status).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public class ReactiveGreetingService {

public Uni<String> greeting(String name) {
return Uni.createFrom().item(name)
.onItem().apply(n -> String.format("hello %s", name));
.onItem().transform(n -> String.format("hello %s", name));
}

public Multi<String> greetings(int count, String name) {
return Multi.createFrom().ticks().every(Duration.ofSeconds(1))
.onItem().apply(n -> String.format("hello %s - %d", name, n))
.onItem().transform(n -> String.format("hello %s - %d", name, n))
.transform().byTakingFirstItems(count);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String helloBlocking(@PathParam("name") String name) {
@Path("/mutiny/{name}")
public Uni<String> helloMutiny(@PathParam("name") String name) {
return mutinyHelloService.sayHello(HelloRequest.newBuilder().setName(name).build())
.onItem().apply((reply) -> generateResponse(reply));
.onItem().transform((reply) -> generateResponse(reply));
}

public String generateResponse(HelloReply reply) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public String helloBlocking(@PathParam("name") String name) {
@Path("/mutiny/{name}")
public Uni<String> helloMutiny(@PathParam("name") String name) {
return mutinyHelloService.sayHello(HelloRequest.newBuilder().setName(name).build())
.onItem().apply(HelloReply::getMessage);
.onItem().transform(HelloReply::getMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class EventResource {
@Path("{name}")
public Uni<String> greeting(@PathParam String name) {
return bus.<String> request("greeting", name)
.onItem().apply(Message::body);
.onItem().transform(Message::body);
}
}
12 changes: 6 additions & 6 deletions vertx-quickstart/src/main/java/org/acme/vertx/Fruit.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public Fruit(Long id, String name) {

public static Uni<List<Fruit>> findAll(PgPool client) {
return client.query("SELECT id, name FROM fruits ORDER BY name ASC").execute()
.onItem().apply(pgRowSet -> {
.onItem().transform(pgRowSet -> {
List<Fruit> list = new ArrayList<>(pgRowSet.size());
for (Row row : pgRowSet) {
list.add(from(row));
Expand All @@ -57,23 +57,23 @@ public static Uni<List<Fruit>> findAll(PgPool client) {

public static Uni<Fruit> findById(PgPool client, Long id) {
return client.preparedQuery("SELECT id, name FROM fruits WHERE id = $1").execute(Tuple.of(id))
.onItem().apply(RowSet::iterator)
.onItem().apply(iterator -> iterator.hasNext() ? from(iterator.next()) : null);
.onItem().transform(RowSet::iterator)
.onItem().transform(iterator -> iterator.hasNext() ? from(iterator.next()) : null);
}

public Uni<Long> save(PgPool client) {
return client.preparedQuery("INSERT INTO fruits (name) VALUES ($1) RETURNING (id)").execute(Tuple.of(name))
.onItem().apply(pgRowSet -> pgRowSet.iterator().next().getLong("id"));
.onItem().transform(pgRowSet -> pgRowSet.iterator().next().getLong("id"));
}

public Uni<Boolean> update(PgPool client) {
return client.preparedQuery("UPDATE fruits SET name = $1 WHERE id = $2").execute(Tuple.of(name, id))
.onItem().apply(pgRowSet -> pgRowSet.rowCount() == 1);
.onItem().transform(pgRowSet -> pgRowSet.rowCount() == 1);
}

public static Uni<Boolean> delete(PgPool client, Long id) {
return client.preparedQuery("DELETE FROM fruits WHERE id = $1").execute(Tuple.of(id))
.onItem().apply(pgRowSet -> pgRowSet.rowCount() == 1);
.onItem().transform(pgRowSet -> pgRowSet.rowCount() == 1);
}

private static Fruit from(Row row) {
Expand Down
20 changes: 10 additions & 10 deletions vertx-quickstart/src/main/java/org/acme/vertx/FruitResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,38 +69,38 @@ private void initdb() {
@GET
public Uni<Response> get() {
return Fruit.findAll(client)
.onItem().apply(Response::ok)
.onItem().apply(ResponseBuilder::build);
.onItem().transform(Response::ok)
.onItem().transform(ResponseBuilder::build);
}

@GET
@Path("{id}")
public Uni<Response> getSingle(@PathParam Long id) {
return Fruit.findById(client, id)
.onItem().apply(fruit -> fruit != null ? Response.ok(fruit) : Response.status(Status.NOT_FOUND))
.onItem().apply(ResponseBuilder::build);
.onItem().transform(fruit -> fruit != null ? Response.ok(fruit) : Response.status(Status.NOT_FOUND))
.onItem().transform(ResponseBuilder::build);
}

@POST
public Uni<Response> create(Fruit fruit) {
return fruit.save(client)
.onItem().apply(id -> URI.create("/fruits/" + id))
.onItem().apply(uri -> Response.created(uri).build());
.onItem().transform(id -> URI.create("/fruits/" + id))
.onItem().transform(uri -> Response.created(uri).build());
}

@PUT
@Path("{id}")
public Uni<Response> update(@PathParam Long id, Fruit fruit) {
return fruit.update(client)
.onItem().apply(updated -> updated ? Status.OK : Status.NOT_FOUND)
.onItem().apply(status -> Response.status(status).build());
.onItem().transform(updated -> updated ? Status.OK : Status.NOT_FOUND)
.onItem().transform(status -> Response.status(status).build());
}

@DELETE
@Path("{id}")
public Uni<Response> delete(@PathParam Long id) {
return Fruit.delete(client, id)
.onItem().apply(deleted -> deleted ? Status.NO_CONTENT : Status.NOT_FOUND)
.onItem().apply(status -> Response.status(status).build());
.onItem().transform(deleted -> deleted ? Status.NO_CONTENT : Status.NOT_FOUND)
.onItem().transform(status -> Response.status(status).build());
}
}

0 comments on commit 7cb3bcb

Please sign in to comment.