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.










share|improve this question






















  • 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















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.










share|improve this question






















  • 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













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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).;





share|improve this answer





















  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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).;





share|improve this answer





















  • 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















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).;





share|improve this answer





















  • 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













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).;





share|improve this answer












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).;






share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma