Call multiple API using akka HTTP
up vote
0
down vote
favorite
I am new to AKKA , I am trying to fire away the 3 requests, set a timeout to ~1 second for each to complete, aggregate the results. The 3 requests or multiple requests will be simple API call , GET, where the response from the API will be in JSON. So far the code I have
final ActorSystem system = ActorSystem.create();
final ActorMaterializer materializer = ActorMaterializer.create(system);
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connectionFlow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connectionFlow)
.runWith(Sink.<HttpResponse>head(), materializer).;
Any help regarding this will be highly appreciated.
java akka-http
add a comment |
up vote
0
down vote
favorite
I am new to AKKA , I am trying to fire away the 3 requests, set a timeout to ~1 second for each to complete, aggregate the results. The 3 requests or multiple requests will be simple API call , GET, where the response from the API will be in JSON. So far the code I have
final ActorSystem system = ActorSystem.create();
final ActorMaterializer materializer = ActorMaterializer.create(system);
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connectionFlow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connectionFlow)
.runWith(Sink.<HttpResponse>head(), materializer).;
Any help regarding this will be highly appreciated.
java akka-http
are the 3 requests going to different hosts or the same host?
– Ramon J Romero y Vigil
Nov 11 at 13:35
different hosts
– Ranveer Bedaysee
Nov 12 at 10:21
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am new to AKKA , I am trying to fire away the 3 requests, set a timeout to ~1 second for each to complete, aggregate the results. The 3 requests or multiple requests will be simple API call , GET, where the response from the API will be in JSON. So far the code I have
final ActorSystem system = ActorSystem.create();
final ActorMaterializer materializer = ActorMaterializer.create(system);
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connectionFlow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connectionFlow)
.runWith(Sink.<HttpResponse>head(), materializer).;
Any help regarding this will be highly appreciated.
java akka-http
I am new to AKKA , I am trying to fire away the 3 requests, set a timeout to ~1 second for each to complete, aggregate the results. The 3 requests or multiple requests will be simple API call , GET, where the response from the API will be in JSON. So far the code I have
final ActorSystem system = ActorSystem.create();
final ActorMaterializer materializer = ActorMaterializer.create(system);
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connectionFlow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connectionFlow)
.runWith(Sink.<HttpResponse>head(), materializer).;
Any help regarding this will be highly appreciated.
java akka-http
java akka-http
asked Nov 10 at 15:02
Ranveer Bedaysee
418
418
are the 3 requests going to different hosts or the same host?
– Ramon J Romero y Vigil
Nov 11 at 13:35
different hosts
– Ranveer Bedaysee
Nov 12 at 10:21
add a comment |
are the 3 requests going to different hosts or the same host?
– Ramon J Romero y Vigil
Nov 11 at 13:35
different hosts
– Ranveer Bedaysee
Nov 12 at 10:21
are the 3 requests going to different hosts or the same host?
– Ramon J Romero y Vigil
Nov 11 at 13:35
are the 3 requests going to different hosts or the same host?
– Ramon J Romero y Vigil
Nov 11 at 13:35
different hosts
– Ranveer Bedaysee
Nov 12 at 10:21
different hosts
– Ranveer Bedaysee
Nov 12 at 10:21
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I am not too familiar with the java
version of akka
so the below example code may not be compile-able but it demonstrates the general idea...
Presumably you have functions for converting the response from previous API calls into requests for the next API:
public static HttpRequest convertAPI1Response(HttpResponse httpResponse) {
}
public static HttpRequest convertAPI2Response(HttpResponse httpResponse) {
}
These converters can then be placed inside Flow
values:
final Flow<HttpResponse, HttpRequest, ...> convertAPI1Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
final Flow<HttpResponse, HttpRequest, ...> convertAPI2Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
These Flows can now be used in concert with the outgoingConnection
flows that the question already references:
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection1Flow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection2Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherHost.com/api", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection3Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherOtherHost.com/api", 80));
And finally, all of the Flows can be connected together:
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connection1Flow)
.via(convertAPI1Flow)
.via(connection2Flow)
.via(convertAPI2Flow)
.via(connection3Flow)
.runWith(Sink.<HttpResponse>head(), materializer).;
thank you very much,Sink.<HttpResponse>head() , gets the headers only i suppose , how to get the body of the response?
– Ranveer Bedaysee
Nov 12 at 13:39
@RanveerBedaysee You are welcome. The body is calledentity
and is represented by a stream also: doc.akka.io/docs/akka-http/current/common/…
– Ramon J Romero y Vigil
Nov 12 at 15:18
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I am not too familiar with the java
version of akka
so the below example code may not be compile-able but it demonstrates the general idea...
Presumably you have functions for converting the response from previous API calls into requests for the next API:
public static HttpRequest convertAPI1Response(HttpResponse httpResponse) {
}
public static HttpRequest convertAPI2Response(HttpResponse httpResponse) {
}
These converters can then be placed inside Flow
values:
final Flow<HttpResponse, HttpRequest, ...> convertAPI1Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
final Flow<HttpResponse, HttpRequest, ...> convertAPI2Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
These Flows can now be used in concert with the outgoingConnection
flows that the question already references:
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection1Flow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection2Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherHost.com/api", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection3Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherOtherHost.com/api", 80));
And finally, all of the Flows can be connected together:
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connection1Flow)
.via(convertAPI1Flow)
.via(connection2Flow)
.via(convertAPI2Flow)
.via(connection3Flow)
.runWith(Sink.<HttpResponse>head(), materializer).;
thank you very much,Sink.<HttpResponse>head() , gets the headers only i suppose , how to get the body of the response?
– Ranveer Bedaysee
Nov 12 at 13:39
@RanveerBedaysee You are welcome. The body is calledentity
and is represented by a stream also: doc.akka.io/docs/akka-http/current/common/…
– Ramon J Romero y Vigil
Nov 12 at 15:18
add a comment |
up vote
0
down vote
I am not too familiar with the java
version of akka
so the below example code may not be compile-able but it demonstrates the general idea...
Presumably you have functions for converting the response from previous API calls into requests for the next API:
public static HttpRequest convertAPI1Response(HttpResponse httpResponse) {
}
public static HttpRequest convertAPI2Response(HttpResponse httpResponse) {
}
These converters can then be placed inside Flow
values:
final Flow<HttpResponse, HttpRequest, ...> convertAPI1Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
final Flow<HttpResponse, HttpRequest, ...> convertAPI2Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
These Flows can now be used in concert with the outgoingConnection
flows that the question already references:
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection1Flow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection2Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherHost.com/api", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection3Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherOtherHost.com/api", 80));
And finally, all of the Flows can be connected together:
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connection1Flow)
.via(convertAPI1Flow)
.via(connection2Flow)
.via(convertAPI2Flow)
.via(connection3Flow)
.runWith(Sink.<HttpResponse>head(), materializer).;
thank you very much,Sink.<HttpResponse>head() , gets the headers only i suppose , how to get the body of the response?
– Ranveer Bedaysee
Nov 12 at 13:39
@RanveerBedaysee You are welcome. The body is calledentity
and is represented by a stream also: doc.akka.io/docs/akka-http/current/common/…
– Ramon J Romero y Vigil
Nov 12 at 15:18
add a comment |
up vote
0
down vote
up vote
0
down vote
I am not too familiar with the java
version of akka
so the below example code may not be compile-able but it demonstrates the general idea...
Presumably you have functions for converting the response from previous API calls into requests for the next API:
public static HttpRequest convertAPI1Response(HttpResponse httpResponse) {
}
public static HttpRequest convertAPI2Response(HttpResponse httpResponse) {
}
These converters can then be placed inside Flow
values:
final Flow<HttpResponse, HttpRequest, ...> convertAPI1Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
final Flow<HttpResponse, HttpRequest, ...> convertAPI2Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
These Flows can now be used in concert with the outgoingConnection
flows that the question already references:
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection1Flow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection2Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherHost.com/api", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection3Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherOtherHost.com/api", 80));
And finally, all of the Flows can be connected together:
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connection1Flow)
.via(convertAPI1Flow)
.via(connection2Flow)
.via(convertAPI2Flow)
.via(connection3Flow)
.runWith(Sink.<HttpResponse>head(), materializer).;
I am not too familiar with the java
version of akka
so the below example code may not be compile-able but it demonstrates the general idea...
Presumably you have functions for converting the response from previous API calls into requests for the next API:
public static HttpRequest convertAPI1Response(HttpResponse httpResponse) {
}
public static HttpRequest convertAPI2Response(HttpResponse httpResponse) {
}
These converters can then be placed inside Flow
values:
final Flow<HttpResponse, HttpRequest, ...> convertAPI1Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
final Flow<HttpResponse, HttpRequest, ...> convertAPI2Flow =
Flow.of(HttpResponse.class).map(convertAPI1Response)
These Flows can now be used in concert with the outgoingConnection
flows that the question already references:
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection1Flow =
Http.get(system).outgoingConnection(toHost("https://example/api/ticker", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection2Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherHost.com/api", 80));
final Flow<HttpRequest, HttpResponse, CompletionStage<OutgoingConnection>> connection3Flow =
Http.get(system).outgoingConnection(toHost("https://someOtherOtherHost.com/api", 80));
And finally, all of the Flows can be connected together:
final CompletionStage<HttpResponse> responseFuture =
Source.single(HttpRequest.create("/"))
.via(connection1Flow)
.via(convertAPI1Flow)
.via(connection2Flow)
.via(convertAPI2Flow)
.via(connection3Flow)
.runWith(Sink.<HttpResponse>head(), materializer).;
answered Nov 12 at 13:33
Ramon J Romero y Vigil
11.4k23970
11.4k23970
thank you very much,Sink.<HttpResponse>head() , gets the headers only i suppose , how to get the body of the response?
– Ranveer Bedaysee
Nov 12 at 13:39
@RanveerBedaysee You are welcome. The body is calledentity
and is represented by a stream also: doc.akka.io/docs/akka-http/current/common/…
– Ramon J Romero y Vigil
Nov 12 at 15:18
add a comment |
thank you very much,Sink.<HttpResponse>head() , gets the headers only i suppose , how to get the body of the response?
– Ranveer Bedaysee
Nov 12 at 13:39
@RanveerBedaysee You are welcome. The body is calledentity
and is represented by a stream also: doc.akka.io/docs/akka-http/current/common/…
– Ramon J Romero y Vigil
Nov 12 at 15:18
thank you very much,Sink.<HttpResponse>head() , gets the headers only i suppose , how to get the body of the response?
– Ranveer Bedaysee
Nov 12 at 13:39
thank you very much,Sink.<HttpResponse>head() , gets the headers only i suppose , how to get the body of the response?
– Ranveer Bedaysee
Nov 12 at 13:39
@RanveerBedaysee You are welcome. The body is called
entity
and is represented by a stream also: doc.akka.io/docs/akka-http/current/common/…– Ramon J Romero y Vigil
Nov 12 at 15:18
@RanveerBedaysee You are welcome. The body is called
entity
and is represented by a stream also: doc.akka.io/docs/akka-http/current/common/…– Ramon J Romero y Vigil
Nov 12 at 15:18
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240218%2fcall-multiple-api-using-akka-http%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
are the 3 requests going to different hosts or the same host?
– Ramon J Romero y Vigil
Nov 11 at 13:35
different hosts
– Ranveer Bedaysee
Nov 12 at 10:21