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"));
}
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()));
}
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>")))));
}
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())));
}
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")));
}
Aggregations