Search in sources :

Example 21 with Request

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

Example 22 with Request

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"));
}
Also used : ByteString(okio.ByteString) Request(com.spotify.apollo.Request) Test(org.junit.Test)

Example 23 with Request

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;
}
Also used : ByteString(okio.ByteString) Request(com.spotify.apollo.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteString(okio.ByteString) ImmutableMap(com.google.common.collect.ImmutableMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 24 with Request

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")));
}
Also used : ByteString(okio.ByteString) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Test(org.junit.Test)

Example 25 with Request

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));
}
Also used : ByteString(okio.ByteString) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Test(org.junit.Test)

Aggregations

Request (com.spotify.apollo.Request)33 Test (org.junit.Test)21 ByteString (okio.ByteString)14 Client (com.spotify.apollo.Client)3 Response (com.spotify.apollo.Response)3 IOException (java.io.IOException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)2 RequestContext (com.spotify.apollo.RequestContext)2 Album (com.spotify.apollo.example.data.Album)2 Artist (com.spotify.apollo.example.data.Artist)2 AsyncHandler (com.spotify.apollo.route.AsyncHandler)2 JsonSerializerMiddlewares (com.spotify.apollo.route.JsonSerializerMiddlewares)2 Route (com.spotify.apollo.route.Route)2 ArrayList (java.util.ArrayList)2 CompletionStage (java.util.concurrent.CompletionStage)2 Stream (java.util.stream.Stream)2 Before (org.junit.Before)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1