Search in sources :

Example 6 with RestClient

use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.

the class TestExceptionsResource2 method testNonRestException.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "exceptionHandlingModesDataProvider")
public void testNonRestException(boolean explicit, ErrorHandlingBehavior errorHandlingBehavior, RootBuilderWrapper<Long, Greeting> builders) {
    Response<Greeting> response = null;
    RestClient brokenClient = new RestClient(getDefaultTransportClient(), "http://localhost:8888/");
    try {
        final Request<Greeting> req = builders.get().id(1L).build();
        ResponseFuture<Greeting> future;
        if (explicit) {
            future = brokenClient.sendRequest(req, errorHandlingBehavior);
        } else {
            future = brokenClient.sendRequest(req);
        }
        response = future.getResponse();
        Assert.fail("expected exception");
    } catch (RemoteInvocationException e) {
        Assert.assertEquals(e.getClass(), RemoteInvocationException.class);
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) RestClient(com.linkedin.restli.client.RestClient) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) Test(org.testng.annotations.Test)

Example 7 with RestClient

use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.

the class TestResponseCompression method testAcceptEncodingConfiguration.

@Test(dataProvider = "encodingsData")
public void testAcceptEncodingConfiguration(String responseContentEncodings, String expectedAcceptEncoding, String expectedContentEncoding) throws RemoteInvocationException {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(HttpClientFactory.HTTP_RESPONSE_CONTENT_ENCODINGS, responseContentEncodings);
    properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, "true");
    Client client = newTransportClient(properties);
    Long[] ids = new Long[100];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = (long) i;
    }
    Request<BatchResponse<Greeting>> request = new GreetingsBuilders().batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding).build();
    RestClient restClient = new RestClient(client, FILTERS_URI_PREFIX);
    Response<BatchResponse<Greeting>> response = restClient.sendRequest(request).getResponse();
    Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), expectedContentEncoding);
}
Also used : HashMap(java.util.HashMap) BatchResponse(com.linkedin.restli.common.BatchResponse) RestClient(com.linkedin.restli.client.RestClient) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) Test(org.testng.annotations.Test)

Example 8 with RestClient

use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.

the class TestResponseCompression method testResponseCompression.

@Test(dataProvider = "requestData")
public void testResponseCompression(Boolean useResponseCompression, CompressionConfig responseCompressionConfig, RestliRequestOptions restliRequestOptions, int idCount, String expectedAcceptEncoding, String expectedCompressionThreshold, boolean responseShouldBeCompressed) throws RemoteInvocationException, CloneNotSupportedException {
    ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("R2 Netty Scheduler"));
    Map<String, CompressionConfig> responseCompressionConfigs = new HashMap<String, CompressionConfig>();
    if (responseCompressionConfig != null) {
        responseCompressionConfigs.put(SERVICE_NAME, responseCompressionConfig);
    }
    HttpClientFactory httpClientFactory = new HttpClientFactory(FilterChains.empty(), new NioEventLoopGroup(0, /* use default settings */
    new NamedThreadFactory("R2 Nio Event Loop")), true, executor, true, executor, false, AbstractJmxManager.NULL_JMX_MANAGER, Integer.MAX_VALUE, Collections.<String, CompressionConfig>emptyMap(), responseCompressionConfigs, true);
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(HttpClientFactory.HTTP_SERVICE_NAME, SERVICE_NAME);
    if (useResponseCompression != null) {
        properties.put(HttpClientFactory.HTTP_USE_RESPONSE_COMPRESSION, String.valueOf(useResponseCompression));
    }
    TransportClientAdapter clientAdapter1 = new TransportClientAdapter(httpClientFactory.getClient(properties));
    RestClient client = new RestClient(clientAdapter1, FILTERS_URI_PREFIX);
    Long[] ids = new Long[idCount];
    for (int i = 0; i < ids.length; i++) {
        ids[i] = (long) i;
    }
    BatchGetRequestBuilder<Long, Greeting> builder = new GreetingsBuilders(restliRequestOptions).batchGet().ids(Arrays.asList(ids)).setHeader(EXPECTED_ACCEPT_ENCODING, expectedAcceptEncoding);
    if (expectedCompressionThreshold != null) {
        builder.setHeader(EXPECTED_COMPRESSION_THRESHOLD, expectedCompressionThreshold);
    }
    Request<BatchResponse<Greeting>> request = builder.build();
    Response<BatchResponse<Greeting>> response = client.sendRequest(request).getResponse();
    if (responseShouldBeCompressed) {
        Assert.assertEquals(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED), EncodingType.GZIP.getHttpName());
    } else {
        Assert.assertNull(response.getHeader(TestCompressionServer.CONTENT_ENCODING_SAVED));
    }
}
Also used : Greeting(com.linkedin.restli.examples.greetings.api.Greeting) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) HashMap(java.util.HashMap) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) BatchResponse(com.linkedin.restli.common.BatchResponse) RestClient(com.linkedin.restli.client.RestClient) GreetingsBuilders(com.linkedin.restli.examples.greetings.client.GreetingsBuilders) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 9 with RestClient

