use of com.spotify.apollo.Request 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.Request in project apollo by spotify.
the class ServiceHelperTest method testCallServiceClient.
@Test
public void testCallServiceClient() throws Exception {
stubClient.respond(Response.forPayload(encodeUtf8("hello from something"))).to("http://something/foo");
Request request = Request.forUri("http://" + SERVICE_NAME + "/call/something/foo");
Response<ByteString> response = serviceHelper.serviceClient().send(request).toCompletableFuture().get();
assertThat(response.payload().get().utf8(), is("hello from something"));
}
use of com.spotify.apollo.Request in project apollo by spotify.
the class ApolloRequestHandler method asApolloRequest.
@VisibleForTesting
Request asApolloRequest(HttpServletRequest req) throws IOException {
final String uri = req.getRequestURI() + (req.getQueryString() == null ? "" : "?" + req.getQueryString());
final String method = req.getMethod();
final int contentLength = req.getContentLength();
final Optional<ByteString> payload = (contentLength > -1) ? of(readPayload(req, contentLength)) : empty();
final ImmutableMap.Builder<String, String> headersBuilder = ImmutableMap.builder();
toStream(req.getHeaderNames()).forEachOrdered(name -> headersBuilder.put(name, toStream(req.getHeaders(name)).collect(Collectors.joining(","))));
final ImmutableMap<String, String> headers = headersBuilder.build();
Request result = Request.forUri(uri, method).withHeaders(headers);
final String callingService = headers.get("X-Calling-Service");
if (!isNullOrEmpty(callingService)) {
result = result.withService(callingService);
}
if (payload.isPresent()) {
result = result.withPayload(payload.get());
}
return result;
}
use of com.spotify.apollo.Request in project apollo by spotify.
the class HttpClientTest method testAuthContextPropagation.
@Test
public void testAuthContextPropagation() throws Exception {
mockServerClient.when(request().withHeader("Authorization", "Basic dXNlcjpwYXNz")).respond(response().withHeader("x-auth-was-fine", "yes"));
String uri = format("http://localhost:%d/foo.php", mockServerRule.getHttpPort());
Request request = Request.forUri(uri, "GET");
Request originalRequest = Request.forUri("http://original.uri/").withHeader("Authorization", "Basic dXNlcjpwYXNz");
final Response<ByteString> response = HttpClient.createUnconfigured().send(request, Optional.of(originalRequest)).toCompletableFuture().get();
assertThat(response.headers().get("x-auth-was-fine"), equalTo(Optional.of("yes")));
}
use of com.spotify.apollo.Request in project apollo by spotify.
the class HttpClientTest method testTimeout.
@Test
public void testTimeout() throws Exception {
mockServerClient.when(request().withMethod("GET").withPath("/foo.php")).callback(callback().withCallbackClass(SleepCallback.class.getCanonicalName()));
String uri = format("http://localhost:%d/foo.php", mockServerRule.getHttpPort());
Request request = Request.forUri(uri, "GET");
Response<ByteString> response = HttpClient.createUnconfigured().send(request, empty()).toCompletableFuture().get();
assertThat(response.status(), withCode(200));
}
Aggregations