Giter Club home page Giter Club logo

riptide's Introduction

Riptide: A next generation HTTP client

Tidal wave

Stability: Active Build Status Coverage Status Code Quality Javadoc Release Maven Central License

Riptide noun, /ˈrɪp.taɪd/: strong flow of water away from the shore

Riptide is a library that implements client-side response routing. It tries to fill the gap between the HTTP protocol and Java. Riptide allows users to leverage the power of HTTP with its unique API.

  • Technology stack: Based on spring-web and uses the same foundation as Spring's RestTemplate.
  • Status: Actively maintained and used in production.
  • Riptide is unique in the way that it doesn't abstract HTTP away, but rather embraces it!

🚨 If you want to upgrade from an older version to the latest one, consult the following migration guides:

Example

Usage typically looks like this:

http.get("/repos/{org}/{repo}/contributors", "zalando", "riptide")
    .dispatch(series(),
        on(SUCCESSFUL).call(listOf(User.class), users -> 
            users.forEach(System.out::println)));

Feel free to compare this e.g. to Feign or Retrofit.

Features

Origin

Most modern clients try to adapt HTTP to a single-return paradigm as shown in the following example. Even though this may be perfectly suitable for most applications, it takes away a lot of the power that comes with HTTP. It's not easy to support multiple return values, i.e. distinct happy cases. Access to response headers or manual content negotiation are also more difficult.

@GET
@Path("/repos/{org}/{repo}/contributors")
List<User> getContributors(@PathParam String org, @PathParam String repo);

Riptide tries to counter this by providing a different approach to leverage the power of HTTP. Go checkout the concept document for more details.

Dependencies

  • Java 17
  • Spring 6

Installation

Add the following dependency to your project:

<dependency>
    <groupId>org.zalando</groupId>
    <artifactId>riptide-core</artifactId>
    <version>${riptide.version}</version>
</dependency>

Additional modules/artifacts of Riptide always share the same version number.

