I recently had a need to write some code for the Kaltura API and the libraries have had a lot of changes since the last time I used them. One thing I found curious was (at least for Java) all API calls are queued making the operation asynchronous. It’s interesting to me that other languages don’t default to this. This may be desirable in some scenarios; however, if you are wanting to take any conditional action, you iI recently had a need to write some code for the Kaltura API, and the libraries have had a lot of changes since the last time I used them. One thing I found curious was that (at least for Java), all API calls are queued, making the operations all asynchronous. It’s interesting to me that other languages don’t default to this. This may be desirable in some scenarios; however, if you are wanting to take any conditional action, you ideally want to wait for the operation to complete. For example, if you want to iterate through the categories and then, depending on the data, take other actions to generate a report, you’re forced to store all the content in memory before processing it. Perhaps you’d prefer to generate your API session ID by logging in rather than using your admin secret, which is highly recommended. The C# examples show all the code using the execute() method with an onComplete method that sets a boolean. The following line after the call does a loop on the boolean with 100 ms of sleep. I’m not sure if the C# API has a queue method; if it does, it seems the developer portal-generated code is wrong and the polling is not required. While I couldn’t find any examples of serial execution of requests in Java, the executor class does have an “execute” method. You just need to massage the code generated by the API tool a bit. It does block until API calls are complete and then returns.
Here is an example of login:
LoginByLoginIdUserBuilder requestBuilder = UserService.loginByLoginId(loginId, password, partnerId, expiry, privileges, otp)
.setCompletion(new OnCompletion<Response<String>>() {
@Override
public void onComplete(Response<String> result) {
System.out.println(result);
}
});
APIOkRequestsExecutor.getExecutor().queue(requestBuilder.build(client));
// proceed to other API interactions
Obviously you need addition controls here (like C# samples) to know when the request is actually complete prior to doing other API actions. You can rewrite this call to block on completion. This simplifies your code if you have nested logic that iterates through a list of items (each which generate an API call).
Generated Sample Code
LoginByLoginIdUserBuilder requestBuilder =
UserService.loginByLoginId(loginId, password, partnerId, expiry, privileges, otp)
.setCompletion(new OnCompletion<Response<String>>() {
@Override
public void onComplete(Response<String> result) {
System.out.println(result);
}
});
APIOkRequestsExecutor.getExecutor().queue(requestBuilder.build(client));
My Revised Code
LoginByLoginIdUserBuilder userRequestBuilder = UserService.loginByLoginId(loginId, password, partnerId, 86400, "","");
Response <String> userResult = (Response <String>) APIOkRequestsExecutor.getExecutor().execute(userRequestBuilder.build(client));
String ks = userResult.results.toString();
I think the usage pattern is a lot easier, and the code is much more straightforward.
Unfortunately, it seems the code examples, most of the client API, and documentation for the Kaltura developer portal are all code-generated. This allows them to spend less time supporting them, but honestly, I think using the REST endpoints is easier due to the lack of human-readable documents and proper code examples that are tested.