Skip to content

Commit

Permalink
Merge pull request #13 from ayeshLK/examples
Browse files Browse the repository at this point in the history
Add examples to the repository
  • Loading branch information
NipunaRanasinghe committed Apr 1, 2024
2 parents 2fd2030 + 68f7c19 commit 40faa55
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/online-application-portal/.github/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Ballerina online application portal

The Online Application Portal, powered by Guidewire InsuranceNow cloud API, is a user-friendly platform designed to simplify the process of applying for insurance coverage. Whether users are seeking auto, home, health, or any other type of insurance, the portal offers a seamless experience from start to finish.

Key functionalities include:

- Create new insurance quotes or applications
- Attach documents to the Insurance Applications
- List previously saved applications
- Convert insurance quotes into applications

## Prerequisites

### 1. Setup Guidewire InsuranceNow account

Refer to the [Setup guide](https://central.ballerina.io/ballerinax/guidewire.insnow/latest#setup-guide) to set up your Guidewire InsuranceNow account, if you do not have one.

### 2. Configuration

Update your Guidewire InsuranceNow account-related configurations in the `Config.toml` file in the example root directory:

```toml
guidewireDomain = "<domain>"
username = "<username>"
password = "<password>"
```

## Run the example

Execute the following command to run the example:

```ballerina
bal run
```
5 changes: 5 additions & 0 deletions examples/online-application-portal/Ballerina.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
org = "wso2"
name = "online_application_portal"
version = "0.1.0"

3 changes: 3 additions & 0 deletions examples/online-application-portal/Config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
guidewireDomain = "<domain>"
username = "<username>"
password = "<password>"
73 changes: 73 additions & 0 deletions examples/online-application-portal/response_err_interceptor.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;

service class ResponseErrorInterceptor {
*http:ResponseErrorInterceptor;

remote function interceptResponseError(http:Request request, error err) returns ClientError|ServerError|error {
string path = request.rawPath;
string method = request.method;

if err is http:PayloadValidationError {
return <ClientError>{
body: {
code: http:STATUS_BAD_REQUEST,
message: err.message(),
path: path,
method: method
}
};
}

if err is http:ApplicationResponseError {
http:Detail responseDetails = err.detail();
if responseDetails.statusCode == http:STATUS_BAD_REQUEST {
GuidewireErrorPayload payload = check responseDetails.body.ensureType();
return <ClientError>{
body: getResponsePayload(path, method, payload)
};
}

if responseDetails.statusCode == http:STATUS_INTERNAL_SERVER_ERROR {
GuidewireErrorPayload payload = check responseDetails.body.ensureType();
return <ServerError>{
body: getResponsePayload(path, method, payload)
};
}
}

return <ServerError>{
body: {
code: http:STATUS_INTERNAL_SERVER_ERROR,
message: err.message(),
path: path,
method: method
}
};
}
}

function getResponsePayload(string path, string method, GuidewireErrorPayload payload) returns ErrorPayload {
string? stacktrace = payload.stackTrace;
return {
code: payload.code,
message: stacktrace is () ? payload.message : string `${payload.message}:${stacktrace}`,
path,
method
};
}
74 changes: 74 additions & 0 deletions examples/online-application-portal/service.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerinax/guidewire.insnow;

configurable string guidewireDomain = ?;
configurable string username = ?;
configurable string password = ?;

service ApplicationPortal /portal on new http:Listener(9090) {
private final insnow:Client insuranceNow;

function init() returns error? {
self.insuranceNow = check new ({auth: {username, password}}, string `https://${guidewireDomain}/coreapi/v5`);
}

public function createInterceptors() returns ResponseErrorInterceptor {
return new ResponseErrorInterceptor();
}

# Retrieves the list of previously saved insurance applications.
#
# + customerId - The filter parameter for customer
# + continuationId - Indicates the starting offset for the API results
# + limit - The maximum number of results to return
# + return - List of applications or an error
resource function get applications(string? customerId = (), string? continuationId = (), string? 'limit = ()) returns insnow:ListApplication|error {
return self.insuranceNow->/applications(
customerId = customerId,
continuationId = continuationId,
'limit = 'limit
);
}

# Starts a new QuickQuote or Quote.
#
# + quote - The insurance quote
# + requestedTypeCd - The type of the quote, QuickQuote or Quote
# + return - An error or nil
resource function post applications(insnow:Quote quote, string? requestedTypeCd = ()) returns error? {
_ = check self.insuranceNow->/applications.post(quote, requestedTypeCd);
}

# Adds an attachment to a quote or application.
#
# + applicationId - System identifier of the application
# + attachment - The attachment
# + return - An error or nil
resource function post applications/[string applicationId]/documents(insnow:Attachment attachment) returns error? {
_ = check self.insuranceNow->/applications/[applicationId]/documents.post(attachment);
}

# Converts a quote to an application.
#
# + applicationId - System identifier of the application
# + return - An error or nil
resource function post applications/[string applicationId]/convert() returns error? {
_ = check self.insuranceNow->/applications/[applicationId]/bindRequest.post;
}
}
52 changes: 52 additions & 0 deletions examples/online-application-portal/service_desc.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (c) 2024 WSO2 LLC. (http://www.wso2.com).
//
// WSO2 LLC. licenses this file to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file except
// in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

import ballerina/http;
import ballerina/time;
import ballerinax/guidewire.insnow;

type ApplicationPortal service object {
*http:InterceptableService;

resource function get applications(string? customerId = (), string? continuationId = (), string? 'limit = ()) returns insnow:ListApplication|error;
resource function post applications(insnow:Quote quote, string? requestedTypeCd = ()) returns error?;
resource function post applications/[string applicationId]/documents(insnow:Attachment attachment) returns error?;
resource function post applications/[string applicationId]/convert() returns error?;
};

type GuidewireErrorPayload record {|
int code;
string message;
string stackTrace?;
|};

type ErrorPayload record {|
int code;
string message;
string path;
string method;
string timeStamp = time:utcToString(time:utcNow());
|};

type ClientError record {|
*http:BadRequest;
ErrorPayload body;
|};

type ServerError record {|
*http:InternalServerError;
ErrorPayload body;
|};

0 comments on commit 40faa55

Please sign in to comment.