Write a Client Application Using the SDK
A software development kit (SDK) is a set of software development tools that allows to create applications for a specific platform. If an API consumer wants to create an application, they can generate a client side SDK for a supported language/framework and use it to write a software application to consume the subscribed APIs. This tutorial shows you how to write a client application using an SDK.
In this example, we use the sample API in WSO2 API Manager as a demonstration. To deploy the sample API, log in to the API Publisher and click the Deploy Sample API button. Note that the button only appears if no APIs have been created so far, in the given tenant space.
If a different API is used, the SDK functions to invoke the API are based on the specifications of that API.
Follow the steps in Invoke your first API, to deploy the sample API, subscribe and generate keys.
Go to the API Store. Select your API and download the SDK for Java. For more details, see Generating client SDKs in the API Store.
In this example, you would have downloaded the
PizzaShackAPI_1.0.0_java.zipfile. This file name includes the API name, version, and language of the SDK. Unzip thePizzaShackAPI_1.0.0_java.zipfile.Build the SDK using maven.
When it’s done, you can include this SDK as a dependency in your software project. Details of this maven dependency are included in the README.md file.Maven dependency
<dependency> <groupId>org.wso2</groupId> <artifactId>org.wso2.client.PizzaShackAPI</artifactId> <version>1.0.0</version> <scope>compile</scope> </dependency>After creating a maven project, import the following with respect to the SDK. These classes will be accessible from the code once the SDK is built using maven and will be included as maven dependencies in the project.
import org.wso2.client.api.ApiClient; import org.wso2.client.api.PizzaShackAPI.DefaultApi; import org.wso2.client.model.PizzaShackAPI.Menu;Create an instance of the
DefaultApiobject in the java code. This instance is needed to get the API client which handles the operations related to consuming the API, using the resources of the API.DefaultApi defaultApi = new DefaultApi();The API client of the
DefaultApiobject instance is used to set HTTP request headers with the required data. Note that these HTTP request headers might differ from one API to another, depending on the implementation of the API. A sample is show below.ApiClient apiClient = defaultApi.getApiClient(); apiClient.addDefaultHeader("Accept", "application/json");Include the access token as a header in the API client object, to invoke the API.
String accessToken = "bc392b16-6ce2-3208-9023-8938fbc376ea"; apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);Set the base path to the API client.
apiClient.setBasePath("http://localhost:8280/pizzashack/1.0.0");
The base path for the client application is the production (or sandbox) URL of the API, found in the Overview tab of the API in the API Store.Once the
ApiClientobject has all the required data, set theApiClientfor the instance of theDefaultApiobject.defaultApi.setApiClient(apiClient);Finally, we can call the available function in the SDK to get the response from the API.
List<MenuItem> menuItems = (List<MenuItem>) defaultApi.menuGet();
Complete java code can be found below