API Request via SDK(Last Updated : 01/11/2017)

API Client

To make an API call, you must first initialize the API client. The SDK currently supports API clients using OkHttp 3. The following is how you can create the API client instances.

import com.trumpia.sdk.client.ApiClient; import com.trumpia.sdk.client.okhttp3.OkHttp3ApiClient; ... ApiClient apiClient = new OkHttp3ApiClient(); ...

If the application is already using OkHttpClient, you can share the cache and thread pool. The following shows you how.

ApiClient apiClient = new OkHttp3ApiClient(myOkHttpClient);

API Context

To send an API request, you must have:

  • The URL for your API endpoint
  • Authentication Information

    - Username

    - API Key

The following shows you how to create an ApiContext object.

String userApiDomain = ...; ApiContext context = new ApiContext(userApiDomain, "myApiKeyBlahBlah", "myUserName");

API Request via API Client

To send a request, you must create an ApiRequest object, then execute it with the API Client. The following examples show you how to create and execute an API request.

// #1 DistributionListCreationResult result = DistributionApi.with(apiContext) .create(distributionList) .executeOn(apiClient); // #2 ApiRequest<DistributionListCreationResult> apiRequest = DistributionApi.with(apiContext).create(distributionList); DistributionListCreationResult result = apiClient.execute(apiRequest);

Handling Exceptions

When you attempt to execute the request, an exception may be thrown. You may see an IOException or a ApiException. IOExceptions may be generated for general cases while ApiException may be generated for issues with the Trumpia API request.

You can retrieve API status codes and JSON responses from the ApiException object.

try { DistributionListCreationResult result = DistributionApi.with(apiContext) .create(distributionList) .executeOn(apiClient); // Process the result… } catch (IOException e) { // Handle general IO exceptions } catch (ApiException e) { // Handle API exceptions logger.error("API error: statusCode={}, rawContent={}", e.getStatusCode(), e.getRawContent()); }