use of com.spotify.apollo.Response in project apollo by spotify.
the class StubClient method send.
@Override
public CompletionStage<Response<ByteString>> send(Request request) {
final ResponseSource responseSource = responseSource(request);
if (responseSource == null) {
final NoMatchingResponseFoundException notFound = new NoMatchingResponseFoundException(formatRequestNotMatchedExceptionMessage(request));
final CompletableFuture<Response<ByteString>> notFoundFuture = new CompletableFuture<>();
notFoundFuture.completeExceptionally(notFound);
return notFoundFuture;
}
// Create response task
final ResponseWithDelay responseWithDelay = responseSource.create(request);
// Schedule a response in the future
final CompletableFuture<Response<ByteString>> future = new CompletableFuture<>();
final Runnable replyTask = () -> {
Response<ByteString> response = responseWithDelay.getResponse();
requestsAndResponses.add(RequestResponsePair.create(request, response));
future.complete(response);
};
if (responseWithDelay.getDelayMillis() <= 0) {
replyTask.run();
} else {
executor.schedule(replyTask, responseWithDelay.getDelayMillis(), TimeUnit.MILLISECONDS);
}
return future;
}
use of com.spotify.apollo.Response in project apollo by spotify.
the class MinimalApp method beer.
static CompletionStage<Response<String>> beer(RequestContext context) {
Request breweryRequest = Request.forUri("http://brewery/order");
CompletionStage<Response<ByteString>> orderRequest = context.requestScopedClient().send(breweryRequest);
return orderRequest.thenApply(orderResponse -> {
if (orderResponse.status() != Status.OK) {
return Response.forStatus(Status.INTERNAL_SERVER_ERROR);
}
// assume we get an order id as a plaintext payload
final String orderId = orderResponse.payload().get().utf8();
return Response.forPayload("your order is " + orderId);
});
}
use of com.spotify.apollo.Response 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.Response 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")));
}
use of com.spotify.apollo.Response in project apollo by spotify.
the class AsyncContextOngoingRequestTest method shouldLogWarningOnErrorWritingResponse.
// note: this test may fail when running in IntelliJ, due to
// https://youtrack.jetbrains.com/issue/IDEA-122783
@Test
public void shouldLogWarningOnErrorWritingResponse() throws Exception {
HttpServletResponse spy = spy(response);
when(asyncContext.getResponse()).thenReturn(spy);
doReturn(outputStream).when(spy).getOutputStream();
doThrow(new IOException("expected")).when(outputStream).write(any(byte[].class));
ongoingRequest.reply(Response.forPayload(ByteString.encodeUtf8("floop")));
List<LoggingEvent> events = testLogger.getLoggingEvents().stream().filter(event -> event.getLevel() == Level.WARN).filter(event -> event.getMessage().contains("Failed to write response")).collect(Collectors.toList());
assertThat(events, hasSize(1));
}
Aggregations