Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Tested and updated for 2.2.0

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. 

...

  1. Follow the steps in Invoke your first API, to deploy the sample API, subscribe and generate keys.

    Info
    titleAccess Token

    Once the keys are generated, copy the access token. You can use this token to invoke APIs that you subscribe to using the same application.

  2. 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.
    Image RemovedImage Added
  3. In this example, you would have downloaded the PizzaShackAPI_1.0.0_java.zip file. This file name includes the API name, version, and language of the SDK. Unzip the PizzaShackAPI_1.0.0_java.zip file. 

    Expand
    titleExpand to see the folder structure of the unzipped file...
    Code Block
    PizzaShackAPI_1.0.0_java
    ├── build.gradle
    ├── build.sbt
    ├── docs
    │   ├── DefaultApi.md
    │   ├── ErrorListItem.md
    │   ├── Error.md
    │   ├── MenuItem.md
    │   └── Order.md
    ├── git_push.sh
    ├── gradle
    │   └── wrapper
    │       ├── gradle-wrapper.jar
    │       └── gradle-wrapper.properties
    ├── gradle.properties
    ├── gradlew
    ├── gradlew.bat
    ├── LICENSE
    ├── pom.xml
    ├── README.md
    ├── settings.gradle
    └── src
        ├── main
        │   ├── AndroidManifest.xml
        │   └── java
        │       └── org
        │           └── wso2
        │               └── client
        │                   ├── api
        │                   │   ├── ApiCallback.java
        │                   │   ├── ApiClient.java
        │                   │   ├── ApiException.java
        │                   │   ├── ApiResponse.java
        │                   │   ├── auth
        │                   │   │   ├── ApiKeyAuth.java
        │                   │   │   ├── Authentication.java
        │                   │   │   ├── HttpBasicAuth.java
        │                   │   │   ├── OAuthFlow.java
        │                   │   │   └── OAuth.java
        │                   │   ├── Configuration.java
        │                   │   ├── JSON.java
        │                   │   ├── Pair.java
        │                   │   ├── PizzaShackAPI
        │                   │   │   └── DefaultApi.java
        │                   │   ├── ProgressRequestBody.java
        │                   │   ├── ProgressResponseBody.java
        │                   │   └── StringUtil.java
        │                   └── model
        │                       └── PizzaShackAPI
        │                           ├── Error.java
        │                           ├── ErrorListItem.java
        │                           ├── MenuItem.java
        │                           └── Order.java
        └── test
            └── java
                └── org
                    └── wso2
                        └── client
                            └── api
                                └── PizzaShackAPI
                                    └── DefaultApiTest.java
  4. 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.

    Code Block
    titleMaven dependency
    collapsetrue
    <dependency>
        <groupId>org.wso2</groupId>
        <artifactId>org.wso2.client.PizzaShackAPI</artifactId>
        <version>1.0.0</version>
        <scope>compile</scope>
    </dependency>
    Info
    titleBuild using maven

    You can build the SDK using the mvn clean install command inside the root directory. For more information see Maven Start Guide.

  5. 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.

    Code Block
    import org.wso2.client.api.ApiClient;
    import org.wso2.client.api.PizzaShackAPI.DefaultApi;
    import org.wso2.client.model.PizzaShackAPI.Menu;
  6. Create an instance of the DefaultApi object 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.

    Code Block
    DefaultApi defaultApi = new DefaultApi();
  7. The API client of the DefaultApi object 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.

    Code Block
    ApiClient apiClient = defaultApi.getApiClient();
    apiClient.addDefaultHeader("Accept", "application/json");
  8. Include the access token as a header in the API client object, to invoke the API.

    Code Block
    String accessToken = "bc392b16-6ce2-3208-9023-8938fbc376ea";
    apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
    Warning

    You need an access token to invoke the API. It is important to have a valid subscription before using the SDK, to obtain an access token. Note that the obtained access token has an expiration time.

  9. Set the base path to the API client.

    Code Block
    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.

    Image RemovedImage Added


  10. Once the ApiClient object has all the required data, set the ApiClient for the instance of the DefaultApi object.

    Code Block
    defaultApi.setApiClient(apiClient);
  11. Finally, we can call the available function in the SDK to get the response from the API.

    Code Block
    List<MenuItem> menuItems = (List<MenuItem>) defaultApi.menuGet();
    Info

    MenuItem is a model class generated with SDK


    Complete java code can be found below


Localtabgroup
Localtab
titleJava Code
Code Block
import org.wso2.client.api.ApiClient;
import org.wso2.client.api.ApiException;
import org.wso2.client.api.PizzaShackAPI.DefaultApi;
import org.wso2.client.model.PizzaShackAPI.MenuItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class APIClient {

    public static void main(String[] args) throws ApiException {
        DefaultApi defaultApi = new DefaultApi();
        String accessToken = "bc392b16-6ce2-3208-9023-8938fbc376ea";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Accept", "application/json");
        headers.put("Authorization", "Bearer " + accessToken);
        ApiClient apiClient = defaultApi.getApiClient();
        apiClient.addDefaultHeader("Accept", "application/json");

        apiClient.addDefaultHeader("Authorization", "Bearer " + accessToken);
        apiClient.setLenientOnJson(true);
        apiClient.setBasePath("http://localhost:8280/pizzashack/1.0.0");
        defaultApi.setApiClient(apiClient);
        List<MenuItem> menuItems = (List<MenuItem>) defaultApi.menuGet();

        System.out.println(menuItems);
    }

}
Localtab
titlepom file
Code Block
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.pizzashack.client</groupId>
    <artifactId>pizzashack-api</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.wso2</groupId>
            <artifactId>org.wso2.client.PizzaShackAPI</artifactId>
            <version>1.0.0</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

</project>