use of com.spotify.apollo.Response in project apollo by spotify.
the class StubClientTest method shouldReturnIterativeRepliesWithVaryingStatusCodes.
@Test
public void shouldReturnIterativeRepliesWithVaryingStatusCodes() throws Exception {
ResponseWithDelay response1 = ResponseWithDelay.forResponse(Response.of(Status.ACCEPTED, encodeUtf8("first response")));
ResponseWithDelay response2 = ResponseWithDelay.forResponse(Response.of(Status.FORBIDDEN, encodeUtf8("second response")));
List<ResponseWithDelay> responses = Arrays.asList(response1, response2);
ResponseSource responseSequence = Responses.sequence(responses);
stubClient.respond(responseSequence).to("http://ping");
CompletionStage<Response<ByteString>> reply1 = getResponseFromPing();
CompletionStage<Response<ByteString>> reply2 = getResponseFromPing();
Response<ByteString> message1 = reply1.toCompletableFuture().get();
Response<ByteString> message2 = reply2.toCompletableFuture().get();
assertThat(message1.status(), is(Status.ACCEPTED));
assertThat(message2.status(), is(Status.FORBIDDEN));
assertThat(message1.payload().get().utf8(), is("first response"));
assertThat(message2.payload().get().utf8(), is("second response"));
}
use of com.spotify.apollo.Response in project apollo by spotify.
the class RouteTransformMetricsExampleTest method responsePayloadSizeHistogram.
/**
* Middleware to track response payload size in a Histogram,
* tagged with an endpoint tag set to the given endpoint name.
*/
public Middleware<AsyncHandler<Response<ByteString>>, AsyncHandler<Response<ByteString>>> responsePayloadSizeHistogram(String endpointName) {
final MetricId histogramId = MetricId.build().tagged("service", serviceName).tagged("endpoint", endpointName).tagged("what", "endpoint-response-size");
final Histogram histogram = registry.histogram(histogramId);
return (inner) -> (requestContext) -> inner.invoke(requestContext).whenComplete((response, t) -> {
if (response != null) {
histogram.update(response.payload().map(ByteString::size).orElse(0));
}
});
}
use of com.spotify.apollo.Response in project apollo by spotify.
the class FallbackClient method send.
@Override
public CompletionStage<Response<ByteString>> send(Request request, Optional<Request> incoming) {
final CompletionStage<Response<ByteString>> send = mainClient.send(request, incoming);
return fallbackCompose(send, t -> {
if (NoMatchingResponseFoundException.class.isAssignableFrom(t.getClass())) {
NoMatchingResponseFoundException e = (NoMatchingResponseFoundException) t;
LOG.debug("Falling back to real client: {}", e.getMessage());
return fallbackClient.send(request, incoming);
} else {
return send;
}
});
}
use of com.spotify.apollo.Response in project apollo by spotify.
the class MiddlewaresTest method httpShouldNotSetContentLengthOrAppendPayloadForInvalidStatusCodes.
@Test
public void httpShouldNotSetContentLengthOrAppendPayloadForInvalidStatusCodes() throws Exception {
List<StatusType> invalid = ImmutableList.of(Status.createForCode(100), NO_CONTENT, NOT_MODIFIED);
for (StatusType status : invalid) {
CompletableFuture<Response<ByteString>> future = new CompletableFuture<>();
delegate = requestContext -> future;
future.complete(Response.of(status, ByteString.of((byte) 14, (byte) 19)));
Response<ByteString> result = getResult(Middlewares.httpPayloadSemantics(delegate));
Optional<String> header = result.headers().get("Content-Length");
assertThat("no content-length for " + status, header, is(Optional.empty()));
assertThat("no payload for " + status, result.payload(), is(Optional.<ByteString>empty()));
}
}
use of com.spotify.apollo.Response in project zoltar by spotify.
the class IrisPrediction method predict.
/**
* Prediction endpoint. Takes a request in a from of a String containing iris features `-`
* separated, and returns a response in a form of a predicted iris class.
*/
public static Response<String> predict(final String requestFeatures) {
if (requestFeatures == null) {
return Response.forStatus(Status.BAD_REQUEST);
}
final String[] features = requestFeatures.split("-");
if (features.length != 4) {
return Response.forStatus(Status.BAD_REQUEST);
}
final Iris featureData = new Iris(Option.apply(Double.parseDouble(features[0])), Option.apply(Double.parseDouble(features[1])), Option.apply(Double.parseDouble(features[2])), Option.apply(Double.parseDouble(features[3])), Option.empty());
int[] predictions = new int[0];
try {
predictions = predictor.predict(featureData).thenApply(p -> p.stream().mapToInt(prediction -> prediction.value().intValue()).toArray()).toCompletableFuture().get();
} catch (final Exception e) {
e.printStackTrace();
// TODO: what to return in case of failure here?
}
final String predictedClass = idToClass.get(predictions[0]);
return Response.forPayload(predictedClass);
}
Aggregations