GoodData Java HTTP Client

Related Tags: api

If your favorite programming language is Java (Groovy or Scala) and you are going to implement functionality on the top of the GoodData API, this article and tooling described here will do you a favor.

Here is the story. You are starting with integration implementation and would need to list all your projects. For sure we have the API capability for this use case! One API call, really easy task right? But once you start there is authentication to overcome. You can do it in “raw format” handling authentication token (we have two of them) in cookies, or you can use the GoodData HTTP Client that will handle the complete authentication process.

For a better understanding, please see the example below. We already mentioned the goal - to get a list of your Projects. This should be pretty easy task.

How it Works

First of all, download the HTTP Client and install it with all dependencies. If you use Maven use following:

<dependency>
<groupId>com.gooddata</groupId>
<artifactId>gooddata-http-client</artifactId>
<version>${gdc.http.client.version}</version>
<dependency>

There are two types of authentication options. The first one is to use credentials, the second one is to use SST token for authentication. Check out the following code example using credentials authentication:

import com.gooddata.http.client.*
import java.io.IOException;
import org.apache.http.*;

HttpHost hostGoodData = new HttpHost("secure.gooddata.com", 443, "https");

// create login strategy, which will obtain SST via credentials
SSTRetrievalStrategy sstStrategy = 
     new LoginSSTRetrievalStrategy(new DefaultHttpClient(),hostGoodData login, password);

HttpClient client = new GoodDataHttpClient(new DefaultHttpClient(), sstStrategy);

// use HTTP client with transparent GoodData authentication
HttpGet getProject = new HttpGet("/gdc/md/");
getProject.addHeader("Accept", ContentType.APPLICATION_JSON.getMimeType());
HttpResponse getProjectResponse = client.execute(hostGoodData, getProject);

System.out.println(EntityUtils.toString(getProjectResponse.getEntity()));

Finally run the code and see the printed result. Easy enough, right? Of course you are able to use it with whatever API resource that is available using Java GET or POST methods. See the API documentation for other available API calls.