Importing Classes in SDK
To access all methods in the SDK, you should import ApiProtection
, ApiProtectionCallback
, and AuthCallBack
. These classes serve different purposes: ApiProtection
and ApiProtectionCallBack
are used for the Registration and Transaction modules, while AuthCallBack
is used for the Terminate method. Declare these three classes in your project and place them in the Activity class where you intend to use the SDK.
import com.safous.waap.ApiProtection;
import com.safous.waap.ApiProtectionCallBack;
import com.safous.waap.AuthCallBack;
Now, you have the SDK in your Activity class. Next, create a new variable to represent the ApiProtection
itself. This variable can be used within your class or accessed globally as long as you have already imported the ApiProtection
class.
public static ApiProtection protection;
Based on the explanation above, there are three modules that you can use in the Android SDK: the Registration Module, the Transaction Module, and the Terminate Module. Each module has different methods, but all methods have the same interface callback. The purpose of this interface is to ensure that the methods have the same state for every response. There are two states: onSuccessReceived()
for successful responses, and onErrorReceived()
for displaying error information.
@Override
public void onSuccessReceived(Response r) {
// state for successful response from methods
}
@Override
public void onErrorReceived(Error e) {
// state for showing error response from methods
}
Registration Module
In this module, there are two methods you will use. Each method is required for specific steps, so make sure you follow the steps by calling the method correctly.
- RegisterDevice method
To start, initialize a new variable of ApiProtection
. This variable is needed for calling every method. Hence, create a new instance of ApiProtection
that extends the context of your Activity class.
protection = new ApiProtection(getApplicationContext());
Next, create the ApiProtectionCallback
variable. By declaring this variable, you will generate the two-state interface of onSuccessReceived
and onErrorReceived
within the block function.
ApiProtectionCallBack<Object> callBack = new ApiProtectionCallBack<Object>() {
@Override
public void onSuccessReceived(Response r) {
// Handle success response
}
@Override
public void onErrorReceived(Error e) {
// Handle error response
}
};
Finally, call the RegisterDevice
method. This method requires callback parameters that you have already created above.
protection.registerDevice(callBack);
2. VerifyDevice method
The purpose of this method is to obtain the Token as authentication for your recent project app. In the SDK, this method requires an Identity value as a parameter request. You can get the Identity value from the success response from the registerDevice
method. So, make sure you have already done the registerDevice
method and it has returned success.
Next, you need to call the method from the SDK. Create the ApiProtectionCallback
variable.
protection = new ApiProtection(getApplicationContext());
Similar to registerDevice
, you also need to create an ApiProtectionCallback
variable. By declaring this variable, you will automatically generate a two-state interface too.
ApiProtectionCallBack<Object> callBack = new ApiProtectionCallBack<Object>() {
@Override
public void onSuccessReceived(Response r) {
// Handle success response
}
@Override
public void onErrorReceived(Error e) {
// Handle error response
}
};
Now, we can declare the verifyDevice
method. There are two parameters in this method. The first is for a callback from ApiProtectionCallBack
, and the second is called header. The header parameter is used as optional authentication in your project. But if you do not want to use it, you can set it to null.
protection.verifyDevice(callBack,null);
Transaction Module
This module only has a single method. The concept of this module is similar to the Register. As a reminder, this module only works if the Registration Module has been completed and you have created and called it.
To create this Module, you will use the transaction
method. The first step to create this method is mostly similar to the registration one. Create the ApiProtection
variable, then create the ApiProtectionCallback
variable to generate a two-state interface.
protection = new ApiProtection(getApplicationContext());
ApiProtectionCallBack<Object> callBack = new ApiProtectionCallBack<Object>() {
@Override
public void onSuccessReceived(Response r) {
// Handle success response
}
@Override
public void onErrorReceived(Error e) {
// Handle error response
}
};
Then you can call the transaction
method. Declare it after you create the several variables above.
protection.transaction(callBack);
Terminate
For every transaction method you have called, you need to call the terminate
method if you want to call another new transaction method. So, ensure your project includes this method.
As usual, you need to create an ApiProtection
variable to access the SDK.
protection = new ApiProtection(getApplicationContext());
Slightly different from the last two modules, Terminate needs an AuthCallBack
variable. This will also generate a two-state interface. So, declare it in your project.
AuthCallBack<Object> callBack = new AuthCallBack<Object>() {
@Override
public void onSuccessReceived(Response r) {
// Handle success response
}
@Override
public void onErrorReceived(Error e) {
// Handle error response
}
};
Finally, create the terminate
method. Put the callback variable into the parameter.
protection.terminate(callBack);
Error Handling
This refer to inform while you get onErrorReceived block in every module. The instance return with JSON format, you can call getMessage method for message error, getBody method for whole format, and getCode for error code. Here's a sample for using those method.
ApiProtectionCallBack<Object> callBack =new ApiProtectionCallBack<Object>() {
@Override
public void onSuccessReceived(Response r) {
/// Do while Success
}
@Override
public void onErrorReceived(Error error) {
/// Show Error Code
error.getCode()
/// Show Error Message
error.getMessage()
/// Show Error Body
error.getBody()
}
};
Here is the complete error code with a description.
Error Code | Message Body | Description |
-1 | Error from SDK | Get failed from SDK. For example: Connection fail, Failed for generate cookies, error on registered device, etc. |
302 | Found | user-cookies have a problem or not match |
400 | Invalid Request | |
401 | Unauthorized Request | |
403 | Forbidden |