use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.

the class RestLiExampleBasicClient method main.

/**
   * This is a stand-alone app to demo the use of client-side Pegasus API. To run in,
   * com.linkedin.restli.example.RestLiExamplesServer has to be running.
   *
   * The only argument is the path to the resource on the photo server, e.g. /photos/1
   */
public static void main(String[] args) throws Exception {
    // create HTTP Netty client with default properties
    final HttpClientFactory http = new HttpClientFactory();
    final TransportClient transportClient = http.getClient(Collections.<String, String>emptyMap());
    // create an abstraction layer over the actual client, which supports both REST and RPC
    final Client r2Client = new TransportClientAdapter(transportClient);
    // REST client wrapper that simplifies the interface
    final StringBuilder serverUrlBuilder = new StringBuilder("http://").append(SERVER_HOSTNAME).append(":").append(SERVER_PORT).append("/");
    final RestClient restClient = new RestClient(r2Client, serverUrlBuilder.toString());
    final RestLiExampleBasicClient photoClient = new RestLiExampleBasicClient(restClient);
    String pathInfo = args.length == 0 ? "" : args[0];
    photoClient.sendRequest(pathInfo, new PrintWriter(System.out));
    photoClient.shutdown();
    http.shutdown(new FutureCallback<None>());
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestClient(com.linkedin.restli.client.RestClient) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None) PrintWriter(java.io.PrintWriter)

Example 10 with RestClient

use of com.linkedin.restli.client.RestClient in project rest.li by linkedin.

the class RestLiFortunesClient method main.

/**
   * This stand-alone app demos the client-side Pegasus API.
   * To see the demo, run RestLiFortuneServer, then start the client
   */
public static void main(String[] args) throws Exception {
    // Create an HttpClient and wrap it in an abstraction layer
    final HttpClientFactory http = new HttpClientFactory();
    final Client r2Client = new TransportClientAdapter(http.getClient(Collections.<String, String>emptyMap()));
    // Create a RestClient to talk to localhost:8080
    RestClient restClient = new RestClient(r2Client, "http://localhost:8080/");
    // Generate a random ID for a fortune cookie, in the range 0-5
    long fortuneId = (long) (Math.random() * 5);
    // Construct a request for the specified fortune
    FortunesGetBuilder getBuilder = _fortuneBuilder.get();
    Request<Fortune> getReq = getBuilder.id(fortuneId).build();
    // Send the request and wait for a response
    final ResponseFuture<Fortune> getFuture = restClient.sendRequest(getReq);
    final Response<Fortune> resp = getFuture.getResponse();
    // Print the response
    System.out.println(resp.getEntity().getFortune());
    // shutdown
    restClient.shutdown(new FutureCallback<None>());
    http.shutdown(new FutureCallback<None>());
}
Also used : RestClient(com.linkedin.restli.client.RestClient) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RestClient(com.linkedin.restli.client.RestClient) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None)

Aggregations

RestClient (com.linkedin.restli.client.RestClient)10 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)5 HashMap (java.util.HashMap)5 None (com.linkedin.common.util.None)4 Client (com.linkedin.r2.transport.common.Client)4 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)4 Greeting (com.linkedin.restli.examples.greetings.api.Greeting)4 Test (org.testng.annotations.Test)4 GreetingsBuilders (com.linkedin.restli.examples.greetings.client.GreetingsBuilders)3 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)2 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)2 NamedThreadFactory (com.linkedin.r2.util.NamedThreadFactory)2 BatchResponse (com.linkedin.restli.common.BatchResponse)2 GreetingsRequestBuilders (com.linkedin.restli.examples.greetings.client.GreetingsRequestBuilders)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 PrintWriter (java.io.PrintWriter)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 DataProvider (org.testng.annotations.DataProvider)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 D2Client (com.linkedin.d2.balancer.D2Client)1