Search in sources :

Example 16 with Request

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

the class HttpClientTest method testSend.

@Test
public void testSend() throws Exception {
    mockServerClient.when(request().withMethod("GET").withPath("/foo.php").withQueryStringParameter("bar", "baz").withQueryStringParameter("qur", "quz")).respond(response().withStatusCode(204));
    String uri = format("http://localhost:%d/foo.php?bar=baz&qur=quz", mockServerRule.getHttpPort());
    Request request = Request.forUri(uri, "GET");
    Response<ByteString> response = HttpClient.createUnconfigured().send(request, empty()).toCompletableFuture().get();
    assertThat(response.status(), withCode(204));
    assertThat(response.payload(), is(empty()));
}
Also used : ByteString(okio.ByteString) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Test(org.junit.Test)

Example 17 with Request

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

the class HttpClientTest method testNoDuplicatedAuthContext.

@Test
public void testNoDuplicatedAuthContext() 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").withHeader("Authorization", "Basic dXNlcjpwYXNz");
    Request originalRequest = Request.forUri("http://original.uri/").withHeader("Authorization", "Basic foobar");
    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 18 with Request

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

the class HttpClientTest method testSendWithBody.

@Test
public void testSendWithBody() throws Exception {
    mockServerClient.when(request().withMethod("POST").withPath("/foo.php").withQueryStringParameter("bar", "baz").withQueryStringParameter("qur", "quz").withHeader("Content-Type", "application/x-spotify-greeting").withBody("hello")).respond(response().withStatusCode(200).withHeader("Content-Type", "application/x-spotify-location").withHeader("Vary", "Content-Type").withHeader("Vary", "Accept").withBody("world"));
    String uri = format("http://localhost:%d/foo.php?bar=baz&qur=quz", mockServerRule.getHttpPort());
    Request request = Request.forUri(uri, "POST").withHeader("Content-Type", "application/x-spotify-greeting").withPayload(ByteString.encodeUtf8("hello"));
    Response<ByteString> response = HttpClient.createUnconfigured().send(request, empty()).toCompletableFuture().get();
    assertThat(response.status(), withCode(200));
    assertThat(response.headers().asMap(), allOf(hasEntry("content-type", "application/x-spotify-location"), hasEntry("vary", "Content-Type, Accept")));
    assertThat(response.payload(), is(Optional.of(ByteString.encodeUtf8("world"))));
}
Also used : ByteString(okio.ByteString) Request(com.spotify.apollo.Request) ByteString(okio.ByteString) Test(org.junit.Test)

Example 19 with Request

use of com.spotify.apollo.Request 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);
    });
}
Also used : Response(com.spotify.apollo.Response) Request(com.spotify.apollo.Request) ByteString(okio.ByteString)

Example 20 with Request

use of com.spotify.apollo.Request 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)

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