This article is now deprecated. SDK usage instructions are now contained in the generated SDK.
See the new APIMATIC docs.
In this article, we will walk you through the generation of the SDK and how to use it.
The following covers the following aspects:
- Setting up the environment
- SDK generation instructions
- SDK installation instructions
- Creating a sample application that uses our SDK
1. Setting up the environment:
Following this tutorial here, a library is already generated, downloaded, and ready for use.
We will be using a Maven eclipse project in Java.
2. SDK generation instructions:
After choosing Java as your code language, as shown in Figure 1, a zipped file will be downloaded. This file contains the code for the APIMATICCalculator library we will be using in our simple Maven project.
3. SDK installation instructions:
In order to use your library, you have to build and install it first. This will result in installing dependencies and adding the generated JAR in your local maven repository.
Extract the unzipped file to a new folder named JavaSDK, as shown in Figure 2. This will store the library for later use in our project.
Method 1: Building via Eclipse:
Firstly, open your library in Eclipse. To build and install, right click on the project in the package explorer, and choose Run as -> maven install from the menu, as shown in Figure 3.
Method 2: Building via CLI:
Alternatively, you can navigate to your directory from command prompt.
cd .../JavaSDK/APIMATICCalculatorLib
then run
mvn install
4. Creating a sample application:
The following section explains how to start a new Maven project that uses your generated APIMATICCalculator library.
4.1 Starting a Maven project
On starting new Maven project on Eclipse,
File -> new -> maven project
Tick on the "Create a simple project" for simplicity, and browse your desired workspace, then click on "next", as shown in Figure 4.
Provide group ID and Artifact ID, then select “finish”, as shown in Figure 5. The project hierarchy will show up on your project explorer.
In order to use the APIMATICCalculator library, go to your pom.xml file and add the dependency of the library needed, as shown in Figure 6. Make sure the tags are closed correctly.
Your maven project knows about your library, and "depends" on it!
Right-click on your project from the package explorer and select
Maven -> Update Project
(or alternatively, ALT+f5). Click "OK".
4.2 Building your Maven project
Now, to build this project and install all its libraries and dependencies, we need to run it.
From the package explorer, right click on your project and
Run As -> Maven Install
"Maven install" compiles, builds and installs the project in one step.
The image below shows the console displaying build success.
You are ready to use the library in your code.
4.3 Using your library in your Maven project:
In order to use the library, you need to instantiate its objects and invoke their methods.
Right-click on your src/main/java, and add a new class. Provide a suitable name (e.g Main.java), and click "finish".
Now we need to add the Java's main method which is the entry point for code execution.
public static void main(String[] args) {...}
Like any other object, a controller is instantiated by calling its constructor.
To get our calculator running, we need to invoke the non-static method getCalculateAsync() shown in Figure 8 on it.
This method takes an operation name (e.g. SUM) of type enum, two doubles and a APIcallback object, which instantiates an interface. As with any interface, we have to provide implementations for its own methods. In this case we have two methods; onFailure() and onSuccess(), which are executed depending on the result of running the program. Figure 9 shows how your main class should look like after adding your code.
In both methods onSuccess(), and onFailure(), “Object arg0” argument is a context object which has a raw information about the HTTP request and response.
- In onSuccess(), “String arg1” is the result of the operation performed by the calculator (the response).
- In onFailure(), “Throwable arg1” is an exception object that can be used to track down the error encountered.
You are ready to run the project successfully, which will print out the result to the console, as shown in Figure 9.
On the unlikely event of running into an error, and onFailure() was called instead, you can fix the problem by:
- Debugging: Adding a toggle point inside the onFailure() method may help figure out the error. (as shown in Figure 9, line 24)
- Search for help provided here.
Comments
0 comments
Please sign in to leave a comment.