You can sign up for a Kloudless Developer account at https://kloudless.com.
- Requirements
- Installation
- Getting Started
- Build
Java 1.8 and later.
Add this dependency to your project's POM:
<dependency>
<groupId>com.kloudless</groupId>
<artifactId>kloudless-java</artifactId>
<version>2.0.1</version>
</dependency>
Add this dependency to your project's build file:
api 'com.kloudless:kloudless-java:2.0.1'
You'll need to manually install the Kloudless JAR, either by compiling it or downloading it from the Releases page
API requests to Kloudless require Bearer Token Authentication. We will describe two ways to obtain the Bearer Token via OAuth 2.0 below.
- Find the Application ID (App ID) located here and copy it to your clipboard.
- Visit the following URL, but replace APPLICATION_ID with the APP ID from your clipboard: https://api.kloudless.com/v1/oauth/?&scope=any.storage&client_id=APPLICATION_ID&response_type=token&state=13373
- After granting access to a specific cloud service, you should be able to obtain an access token (A.K.A Secret Token) for the account. Please copy this to your clipboard.
Now you can initialize the Java SDK with the following account to make requests.
Account account = new Account("YOUR BEARER TOKEN");
The Java SDK also provides a programmatic way for the developer to implement the 3-Legged OAuth Flow with the help of a webserver. Please refer to the additional documentation located here.
- Find the Application ID (App ID) located here and copy it to your clipboard.
- Determine the scope of what cloud services your users need to have to authenticate their account.
The scope
all
includes all services Kloudless supports. - The Redirect URI is of the webserver that the authenticated user will be redirected from. See documentation here for more details.
- Fill in the
App ID
,Redirect URI
, andScope
below to retrieve the Authorization URL. - Store the state for later use during third leg step.
- Redirect the user to this URL to begin the second leg of the OAuth flow process.
The example below shows how to retrieve a token in the context of a web application. The
Application
class helps generate an Authorization URL that redirects the user to after storing
the state
data generated to check when the user returns to our app.
@Controller
get("/", (request, response) -> {
Map<String, String> urlAndState = Application.getAuthorizationUrl(
"YOUR APP ID",
"http://YOUR DOMAIN NAME/callback",
"Scope");
App.currentState = urlAndState.get("state");
response.redirect(urlAndState.get("url"));
return "";
});
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. This access token will use the Bearer Token Authentication. Additional documentation available here.
- State: Obtained from the user after redirecting
- Code: Obtained from the user after redirecting
- Redirect URI: The Redirect URI specified in the First Leg
- Client ID: App ID in First Leg
- Client Secret: Find the API Key located here.
- Call the Java SDK Helper method with the information to retrieve the Bearer Token.
The code below continues our earlier example using a web application. The application has a
/callback
endpoint that receives the callback from the Kloudless server which includes the
original state
and a code
to exchange for an access token using the Application
class as
shown below.
@Controller
get("/callback", (request, response) -> {
String accessToken = Application.retrieveToken(
request.queryParams.get("state"),
"STATE FROM THE PREVIOUS STEP",
request.queryParams.get("code"),
"http://YOUR DOMAIN NAME/callback",
"YOUR APP ID",
"YOUR API KEY");
Account account = new Account(accessToken);
Resource me = (Resource) account.get("");
return me.toString();
});
If you already know your bearer token, you can instantiate an instance of the Account
class with
the token.
The Account class is the core of the Kloudless Java SDK. The account represents the authorized cloud
service that the user is connected to. Below are some examples of Kloudless API requests using the
instantiated account. The Kloudless Java SDK also has three response classes:
Resource
, ResourceList
, and ResponseRaw
. Examples on how to use these
response objects returned from the API requests are also included below.
Account account = new Account("YOUR BEARER TOKEN");
You can also use the Account ID and API Key to instantiate an Account object.
Account account = new Account("accountId", "YOUR API KEY");
We recommend verifying the token retrieved with your Application ID.
boolean isMatch = Application.verifyToken("YOUR BEARER TOKEN", "YOUR APP ID");
The Kloudless SDK will generate different objects for different types of data returned by the
Kloudless API Server. If Kloudless API Server returns a JSONObject list, the returned object will
be a ResourceList
that it contains a List of Resource
objects. Return data such as ID and name
can be found inside each Resource
object.
The example below showcases how to retrieve all calendars of a calendar cloud service account, such as Google Calendar or Outlook Calendar.
ResourceList calendarList = (ResourceList) account.get("cal/calendars");
for (Resource calendar : calendarList.getResources()) {
JsonObject calendarData = calendar.getData();
System.out.println(calendar.getId());
System.out.println(calendarData.get("name").getAsString());
}
You can also make an API request to retrieve information about a specific calendar. The SDK will
generate a Resource
object as the response.
Resource calendar = (Resource) account.get("cal/calendars/" + calendarId);
JsonObject calendarData = calendar.getData();
System.out.println(calendar.getId());
System.out.println(calendarData.get("name").getAsString());
The Resource
class represents a basic JSON response returned by the API server.
It also has helper methods for making HTTP requests.
Please refer to the example below.
//Get a calendar
Resource myCalendar = (Resource) account
.get("cal/calendars/" + calendarId);
//Modify the name of my calendar
Map<String, Object> updateContent = new HashMap<String, Object>();
updateContent.put("name", "The NEW name of my calendar");
Resource modifiedCalendar = myCalendar.patch(updateContent);
//Delete my calendar
modifiedCalendar.delete();
After you've retrieved a Resource
object, you can also modify the contents or metadata of the
object via the patch
or put
helper methods. Please refer to the file type
Resource
example below.
Resource file = (Resource) account.get("storage/files/" + fileId);
// Modify the meta data of file
Map<String, Object> updateMeta = new HashMap<String, Object>();
updateMeta.put("name", "The modified name");
updateMeta.put("parent_id", fileMeta.getData().get("parent").getAsString());
updateMeta.put("account", account.getId());
Resource modifiedFile = file.patch(updateMeta);
// Modify the file content
File newFile = new File("PATH OF NEW FILE");
Resource modifiedFileContent = file.put(newFile);
// Delete file
modifiedFileContent.delete();
The Kloudless API unifies data retrieved from the upstream service. However, if you would like to
parse the raw data returned yourself, you can set the X-Kloudless-Raw-Data
header attribute to
true
. The raw data will be returned in the raw
field of the JSON response.
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("X-Kloudless-Raw-Data", Boolean.toString(true));
Resource calendarRaw = (Resource) account.get("cal/calendars/" + calendarId, headers);
System.out.println(calendarRaw.getData().get("raw").toString());
The ResourceList Class is a collection of Resource objects, and it has additional helper methods below.
The Kloudless API supports pagination when a list of resources is returned. the ResourceList
Class includes helper methods to paginate through the list of Resource
objects. The following
example below shows how to retrieve events of a specific calendar with the current page.
It will then paginate for additional resources using hasNextPage()
and getNextPage()
.
ResourceList eventList = (ResourceList) account.get("cal/calendars/" + calendarId + "/events");
// Show the page number
System.out.println(eventList.getPage())
if(eventList.hasNextPage()){
ResourceList secondPage = eventList.getNextPage();
}
The ResourceList
class also supports auto-pagination to allow you to iterate over all resources.
The example below shows how to use the ResourceList
Iterator
.
ResourceList eventList = (ResourceList) account.get("cal/calendars/" + calendarId + "/events");
Iterator<Resource> resourceIterator = eventList.getPageIterator();
while (resourceIterator.hasNext()) {
Resource resource = resourceIterator.next();
System.out.println(resource.getData().get("name").getAsString());
}
The auto-pagination iterator can also be set with a limit on the number of resources traversed. The following example shows how to iterate over this subset.
ResourceList eventList = (ResourceList) account.get("cal/calendars/" + calendarId + "/events");
resourceIterator = eventList.getPageIterator(300L);
while (resourceIterator.hasNext()) {
Resource resource = resourceIterator.next();
System.out.println(resource.getData().get("name").getAsString());
}
The Kloudless API may return a JSON response that the SDK cannot parse into a Resource
or
ResourceList
instance. Please treat them similarly as you would a JsonObject
instance.
For handling different content types other than JSON, such as binary data, the SDK provides a
separate method getBinary
as well as a third type of Resource Class named ResponseRaw
.
ResponseRaw
inherits from the HttpResponse
class.
Resource fileMeta = (Resource) account.get("storage/files/" + fileId);
System.out.println("Get file meta " + fileMeta.getData().toString());
ResponseRaw responseRaw = account.getBinary(
"storage/files/" + fileId + "/contents");
System.out.println(ContentType.get(responseRaw.getData().getEntity())
HttpResponse httpResponse = responseRaw.getData();
try (InputStream inputStream = httpResponse.getEntity().getContent()) {
// Handle input stream here
}
Below are more examples on how to use the get
helper method to make API requests directly to the
Kloudless API Server. However, the Account class also supports other HTTP methods, such as post
,
put
, patch
, and delete
.
//Use account to create a new calendar
Map<String, Object> httpContent = new HashMap<String, Object>();
httpContent.put("name", "The New Calendar");
httpContent.put("description", "This is for an new calendar");
httpContent.put("location", "San Francisco, CA");
httpContent.put("timezone", "US/Pacific");
Resource newCalendar = (Resource) account.post("cal/calendars", httpContent);
String calendarId = newCalendar.getId();
//modify calendar
Map<String, Object> updateContent = new HashMap<String, Object>();
updateContent.put("name", "The Revised Calendar");
Resource modifiedCalendar = account.patch("cal/calendars/" + calendarId, updateContent);
//delete calendar
account.delete("cal/calendars/" + calendarId);
You can also use the Account object to create resources with the post
helper method. In
the example below, a file typed Resource
object with the name
and parent_id
metadata
set in the header of the HTTP request is created.
File newFile = new File("PATH OF LOCAL FILE");
String folderId = "FOLDER ID";
Map<String, Object> json = new HashMap<String, Object>();
json.put("name", newFile.getName());
json.put("parent_id", folderId);
Map<String, Object> headers = new HashMap<String, Object>();
headers.put("X-Kloudless-Metadata", new Gson().toJson(json));
Resource file = (Resource) account.post("storage/files?overwrite=false", headers, newFile);
It is possible use the Pass-through API to make HTTP requests directly to the upstream service for any endpoints unsupported by the Kloudless API. Please note that a raw HttpResponse object will be returned by the SDK.
HttpResponse driveInfo = account.raw("GET", "/drive/v2/about", null, null);
- You can create the JAR, skipping tests, with
gradle build -x test
. - You can create a JAR and install it to the local maven repository with
gradle install