Alternatively, you can import our bill of materials...

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.zalando</groupId>
      <artifactId>riptide-bom</artifactId>
      <version>${riptide.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

... which allows you to omit versions:

<dependencies>
  <dependency>
      <groupId>org.zalando</groupId>
      <artifactId>riptide-core</artifactId>
  </dependency>
  <dependency>
      <groupId>org.zalando</groupId>
      <artifactId>riptide-failsafe</artifactId>
  </dependency>
  <dependency>
      <groupId>org.zalando</groupId>
      <artifactId>riptide-faults</artifactId>
  </dependency>
</dependencies>

Configuration

Integration of your typical Spring Boot Application with Riptide, Logbook and Tracer can be greatly simplified by using the Riptide: Spring Boot Starter. Go check it out!

Http.builder()
    .executor(Executors.newCachedThreadPool())
    .requestFactory(new HttpComponentsClientHttpRequestFactory())
    .baseUrl("https://api.github.com")
    .converter(new MappingJackson2HttpMessageConverter())
    .converter(new Jaxb2RootElementHttpMessageConverter())
    .plugin(new OriginalStackTracePlugin())
    .build();

The following code is the bare minimum, since a request factory is required:

Http.builder()
    .executor(Executors.newCachedThreadPool())
    .requestFactory(new HttpComponentsClientHttpRequestFactory())
    .build();

This defaults to:

Thread Pool

All off the standard Executors.new*Pool() implementations only support the queue-first style, i.e. the pool scales up to the core pool size, then fills the queue and only then will scale up to the maximum pool size.

Riptide provides a ThreadPoolExecutors.builder() which also offers a scale-first style where thread pools scale up to the maximum pool size before they queue any tasks. That usually leads to higher throughput, lower latency on the expense of having to maintain more threads.

The following table shows which combination of properties are supported

Configuration Supported
Without queue, fixed size¹ ✔️
Without queue, elastic size² ✔️
Bounded queue, fixed size ✔️
Bounded queue, elastic size ✔️
Unbounded queue, fixed size ✔️
Unbounded queue, elastic size ❌³
Scale first, without queue, fixed size ❌⁴
Scale first, without queue, elastic size ❌⁴
Scale first, bounded queue, fixed size ❌⁵
Scale first, bounded queue, elastic size ✔️⁶
Scale first, unbounded queue, fixed size ❌⁵
Scale first, unbounded queue, elastic size ✔️⁶

¹ Core pool size = maximum pool size
² Core pool size < maximum pool size
³ Pool can't grow past core pool size due to unbounded queue
⁴ Scale first has no meaning without a queue
⁵ Fixed size pools are already scaled up
⁶ Elastic, but only between 0 and maximum pool size

Examples

  1. Without queue, elastic size

    ThreadPoolExecutors.builder()
        .withoutQueue()
        .elasticSize(5, 20)
        .keepAlive(1, MINUTES)
        .build()
  2. Bounded queue, fixed size

    ThreadPoolExecutors.builder()
        .boundedQueue(20)
        .fixedSize(20)
        .keepAlive(1, MINUTES)
        .build()
  3. Scale-first, unbounded queue, elastic size

    ThreadPoolExecutors.builder()
        .scaleFirst()
        .unboundedQueue()
        .elasticSize(20)   
        .keepAlive(1, MINUTES)
        .build()

You can read more about scale-first here:

In order to configure the thread pool correctly, please refer to How to set an ideal thread pool size.

Non-blocking IO

🚨 While the previous versions of Riptide supported both, blocking and non-blocking request factories, due to the removal of AsyncClientHttpRequestFactory in Spring 6, Riptide 4 only supports blocking request factories:

In order to provide asynchrony for blocking IO you need to register an executor. Not passing an executor will make all network communication synchronous, i.e. all futures returned by Riptide will already be completed.

Synchronous Asynchronous
ClientHttpRequestFactory Executor + ClientHttpRequestFactory

Usage

Requests

A full-blown request may contain any of the following aspects: HTTP method, request URI, query parameters, headers and a body:

http.post("/sales-order")
    .queryParam("async", "false")
    .contentType(CART)
    .accept(SALES_ORDER)
    .header("Client-IP", "127.0.0.1")
    .body(cart)
    //...

Riptide supports the following HTTP methods: get, head, post, put, patch, delete, options and trace respectively. Query parameters can either be provided individually using queryParam(String, String) or multiple at once with queryParams(Multimap<String, String>).

The following operations are applied to URI Templates (get(String, Object...)) and URIs (get(URI)) respectively:

URI Template

URI

  • none, used as is
  • expected to be already encoded

Both

  • after respective transformation
  • resolved against Base URL (if present)
  • Query String (merged with existing)
  • Normalization

The URI Resolution table shows some examples how URIs are resolved against Base URLs, based on the chosen resolution strategy.

The Content-Type- and Accept-header have type-safe methods in addition to the generic support that is header(String, String) and headers(HttpHeaders).

Responses

Riptide is special in the way it handles responses. Rather than having a single return value, you need to register callbacks. Traditionally, you would attach different callbacks for different response status codes. Alternatively, there are built-in routing capabilities on status code families (called series in Spring) as well as on content types.

http.post("/sales-order")
    // ...
    .dispatch(series(),
        on(SUCCESSFUL).dispatch(contentType(),
            on(SALES_ORDER).call(SalesOrder.class, this::persist),
        on(CLIENT_ERROR).dispatch(status(),
            on(CONFLICT).call(this::retry),
            on(PRECONDITION_FAILED).call(this::readAgainAndRetry),
            anyStatus().call(problemHandling())),
        on(SERVER_ERROR).dispatch(status(),
            on(SERVICE_UNAVAILABLE).call(this::scheduleRetryLater))));

The callbacks can have the following signatures:

persist(SalesOrder)
retry(ClientHttpResponse)
scheduleRetryLater()

Futures

Riptide will return a CompletableFuture<ClientHttpResponse>. That means you can choose to chain transformations/callbacks or block on it.

If you need proper return values take a look at Riptide: Capture.

Exceptions

The only special custom exception you may receive is UnexpectedResponseException, if and only if there was no matching condition and no wildcard condition.

Plugins

Riptide comes with a way to register extensions in the form of plugins.

  • OriginalStackTracePlugin, preserves stack traces when executing requests asynchronously
  • AuthorizationPlugin, adds Authorization support
  • FailsafePlugin, adds retries, circuit breaker, backup requests and timeout support
  • MicrometerPlugin, adds metrics for request duration
  • TransientFaults, detects transient faults, e.g. network issues

Whenever you encounter the need to perform some repetitive task on the futures returned by a remote call, you may consider implementing a custom Plugin for it.

Plugins are executed in phases:

Plugin phases

Please consult the Plugin documentation for details.

Testing

Riptide is built on the same foundation as Spring's RestTemplate. That allows us, with a small trick, to use the same testing facilities, the MockRestServiceServer:

RestTemplate template = new RestTemplate();
MockRestServiceServer server = MockRestServiceServer.createServer(template);
ClientHttpRequestFactory requestFactory = template.getRequestFactory();

Http.builder()
    .requestFactory(requestFactory)
    // continue configuration

We basically use an intermediate RestTemplate as a holder of the special ClientHttpRequestFactory that the MockRestServiceServer manages.

If you are using Spring Boot Starter, the test setup is provided by a convenient annotation @RiptideClientTest. See here.

Getting help

If you have questions, concerns, bug reports, etc., please file an issue in this repository's Issue Tracker.

Getting involved/Contributing

To contribute, simply make a pull request and add a brief description (1-2 sentences) of your addition or change. For more details check the contribution guidelines.

Credits and references

riptide's People

Contributors

alexanderyastrebov avatar bocytko avatar buzzlighty avatar cberg-zalando avatar danpersa avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar dingxiangfei2009 avatar epaul avatar evilkid avatar fatroom avatar gitter-badger avatar gpradeepkrishna avatar gregdeane avatar jhorstmann avatar jmatusewicz avatar jonasjurczok avatar jrehwaldt avatar lukasniemeier-zalando avatar lzx404243 avatar mazenmelouk avatar msdousti avatar semernitskaya avatar snurmine avatar thomasabraham avatar tkrop avatar whiskeysierra avatar yuppie-flu avatar zeitlinger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

riptide's Issues

TypedCondition::call should use Optional

Right now TypedCondition::call expects a EntityConsumer<I> when creating a Binding for type I. I think it would be better if the consumer is of EntityConsumer<Optional<I>>.

Looking at HttpMessageConverterExtractor there are some cases where the entity will be null (e.g. empty response body for a HEAD request). Client's should think about such cases when using TypedCondition::call.

Add support for “fall-through“

In case two different conditions target the same action:

rest.execute(GET, url)
    .dispatch(status(),
        on(SUCCESS)
        .on(CREATED, ..).call(..));

Or alternatively support arbitrary predicates for conditions, not just equals.

Consider supporting other interfaces of RestTemplate

Right now the only interface of RestTemplate one can use with riptide is execute - this means one have to implement a RequestCallback when adding headers or a body, which is kind of cumbersome due to its low-level nature.

It would be great if riptide somehow leverages the magic of RestTemplate including its MessageConverters and co

Improve readme

  • routing tree
  • comparison to request routing on the server side
  • default/wildcard actions for more robust clients

Allow retrieval of non-Optional values

A normal use case for accessing the value of the Optional returned from the Retriever looks like this:

Retriever retriever = rest.execute(GET, url)
    .dispatch(status(),
        on(SUCCESS, Success.class).capture(),
        on(BAD_REQUEST).call(propagate()),
        anyStatus().call(this::fail));

retriever.retrieve(String.class).orElseThrow(AssertionError::new);

Depending on the dispatch spec one can see that the only way we exit the dispatch method without an exception is the happy case, i.e. any exception raised when accessing the Optional would be caused by a bug, either in riptide or the client that is using it.

A second method on the Retriever to access captured values raw, without being wrapped in an optional would help.

Allow call() with a parameterless "consumer" (e.g.. Runnable)

There are some cases where the conditions of the dispatch function are already everything which is interesting (i.e. neither response body nor any remaining header information matter). (An example is the 200 response from Nakadi's "submit events" operation.)

But currently I still need to pass to .call() a consumer of either some class mapped from the response, or even the HttpClientResponse.

I propose to add a call method in UntypedCondition which accepts a Runnable (or a throwing version thereof), simply discarding the ClientHttpResponse and calling the runnable.

For now I'm using this wrap function as a workaround:

    private static ThrowingConsumer<ClientHttpResponse, ?> wrap(Runnable runnable) {
        return response -> runnable.run();
    }

Add better support for exceptions/problems

i.e. by having special versions of capture

try {
  rest.execute(..).dispatch(
      on(PROBLEM, Problem.java).propagate()
} catch (OutOfStockProblem e) {

} catch (Exception e) {

}

Support reuse of routers for routing trees.

Currently, users users have to create the routing tree for each Rest instance. As far as I can see the routing tree for many request is pretty the same (pretty likely thread safe) and can be often declared static in advance of the request and reused on different occasions.

To allow declaration of independent routing trees, on solution would be support the stand alone creation and reuse of routers for routing trees. An example usage would look as follows:

private static final Router<MediaType> CONTENT_ROUTER = create(contentType(),
        on(TEXT_PLAIN).capture(String.class),
        anyContentType().call(MyTest::failContent));

private static final Router<HttpStatus> STATUS_ROUTER = create(status(),
        on(OK).dispatch(CONTENT_ROUTER),
        on(BAD_GATEWAY).call(MyTest::badGateway),
        anyStatus().call(MyTest::failStatus));

...

    Rest rest = Rest.create(new RestTemplate());
    rest.execute(GET, URI.create("http://my.uri/api"))
            .dispatch(STATUS_ROUTER)
            .as(String.class);

Example for retrieving created resource

  • PUT/POST
  • 201 response
  • read Location header
  • check for presence of Content-Location header
  • resolve both against request URI
  • compare both
  • return body, if match
  • issue get request otherwise

Update README

  • add title, e.g. Riptide: Client-side Response Routing
  • add descriptions to selectors
  • add origin/reason for existence
  • document that the underlying RestTemplate shouldn't be changed afterwards
  • define Route and RoutingTree
  • Visualize routing tree
  • How to systematically produce a routing tree
  • How to programmatically modify the routing tree

Improve type inference for Retriever.retrieve(TypeToken)

The current signature is

public <T> Optional<T> retrieve(final TypeToken<?> type)

Meaning the type is inferenced based on the variable this is assigned to. The type should instead be inferred based on the generic type of the TypeToken parameter.

Add redirection example to readme

private String send(URI url, String body) {
    return unit.execute(POST, url, body).dispatch(series(),
            on(SUCCESSFUL, String.class).capture(),
            on(REDIRECTION).map(response ->
                    follow(response, body)).capture())
            .to(String.class);
}

private String follow(final ClientHttpResponse response, final String body) {
    final String url = response.getHeaders().getLocation();
    // log redirect follow
    return send(url, body);
}

Retrieval of Parameterized Types

The Conditions API allows the usage of TypeToken which enables clients to specify parameterized types for conversion. This type information is lost after conversion and clients are not able to retrieve any captured value by such TypeToken.

final TypeToken<List<String>> type = new TypeToken<List<String>>() {};
final Retriever retriever = unit.execute(GET, url).dispatch(status(),
        on(OK, type).capture(),
        anyStatus().call(this::error));

retriever.retrieve(type);         // is null
retriever.retrieve(List.class);   // retrieves captured value

Preserving the type information clients would not have to cast in their client code.

Allow map + dispatch

private URI create(URI url, String body) {
    return unit.execute(POST, url, body).dispatch(series(),
            on(SUCCESSFUL).map(normalize(url)).dispatch(sameHeader("Location"),
                    on("Content-Location").capture(),
                    any(String.class).capture(location().andThen(location ->
                            unit.execute(GET, location).dispatch(series(),
                                    on(SUCCESSFUL).capture(String.class),
                                    anySeries().call(this::fail))
                                    .to(String.class)))),
            anySeries().call(this::fail))
            .to(URI.class);
}

Move type information from condition to actions

The current syntax looks like this

Success success = rest.execute(GET, url)
        .dispatch(series(),
                on(SUCCESSFUL, Success.class).capture(),
                on(ACCEPTED, Success.class).map(this::handle).capture(),
                on(CLIENT_ERROR)
                    .dispatch(status(),
                            on(UNAUTHORIZED).call(this::login),
                            on(UNPROCESSABLE_ENTITY)
                                    .dispatch(contentType(),
                                            on(PROBLEM, ThrowableProblem.class).call(propagate()),
                                            on(ERROR, Exception.class).call(propagate()))),
                on(SERVER_ERROR)
                    .dispatch(statusCode(),
                            on(503).call(this::retryLater)),
                anySeries().call(this::fail))
        .as(Success.class).orElse(null);

I'm thinking whether it makes sense to move the type information from conditions to action level:

Success success = rest.execute(GET, url)
        .dispatch(series(),
                on(SUCCESSFUL).capture(Success.class),
                on(ACCEPTED).map(Success.class, this::handle).capture(),
                on(CLIENT_ERROR)
                    .dispatch(status(),
                            on(UNAUTHORIZED).call(this::login),
                            on(UNPROCESSABLE_ENTITY)
                                    .dispatch(contentType(),
                                            on(PROBLEM).call(ThrowableProblem.class, propagate()),
                                            on(ERROR).call(Exception.class, propagate()))),
                on(SERVER_ERROR)
                    .dispatch(statusCode(),
                            on(503).call(this::retryLater)),
                anySeries().call(this::fail))
        .as(Success.class).orElse(null);

@lukasniemeier-zalando What do you think?

Propagate response to upper level if no match found

Assuming you get a 200 appication/json back and this is your routing table:

2xx
  200
    application/xml
    text/plain
  201
  any: onAnySuccess
4xx
5xx

Since there is no specific match for that content type in 2xx > 200 riptide should fall back to the onAnySuccess handler (or even higher up the chain) if needed.

Reconsider exception handling concept

Right now the exception handling is kind of shady:

  • any*().call(..) handles the no match case (which is fine)
  • it also catches and swallows any RestClientException and HttpMessageNotReadableException
  • the general idea should be: pass through everything as-is from custom callbacks/mappers as well as from the underlying RestTemplate
  • the only case I can think of right now that justifies a custom exception is no match without an any condition

How do I mock Riptide?

I'm currently writing an application based on Riptide, and I'm struggling on how to test the class which does the Riptide setup.

My first try was to mock just the RestTemplate, i.e. to include Riptide (as far as it is used) in my test.
But this is getting cumbersome, as I have to check how Riptide is using RestTemplate (and its related classes/interfaces) to get access to the raw request which is being sent.

So I guess a better idea would be to mock Riptide where it is used in my tested class.
But as that consists of a large bunch of callbacks being passed, I'm not sure how to do this elegantly ... do you have any examples or a guide?

Just for fun, here is my method which needs to be tested:

    private void submitToURI(final List<? extends Map<String, Object>> events, final NakadiResponseCallback callback,
            final HttpHeaders headers, final URI url) {
        log.info("submitting {} events to {} ...", events.size(), url);
        rest.execute(HttpMethod.POST, url, headers, events) //
            .dispatch(series(),
                on(SUCCESSFUL).dispatch(status(),
                    on(MULTI_STATUS, BATCH_RESULT_TYPE_TOKEN).call(callback::partiallySubmitted),
                    on(OK).call(response -> callback.successfullyPublished()), //
                    anyStatus().call(response -> callback.otherSuccessStatus(response.getStatusCode()))),
                on(REDIRECTION).call(response -> {
                    final URI redirectUrl = response.getHeaders().getLocation();

                    // TODO: handle redirect loops? Or let the
                    // StackOverflowError handle this?
                    log.info("redirecting from {} to {}", url, redirectUrl);
                    submitToURI(events, callback, headers, redirectUrl);
                }),
                on(CLIENT_ERROR).dispatch(status(),
                    on(HttpStatus.UNPROCESSABLE_ENTITY, BATCH_RESULT_TYPE_TOKEN).call(callback::validationProblem),
                    anyStatus().dispatch(Selectors.contentType(),

                        // TODO: check if Nakadi actually sends application/problem+json back. Otherwise this won't work.
                        on(PROBLEM_MEDIA_TYPE, Problem.class).call(callback::clientError),
                        anyContentType().call(response -> callback.clientError(unknownProblem(response))))),
                on(Series.SERVER_ERROR).dispatch(Selectors.contentType(),
                    on(PROBLEM_MEDIA_TYPE, Problem.class).call(callback::serverError), //
                    anyContentType().call(response -> callback.serverError(unknownProblem(response)))));
    }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.