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

Update example plugin to latest api #316

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import com.hotels.styx.api.HttpHandler;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.api.StyxObservable;
import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.LiveHttpRequest;
import com.hotels.styx.api.LiveHttpResponse;
import com.hotels.styx.api.plugins.spi.Plugin;

import java.util.Map;
Expand Down Expand Up @@ -56,7 +58,7 @@ public ExamplePlugin(ExamplePluginConfig config) {
* @return
*/
@Override
public StyxObservable<HttpResponse> intercept(HttpRequest request, Chain chain) {
public Eventual<LiveHttpResponse> intercept(LiveHttpRequest request, Chain chain) {
/* the intercept method is where you can modify the request, modify the response and
handle side-effects.

Expand All @@ -68,7 +70,7 @@ public StyxObservable<HttpResponse> intercept(HttpRequest request, Chain chain)
* */

// Here is a simple example of modifying an incoming request.
HttpRequest newRequest = request.newBuilder()
LiveHttpRequest newRequest = request.newBuilder()
.header("myRequestHeader", config.requestHeaderValue())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
*/
package com.hotels.styx;

import com.hotels.styx.api.Buffer;
import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.HttpInterceptor;
import com.hotels.styx.api.HttpRequest;
import com.hotels.styx.api.HttpResponse;
import com.hotels.styx.api.StyxObservable;
import com.hotels.styx.api.Eventual;
import com.hotels.styx.api.LiveHttpRequest;
import com.hotels.styx.api.LiveHttpResponse;
import org.testng.annotations.Test;


import static com.hotels.styx.api.HttpRequest.get;
import static com.hotels.styx.api.HttpResponse.response;
import static com.hotels.styx.api.LiveHttpRequest.get;
import static com.hotels.styx.api.LiveHttpResponse.response;
import static com.hotels.styx.api.HttpResponseStatus.OK;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -44,15 +48,16 @@ public void addsExtraHeaders() throws Exception {
HttpInterceptor.Chain chain = request -> {
assertThat(request.header("myRequestHeader").orElse(null), is("foo"));

return StyxObservable.of(response(OK).build());
return Eventual.of(response(OK).build());
};

// an example request you expect your plugin to receive
HttpRequest request = get("/foo")
LiveHttpRequest request = get("/foo")
.build();


//The method StyxFutures.await() in styx-common wraps future.get() including appropriate Exception handling.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StyxFutures is not included in the API, so we should probably remove this comment.
Additionally, we have two utility classes for CompetableFutures in the same module+package, StyxFutures and CompletableFutures. Maybe we want to take this chance to fix that.

HttpResponse response = plugin.intercept(request, chain).asCompletableFuture().get();
LiveHttpResponse response = plugin.intercept(request, chain).asCompletableFuture().get();
assertThat(response.header("myResponseheader").orElse(null), is("bar"));
}
}