Skip to content

Instantly share code, notes, and snippets.

@ldclakmal
Last active January 17, 2020 09:56
Show Gist options
  • Save ldclakmal/1d9d61818888e4db84068a908b71051d to your computer and use it in GitHub Desktop.
Save ldclakmal/1d9d61818888e4db84068a908b71051d to your computer and use it in GitHub Desktop.
Ballerina client for HTTP/2 server push
import ballerina/http;
import ballerina/log;
http:Client clientEP = new("http://localhost:9095", config = { httpVersion: "2.0" });
public function main() {
// Submit a `GET` request.
http:Request serviceReq = new;
http:HttpFuture httpFuture = checkpanic clientEP->submit("GET", "/hello/sayHello", serviceReq);
// Get the requested resource response.
http:Response response = checkpanic clientEP->getResponse(httpFuture);
json responsePayload = checkpanic response.getJsonPayload();
log:printInfo("Response : " + responsePayload.toString());
// Check if promises exists.
boolean hasPromise = clientEP->hasPromise(httpFuture);
// Get the response for the promises.
while (hasPromise) {
http:PushPromise pushPromise = checkpanic clientEP->getNextPromise(httpFuture);
log:printInfo("Received a promise for " + pushPromise.path);
// Fetch required promise responses.
http:Response promisedResponse = checkpanic clientEP->getPromisedResponse(pushPromise);
json promisedPayload = checkpanic promisedResponse.getJsonPayload();
log:printInfo("Promised resource : " + promisedPayload.toString());
// Check if more promises exists.
hasPromise = clientEP->hasPromise(httpFuture);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment