Search in sources :

Example 1 with Client

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

the class EnvironmentModule method environmentFactory.

@Provides
@Singleton
EnvironmentFactory environmentFactory(Config configNode, ApolloConfig apolloConfig, Closer closer, Injector injector, IncomingRequestAwareClient incomingRequestAwareClient) {
    final String backend = apolloConfig.backend();
    final Client unawareClient = incomingRequestAwareClient.asUnawareClient();
    return EnvironmentFactoryBuilder.newBuilder(backend, unawareClient, closer, injector::getInstance).withStaticConfig(configNode).build();
}
Also used : Client(com.spotify.apollo.Client) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 2 with Client

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

the class ClientDecoratorTest method shouldBindDecoratedClient.

@Test
public void shouldBindDecoratedClient() throws Exception {
    try (Service.Instance i = service.build().start()) {
        Client client = ApolloEnvironmentModule.environment(i).environment().client();
        Request request = Request.forUri("http://example.com/");
        Request withService = request.withService("ping");
        client.send(request);
        verify(mockClient).send(eq(withService), eq(empty()));
    } catch (IOException e) {
        fail();
    }
}
Also used : Request(com.spotify.apollo.Request) Service(com.spotify.apollo.core.Service) IOException(java.io.IOException) Client(com.spotify.apollo.Client) Test(org.junit.Test)

Example 3 with Client

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

Example 4 with Client

use of com.spotify.apollo.Client 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 5 with Client

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

the class RequestHandlerImpl method handleEndpointMatch.

/**
 * Continuation for the {@link RequestRunnableFactory}
 *
 * @param request  request being processed
 * @param match    the match that was made
 */
private void handleEndpointMatch(OngoingRequest request, RuleMatch<Endpoint> match) {
    final Endpoint endpoint = match.getRule().getTarget();
    final Map<String, String> parsedPathArguments = match.parsedPathArguments();
    final Client requestScopedClient = client.wrapRequest(request.request());
    final RequestContext requestContext = RequestContexts.create(request.request(), requestScopedClient, parsedPathArguments, request.arrivalTimeNanos(), request.metadata());
    erf.create(request, requestContext, endpoint).run();
}
Also used : Endpoint(com.spotify.apollo.dispatch.Endpoint) RequestContext(com.spotify.apollo.RequestContext) IncomingRequestAwareClient(com.spotify.apollo.environment.IncomingRequestAwareClient) Client(com.spotify.apollo.Client)

Aggregations

Client (com.spotify.apollo.Client)5 Request (com.spotify.apollo.Request)3 RequestContext (com.spotify.apollo.RequestContext)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 Response (com.spotify.apollo.Response)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 ByteString (okio.ByteString)2 Provides (com.google.inject.Provides)1 Status (com.spotify.apollo.Status)1 Service (com.spotify.apollo.core.Service)1