The generated code uses a few Maven dependencies e.g., Jackson, OkHttp, and Apache HttpClient. The reference to these dependencies is already added in the pom.xml file will be installed automatically. Therefore, you will need internet access for a successful build.
- In order to open the client library in Eclipse click on
File -> Import.
- In the import dialog, select
Existing Java Projectand clickNext.
- Browse to locate the folder containing the source code. Select the detected location of the project and click
Finish.
- Upon successful import, the project will be automatically built by Eclipse after automatically resolving the dependencies.
The following section explains how to use the SimpleCalculatorLib library in a new project.
For starting a new project, click the menu command File > New > Project.
Next, choose Maven > Maven Project and click Next.
Here, make sure to use the current workspace by choosing Use default Workspace location, as shown in the picture below and click Next.
Following this, select the quick start project type to create a simple project with an existing class and a main method. To do this, choose maven-archetype-quickstart item from the list and click Next.
In the last step, provide a Group Id and Artifact Id as shown in the picture below and click Finish.
The created Maven project manages its dependencies using its pom.xml file. In order to add a dependency on the SimpleCalculatorLib client library, double click on the pom.xml file in the Package Explorer. Opening the pom.xml file will render a graphical view on the canvas. Here, switch to the Dependencies tab and click the Add button as shown in the picture below.
Clicking the Add button will open a dialog where you need to specify SimpleCalculatorLib in Group Id, simple-calculator-lib in Artifact Id and 4.0.0 in the Version fields. Once added click OK. Save the pom.xml file.
Once the SimpleConsoleApp is created, a file named App.java will be visible in the Package Explorer with a main method. This is the entry point for the execution of the created project.
Here, you can add code to initialize the client library and instantiate a Controller class. Sample code to initialize the client library and using controller methods is given in the subsequent sections.
The following parameters are configurable for the API Client:
| Parameter | Type | Description |
|---|---|---|
httpClientConfig |
ReadonlyHttpClientConfiguration |
Http Client Configuration instance. * See available builder methods here. |
username |
String |
Default: "testuser" |
password |
String |
Default: "Test@123" |
The API client can be initialized as follows:
SimpleCalculatorClient client = new SimpleCalculatorClient.Builder()
.httpClientConfig(configBuilder -> configBuilder
.timeout(0))
.basicAuthCredentials("testuser", "Test@123")
.build();This API uses Basic Authentication.
The gateway for the SDK. This class acts as a factory for the Controllers and also holds the configuration of the SDK.
| Name | Description | Return Type |
|---|---|---|
getSimpleCalculatorController() |
Provides access to SimpleCalculator controller. | SimpleCalculatorController |
getDummyController() |
Provides access to Dummy controller. | DummyController |
| Name | Description | Return Type |
|---|---|---|
shutdown() |
Shutdown the underlying HttpClient instance. | void |
getEnvironment() |
Current API environment. | Environment |
getHttpClient() |
The HTTP Client instance to use for making HTTP requests. | HttpClient |
getHttpClientConfig() |
Http Client Configuration instance. | ReadonlyHttpClientConfiguration |
getBaseUri(Server server) |
Get base URI by current environment | String |
getBaseUri() |
Get base URI by current environment | String |
An instance of the SimpleCalculatorController class can be accessed from the API Client.
SimpleCalculatorController simpleCalculatorController = client.getSimpleCalculatorController();
Calculates the expression using specified operation.
CompletableFuture<Double> calculateAsync(
final OperationTypeEnum operation,
final double x,
final double y)| Parameter | Type | Tags | Description |
|---|---|---|---|
operation |
OperationTypeEnum |
Template, Required | The operator to apply on the variables |
x |
double |
Query, Required | This is LHS value |
y |
double |
Query, Required | This is RHS value |
double
OperationTypeEnum operation = OperationTypeEnum.SUM;
double x = 222.14;
double y = 165.14;
simpleCalculatorController.calculateAsync(operation, x, y).thenAccept(result -> {
// TODO success callback handler
}).exceptionally(exception -> {
// TODO failure callback handler
return null;
});An instance of the DummyController class can be accessed from the API Client.
DummyController dummyController = client.getDummyController();
CompletableFuture<Double> dummyAsync()double
dummyController.dummyAsync().thenAccept(result -> {
// TODO success callback handler
}).exceptionally(exception -> {
// TODO failure callback handler
return null;
});Possible operators are sum and subtract
OperationTypeEnum
| Name | Description |
|---|---|
SUM |
Represents the sum operator |
SUBTRACT |
Represents the subract operator |
MULTIPLY |
Represents the multiply operator |
This is a Helper class with commonly used utilities for the SDK.
| Name | Description | Type |
|---|---|---|
| mapper | Deserialization of Json data. | ObjectMapper |
| Name | Description | Return Type |
|---|---|---|
serialize(Object obj) |
Json Serialization of a given object. | String |
deserialize(String json) |
Json deserialization of the given Json string. | LinkedHashMap<String, Object> |
deserialize(String json, Class<T> clazz) |
Json deserialization of the given Json string. | <T extends Object> T |
deserialize(String json, TypeReference<T> typeReference) |
JSON Deserialization of the given json string. | <T extends Object> T |
deserializeArray(String json, Class<T[]> classArray) |
JSON Deserialization of the given json string. | <T extends Object> List<T> |
Class to wrap file and contentType to be sent as part of a HTTP request.
| Name | Description |
|---|---|
FileWrapper(File file) |
Initialization constructor. |
FileWrapper(File file, String contentType) |
Initialization constructor. |
| Name | Description | Return Type |
|---|---|---|
getFile() |
File instance. | File |
getContentType() |
Content type of the file. | String |
Class for creating and managing HTTP Requests.
| Name | Description |
|---|---|
HttpRequest(HttpMethod method, StringBuilder queryUrlBuilder, Headers headers, Map<String, Object> queryParameters, List< SimpleEntry < String, Object >> parameters) |
Initializes a simple http request. |
| Name | Description | Return Type |
|---|---|---|
getHttpMethod() |
HttpMethod for the http request. | HttpMethod |
getHeaders() |
Headers for the http request. | Headers |
getQueryUrl() |
Query url for the http request. | String |
getParameters() |
Parameters for the http request. | List<SimpleEntry<String, Object>> |
getQueryParameters() |
Query parameters for the http request. | Map<String, Object> |
addQueryParameter(String key, Object value) |
Add Query parameter in http request. | void |
Class to hold HTTP Response.
| Name | Description |
|---|---|
HttpResponse(int code, Headers headers, InputStream rawBody) |
Constructor for HttpResponse. |
| Name | Description | Return Type |
|---|---|---|
getStatusCode() |
HTTP Status code of the http response.. | int |
getHeaders() |
Headers of the http response. | Headers |
getRawBody() |
Raw body of the http response. | InputStream |
Class to hold response body as string.
| Name | Description |
|---|---|
HttpStringResponse(int code, Headers headers, InputStream rawBody, String body) |
Constructor for HttpStringResponse. |
| Name | Description | Return Type |
|---|---|---|
getStatusCode() |
HTTP Status code of the http response. | int |
getHeaders() |
Headers of the http response. | Headers |
getBody() |
String body of the http response. | String |
Class to wrap the request sent to the server and the response received from the server.
| Name | Description |
|---|---|
HttpContext(HttpRequest request, HttpResponse response) |
Constructor for HttpContext. |
| Name | Description | Return Type |
|---|---|---|
getRequest() |
Getter for the Http Request. | HttpRequest |
getHttpContext() |
Getter for the Http Response. | HttpContext |
HTTP Request with an explicit body.
| Name | Description |
|---|---|
HttpBodyRequest(HttpMethod method, StringBuilder queryUrlBuilder, Headers headers, Map<String, Object> queryParams, Object body) |
Create a request with explicit body. |
| Name | Description | Return Type |
|---|---|---|
getBody() |
Body for the http request. | Object |
Class for creating and managing HTTP Headers.
| Name | Description |
|---|---|
Headers() |
Default constructor. |
Headers(Map<String, List<String>> headers) |
Constructor that creates a new instance using a given Map. |
Headers(Headers h) |
Copy Constructor. |
| Name | Description | Return Type |
|---|---|---|
has(String headerName) |
Use to check if the given name is present in headers. | boolean |
names() |
Returns a Set containing all header names. | Set<String> |
value(String headerName) |
Returns the first value associated with a given header name, or null if the header name is not found. | String |
values(String headerName) |
Returns a List of all values associated with a given header name, or null if the header name is not found. | List<String> |
asSimpleMap() |
Returns a Map of the headers, giving only one value for each header name. | Map<String, String> |
asMultimap() |
Returns a simulated MultiMap of the headers. | Map<String, List<String>> |
cloneHeaderMap(Map<String, List<String>> headerMap) |
Clones a header map. | Map<String, List<String>> |
add(String headerName, String value) |
Adds a value for a header name to this object. | void |
add(String headerName, List<String> values) |
Adds a List of values for a header name to this object. | void |
addAllFromMap(Map<String, String> headers) |
Adds values from a Map to this object. | void |
addAllFromMultiMap(Map<String, List<String>> headers) |
Adds values from a simulated Multi-Map to this object. | void |
addAll(Headers headers) |
Adds all the entries in a Headers object to this object. | void |
remove(String headerName) |
Removes the mapping for a header name if it is present. | List<String> |
This is the base class for all exceptions that represent an error response from the server.
| Name | Description |
|---|---|
ApiException(String reason) |
Initialization constructor. |
ApiException(String reason, HttpContext context) |
Initialization constructor. |
| Name | Description | Return Type |
|---|---|---|
getResponseCode() |
The HTTP response code from the API request | int |
getHeaders() |
The HTTP response body from the API request. | Headers |
This is the base class for all exceptions that represent an error response from the server.
| Name | Description | Return Type |
|---|---|---|
getEnvironment() |
Current API environment. | Environment |
getHttpClientConfig() |
Http Client Configuration instance. * See available builder methods here. |
ReadonlyHttpClientConfiguration |
getBaseUri(Server server) |
Get base URI by current environment. | String |
getBaseUri() |
Get base URI by current environment. | String |
Class for holding http client configuration.
| Name | Description | Return Type |
|---|---|---|
getTimeout() |
The timeout in seconds to use for making HTTP requests. | long |
getNumberOfRetries() |
The number of retries to make. | int |
getBackOffFactor() |
To use in calculation of wait time for next request in case of failure. | int |
getRetryInterval() |
To use in calculation of wait time for next request in case of failure. | long |
getHttpStatusCodesToRetry() |
Http status codes to retry against. | Set<Integer> |
getHttpMethodsToRetry() |
Http methods to retry against. | Set<HttpMethod> |
getMaxBackOff() |
The maximum wait time for overall retrying requests. | long |
shouldRetryOnTimeout() |
Whether to retry on request timeout. | boolean |
toString() |
Converts this HttpClientConfiguration into string format. | String |
newBuilder() |
Builds a new {@link HttpClientConfiguration.Builder} object. Creates the instance with the current state. | HttpClientConfiguration.Builder |
Class to build instances of {@link HttpClientConfiguration}.
| Name | Description | Return Type |
|---|---|---|
timeout() |
The timeout in seconds to use for making HTTP requests. | long |
numberOfRetries() |
The number of retries to make. | int |
backOffFactor() |
To use in calculation of wait time for next request in case of failure. | int |
retryInterval() |
To use in calculation of wait time for next request in case of failure. | long |
httpStatusCodesToRetry() |
Http status codes to retry against. | Set<Integer> |
httpMethodsToRetry() |
Http methods to retry against. | Set<HttpMethod> |
maxBackOff() |
The maximum wait time for overall retrying requests. | long |
shouldRetryOnTimeout() |
Whether to retry on request timeout. | boolean |