REGISTER NOW: Supercharge Your Cloud Integrations and Watch Our Webinar Recording

Kloudless Blog

Tutorials, case studies and how-tos from our experts

Getting Started with Java SDK and Kloudless

Here at Kloudless, the developer experience is always our first priority, and we’re happy to say we have some good news for developers, especially those using Java. Today, we are glad to announce the release of our Kloudless Java SDK.

What’s New

This new version of our Kloudless Java SDK provides an intuitive and common way to allow developers to access any resources from the Kloudless API server.

The new SDK includes:

  • An account based HTTP library.
  • A Generic Response Object providing more flexible ways of data access.
  • Useful helper methods to handle the OAuth process.

The Kloudless Java SDK is compatible with Java 8 and higher version.

Getting Started

If you use Maven as your package management, please add this dependency to your project’s POM file:

If you use Gradle, please add this to project’s build file:

The Basics

Then, simply input your Bearer Token to initialize your account:

An Account object will give you access to any resources in the Kloudless Server. The Account tool is based on Apache Http Components and subsequently will support the basic abilities of HTTP access. Moreover, you don’t need to assemble an HTTP header each time, as the Account object will do it for you.

Obtaining a Bearer Token

API requests to Kloudless require Bearer Token Authentication. This SDK provides a programmatic way for the developer to implement the 3-Legged OAuth Flow with the help of a web application. Here, we use the Spark web framework as an example to show how to obtain a bearer token.

The first leg is to generate an authorization url and guide users to this url.

You will need to take the response from the 2nd leg after the user is redirected and call this helper method in the Java SDK to obtain an access token.

Actions

The Account supports basic HTTP methods, such as GET, POST, PATCH, PUT, and DELETE.

Here is an example showing how to use the Account object to add a new Calendars object, then revise and delete it:

Generic Response Class

You may notice you obtain access to a Resource object when using an Account object to access Kloudless’ Server in the previous example. Essentially, Account generates four types of response classes: Resource, ResourceList, ResponseRaw, and ResponseJson, according to different types of data from Kloudless server. We will show you how to use them and what features these classes provide.

The Resource object is the basic unit of data object from the Kloudless server. It is what stores the JsonObject. This is where you can locate some basic attributes such as id and url from the Resource object. If you would like to get other attributes, you can directly access the JsonObject itself. Please refer to the example below:

The ResourceList object is a collection of Resource objects. It is generated when your HTTP operation retrieves multiple objects from the Kloudless server. Below is an example:

Moreover, ResourceList is not only a collection of Resource objects, but it also supports additional abilities to allow you to get page information. It also provides two methods, hasNextPage() and getNextPage(), to get more objects:

ResourceList also provides an auto-pagination ability to help you access all items and the Iteratorcan automatically handle cross page retrieval:

However, not all of the object types from the Kloudless Server are JSON objects. Binary data from a storage service is one example of this. As a result, the third type of Response is ResponseRaw and the pure HTTP response object is stored in the ResponseRaw object instead of JsonObject. Here is an example to illustrate the difference:

ResponseJson is the fourth type of Response object. It is generated when the object is a JsonObject but contains no Object Id:

Actions of Generic Response Object

All types of Response objects also have access to HTTP verb abilities through built-in methods. An example of this would be for a Resource object to update or delete itself. Please check the below example:

You can update the file content via the PUT method of the Resource object:

More so, it provides users the ability to retrieve data through the built-in GET method. Here we see an example of how to use it:

More Information

For more information that includes detailed API document and release information, please check out the Kloudless Github.

Published By

Chih-Jen Wu