Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Future expectation 4.x #5197

Merged
merged 2 commits into from
May 7, 2024
Merged

Future expectation 4.x #5197

merged 2 commits into from
May 7, 2024

Conversation

vietj
Copy link
Member

@vietj vietj commented May 7, 2024

Future control flow using predicates.

Future has already synchronous operations modifying the control flow

  • map : transforms a mapper failure into a failed future (e.g. future.map(val -> throw new VertxException()))
  • otherwise : transforms a failure into a success

This contribution aims to transform a success into a failure based on the result of a predicate.

Goals

  • capture useful predicates that modify the control flow, e.g. a predicate that checks the HTTP client response (status code, headers, etc...)
  • provide inline guards using lambda expressions

Vert.x Web Client has something close to this with HTTP expectations:

    client
      .get(8080, "myserver.mycompany.com", "/some-uri")
      .expect(ResponsePredicate.SC_SUCCESS)
      .expect(ResponsePredicate.JSON)
      .send()
      .onSuccess(res -> {
        // Safely decode the body as a json object
        JsonObject body = res.bodyAsJsonObject();
        System.out.println(
          "Received response with status code" +
            res.statusCode() +
            " with body " +
            body);
      })
      .onFailure(err ->
        System.out.println("Something went wrong " + err.getMessage()));

A predicate like interface Expectation: a synchronous predicate that checks a boolean value and has a method to create a meaningful exception.

Future<Buffer> body = client.request(options)
  .compose(req -> req
    .send()
    .expecting(resp -> resp.statusCode() == 200)
    .compose(resp -> resp.body()); 

The HTTP response status code can be captured so it can be reused.

Future<Buffer> body = client.request(options)
  .compose(req -> req
    .send()
    .expecting(HttpResponseExpectation.SC_OK)
    .compose(resp -> resp.body()); 

@vietj vietj added this to the 4.5.8 milestone May 7, 2024
@vietj vietj merged commit dc12f93 into 4.x May 7, 2024
7 checks passed
@vietj vietj deleted the future-expectation-4.x branch May 7, 2024 12:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant