-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix basic type handling and add a sample
- Loading branch information
1 parent
ccf8e21
commit fd3ef9b
Showing
11 changed files
with
611 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
## Overview | ||
|
||
This sample demonstrates the supported types of the Ballerina MI connector SDK. A Ballerina function with parameters and return type of `boolean`, `string`, `int`, `float`, `decimal`, `xml`, and `json` is supported by the MI SDK. | ||
This scenario illustrates a content-based routing use case where a JSON payload containing a `type` property is sent to an API. Based on the value of this `type` property, a corresponding Ballerina function is invoked. | ||
|
||
A json payload of following format is sent to the endpoint | ||
|
||
```json | ||
{ | ||
"type": "integer", | ||
"val": 21 | ||
} | ||
``` | ||
|
||
For the type field it is possible to send `boolean`, `string`, `int`, `float`, `decimal`, `xml`, and `json`. | ||
|
||
This would output a result in the following format. | ||
|
||
```json | ||
{ | ||
"result": 42 | ||
} | ||
``` | ||
|
||
## Steps to Invoke the Sample | ||
|
||
Follow these steps to invoke the sample using the connector: | ||
|
||
1. The `bal-type-processing` folder contains the Ballerina code for the connector. Invoke the following command to generate the connector: | ||
|
||
```bash | ||
bal mi -i <ballerina-project> | ||
``` | ||
|
||
2. A ZIP file of the connector will be generated. Add this ZIP file to the MI project inside the folder `mi-type-processing` following the approach described [here](https://mi.docs.wso2.com/en/latest/develop/creating-artifacts/adding-connectors/). | ||
|
||
3. Once the connector is added, run the MI project. | ||
|
||
4. Send an HTTP POST request to the following resource with a payload as specified: | ||
|
||
```bash | ||
curl --location 'http://localhost:8290/type-processing' \ | ||
--header 'Content-Type: application/json' \ | ||
--data '{ | ||
"type": "decimal", | ||
"val": 5.3 | ||
}' | ||
``` | ||
|
||
Output: | ||
|
||
```json | ||
{ | ||
"result":15.3 | ||
} | ||
``` |
14 changes: 14 additions & 0 deletions
14
examples/type-processing/bal-type-processing/Ballerina.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
org = "testOrg" | ||
name = "bal_type_processing" | ||
version = "0.1.0" | ||
distribution = "2201.9.2" | ||
|
||
[build-options] | ||
observabilityIncluded = true | ||
|
||
[[dependency]] | ||
org = "ballerinax" | ||
name = "mi" | ||
version = "0.1.3" | ||
repository = "local" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import ballerinax/mi; | ||
|
||
@mi:ConnectorInfo | ||
public function invertBoolean(boolean b) returns boolean => !b; | ||
|
||
@mi:ConnectorInfo | ||
public function doubleInt(int n) returns int => n * 2; | ||
|
||
@mi:ConnectorInfo | ||
public function reciprocalFloat(float f) returns float { | ||
if f == 0.0 { | ||
return 1; | ||
} | ||
return 1.0 / f; | ||
} | ||
|
||
@mi:ConnectorInfo | ||
public function addConstantToDecimal(decimal d) returns decimal => d + 10; | ||
|
||
@mi:ConnectorInfo | ||
public function doubleString(string s) returns string => s + s; | ||
|
||
@mi:ConnectorInfo | ||
public function getJsonNameProperty(json j) returns json { | ||
json jsn = j; | ||
if jsn is string { | ||
json|error je = jsn.fromJsonString(); | ||
if je is error { | ||
return {err: je.message()}; | ||
} | ||
jsn = je; | ||
} | ||
json|error val = jsn.name; | ||
if val is error { | ||
return {err: val.message()}; | ||
} | ||
return {val}; | ||
} | ||
|
||
@mi:ConnectorInfo | ||
public function getXmlNameElement(xml x) returns xml { | ||
xml y = x/<name>; | ||
return xml `<result>${y}</result>`; | ||
} |
18 changes: 18 additions & 0 deletions
18
examples/type-processing/mi-type-processing/deployment/deployment.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[server] | ||
hostname = "localhost" | ||
hot_deployment = false | ||
|
||
[user_store] | ||
type = "read_only_ldap" | ||
|
||
[keystore.primary] | ||
file_name = "repository/resources/security/wso2carbon.jks" | ||
password = "wso2carbon" | ||
alias = "wso2carbon" | ||
key_password = "wso2carbon" | ||
|
||
[truststore] | ||
file_name = "repository/resources/security/client-truststore.jks" | ||
password = "wso2carbon" | ||
alias = "symmetric.key.value" | ||
algorithm = "AES" |
5 changes: 5 additions & 0 deletions
5
examples/type-processing/mi-type-processing/deployment/docker/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
ARG BASE_IMAGE | ||
FROM ${BASE_IMAGE} | ||
COPY CompositeApps/*.car ${WSO2_SERVER_HOME}/repository/deployment/server/carbonapps/ | ||
COPY resources/wso2carbon.jks ${WSO2_SERVER_HOME}/repository/resources/security/wso2carbon.jks | ||
COPY resources/client-truststore.jks ${WSO2_SERVER_HOME}/repository/resources/security/client-truststore.jks |
Binary file added
BIN
+166 KB
...ples/type-processing/mi-type-processing/deployment/docker/resources/client-truststore.jks
Binary file not shown.
Binary file added
BIN
+168 KB
examples/type-processing/mi-type-processing/deployment/docker/resources/wso2carbon.jks
Binary file not shown.
Oops, something went wrong.