Search in sources :

Example 1 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class SyncHandlerTest method shouldFlatMap.

@Test
public void shouldFlatMap() throws Exception {
    RequestContext context = TestContext.forPathArgs(ImmutableMap.of("foo", "bar", "bar", "baz"));
    SyncHandler<String> a = ctx -> ctx.pathArgs().get("foo");
    SyncHandler<String> b = a.flatMap(fooVal -> ctx -> ctx.pathArgs().get(fooVal));
    String baz = b.invoke(context);
    assertThat(baz, is("baz"));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Assert.assertThat(org.junit.Assert.assertThat) ImmutableMap(com.google.common.collect.ImmutableMap) RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test) RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test)

Example 2 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class MetricsCollectingEndpointRunnableFactoryDecoratorTest method shouldCopyMetadataFromIncomingRequestContext.

@Test
public void shouldCopyMetadataFromIncomingRequestContext() throws Exception {
    decorated.create(ongoingRequest, requestContext, endpoint).run();
    RequestContext copied = requestContextCaptor.getValue();
    assertThat(copied.metadata(), is(requestContext.metadata()));
}
Also used : RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test)

Example 3 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class RouteTransformMetricsExampleTest method shouldTrackResponsePayloadSize.

@Test
public void shouldTrackResponsePayloadSize() throws Exception {
    Route<AsyncHandler<Response<ByteString>>> testRoute = Route.sync("GET", "/foo/<name>", context -> Response.forPayload(ByteString.encodeUtf8(context.pathArgs().get("name"))));
    Route<AsyncHandler<Response<ByteString>>> trackedRoute = withResponsePayloadSizeHistogram(testRoute);
    RequestContext context = mock(RequestContext.class);
    when(context.pathArgs()).thenReturn(Collections.singletonMap("name", "bar"));
    trackedRoute.handler().invoke(context).toCompletableFuture().get();
    assertThat(registry.getHistograms().keySet(), containsInAnyOrder(hasProperty("tags", allOf(hasEntry("service", "example"), hasEntry("what", "endpoint-response-size"), hasEntry("endpoint", "GET:/foo/<name>")))));
}
Also used : AsyncHandler(com.spotify.apollo.route.AsyncHandler) ByteString(okio.ByteString) RequestContext(com.spotify.apollo.RequestContext) Test(org.junit.Test)

Example 4 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class AlbumResource method getAlbums.

CompletionStage<Response<ArrayList<Album>>> getAlbums(RequestContext requestContext, String tag) {
    // We need to first query the search API, parse the result, then query the album API.
    Request searchRequest = Request.forUri(SEARCH_API + "?type=album&q=tag%3A" + tag);
    Client client = requestContext.requestScopedClient();
    return client.send(searchRequest).thenComposeAsync(response -> {
        String ids = parseResponseAlbumIds(response.payload().get().utf8());
        return client.send(Request.forUri(ALBUM_API + "?ids=" + ids));
    }).thenApplyAsync(response -> Response.ok().withPayload(parseAlbumData(response.payload().get().utf8())));
}
Also used : Response(com.spotify.apollo.Response) AsyncHandler(com.spotify.apollo.route.AsyncHandler) Request(com.spotify.apollo.Request) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) RequestContext(com.spotify.apollo.RequestContext) Album(com.spotify.apollo.example.data.Album) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonSerializerMiddlewares(com.spotify.apollo.route.JsonSerializerMiddlewares) IOException(java.io.IOException) Route(com.spotify.apollo.route.Route) ArrayList(java.util.ArrayList) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) StringJoiner(java.util.StringJoiner) ByteString(okio.ByteString) Artist(com.spotify.apollo.example.data.Artist) JsonNode(com.fasterxml.jackson.databind.JsonNode) Client(com.spotify.apollo.Client) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Client(com.spotify.apollo.Client)

Example 5 with RequestContext

use of com.spotify.apollo.RequestContext in project apollo by spotify.

the class ArtistResource method getArtistTopTracks.

CompletionStage<Response<ArrayList<Track>>> getArtistTopTracks(RequestContext requestContext) {
    // Validate request
    Optional<String> query = requestContext.request().parameter("q");
    if (!query.isPresent()) {
        return CompletableFuture.completedFuture(Response.forStatus(Status.BAD_REQUEST.withReasonPhrase("No search query")));
    }
    String country = requestContext.pathArgs().get("country");
    // We need to first query the search API, parse the result, then query top-tracks.
    Request searchRequest = Request.forUri(SEARCH_API + "?type=artist&q=" + query.get());
    Client client = requestContext.requestScopedClient();
    return client.send(searchRequest).thenComposeAsync(response -> {
        String topArtistId = parseFirstArtistId(response.payload().get().utf8());
        Request topTracksRequest = Request.forUri(String.format("%s/%s/top-tracks?country=%s", ARTIST_API, topArtistId, country));
        return client.send(topTracksRequest);
    }).thenApplyAsync(response -> Response.ok().withPayload(parseTopTracks(response.payload().get().utf8()))).exceptionally(throwable -> Response.forStatus(Status.INTERNAL_SERVER_ERROR.withReasonPhrase("Something failed")));
}
Also used : Response(com.spotify.apollo.Response) AsyncHandler(com.spotify.apollo.route.AsyncHandler) Request(com.spotify.apollo.Request) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) RequestContext(com.spotify.apollo.RequestContext) Album(com.spotify.apollo.example.data.Album) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonSerializerMiddlewares(com.spotify.apollo.route.JsonSerializerMiddlewares) IOException(java.io.IOException) CompletableFuture(java.util.concurrent.CompletableFuture) Route(com.spotify.apollo.route.Route) ArrayList(java.util.ArrayList) Track(com.spotify.apollo.example.data.Track) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) ByteString(okio.ByteString) Artist(com.spotify.apollo.example.data.Artist) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) Client(com.spotify.apollo.Client) Status(com.spotify.apollo.Status) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Client(com.spotify.apollo.Client)

Aggregations

RequestContext (com.spotify.apollo.RequestContext)10 Test (org.junit.Test)7 AsyncHandler (com.spotify.apollo.route.AsyncHandler)4 ByteString (okio.ByteString)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 Client (com.spotify.apollo.Client)3 Response (com.spotify.apollo.Response)3 Route (com.spotify.apollo.route.Route)3 CoreMatchers.is (org.hamcrest.CoreMatchers.is)3 Assert.assertThat (org.junit.Assert.assertThat)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 Request (com.spotify.apollo.Request)2 Album (com.spotify.apollo.example.data.Album)2 Artist (com.spotify.apollo.example.data.Artist)2 JsonSerializerMiddlewares (com.spotify.apollo.route.JsonSerializerMiddlewares)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)2