public static <R> RestResponse<R> of(final R response) { return successful(response); }
private <T> RestResponse<T> getRequest(final String path, final Class<T> type) { try (Response response = client.target(serverAddress) .path(path) .request(MediaType.APPLICATION_JSON_TYPE) .get()) { return response.getStatus() == Response.Status.OK.getStatusCode() ? RestResponse.successful(response.readEntity(type)) : createErrorResponse(path, response); } catch (final Exception e) { throw new KsqlRestClientException("Error issuing GET to KSQL server. path:" + path, e); } }
@Test public void shouldOutputNewServerDetails() { expect(restClient.makeRootRequest()).andReturn(RestResponse.successful(SERVER_INFO)); replay(restClient); command.execute(ImmutableList.of(VALID_SERVER_ADDRESS), terminal); assertThat(out.toString(), containsString("Server now: " + VALID_SERVER_ADDRESS)); }
private <T> RestResponse<T> postRequest( final String path, final Object jsonEntity, final Optional<Integer> readTimeoutMs, final boolean closeResponse, final Function<Response, T> mapper) { Response response = null; try { final WebTarget target = client.target(serverAddress) .path(path); readTimeoutMs.ifPresent(timeout -> target.property(ClientProperties.READ_TIMEOUT, timeout)); response = target .request(MediaType.APPLICATION_JSON_TYPE) .post(Entity.json(jsonEntity)); return response.getStatus() == Response.Status.OK.getStatusCode() ? RestResponse.successful(mapper.apply(response)) : createErrorResponse(path, response); } catch (final ProcessingException e) { if (shouldRetry(readTimeoutMs, e)) { return postRequest(path, jsonEntity, calcReadTimeout(readTimeoutMs), closeResponse, mapper); } throw new KsqlRestClientException("Error issuing POST to KSQL server. path:" + path, e); } catch (final Exception e) { throw new KsqlRestClientException("Error issuing POST to KSQL server. path:" + path, e); } finally { if (response != null && closeResponse) { response.close(); } } }
@Test public void shouldPrintServerAddressWhenEmptyStringArg() throws Exception { expect(restClient.makeRootRequest()).andReturn(RestResponse.successful(SERVER_INFO)); expect(restClient.getServerAddress()).andReturn(new URI(INITIAL_SERVER_ADDRESS)); restClient.setServerAddress(anyString()); expectLastCall().andThrow(new AssertionError("should not set address")); replay(restClient); command.execute(ImmutableList.of(), terminal); assertThat(out.toString(), equalTo(INITIAL_SERVER_ADDRESS + "\n")); }
@Test public void shouldRestClientServerAddressWhenNonEmptyStringArg() { expect(restClient.makeRootRequest()).andReturn(RestResponse.successful(SERVER_INFO)); restClient.setServerAddress(VALID_SERVER_ADDRESS); expectLastCall(); replay(restClient); command.execute(ImmutableList.of(VALID_SERVER_ADDRESS), terminal); verify(restClient); }