Skip to content

Commit

Permalink
Merge pull request #160 from Nuvindu/wrapper
Browse files Browse the repository at this point in the history
Improve the generated client with a wrapper implementation
  • Loading branch information
Nuvindu authored Jan 2, 2025
2 parents fae5295 + 9e92260 commit 7d96eab
Show file tree
Hide file tree
Showing 15 changed files with 2,814 additions and 234 deletions.
1 change: 1 addition & 0 deletions .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ jobs:
uses: ballerina-platform/ballerina-library/.github/workflows/pr-build-connector-template.yml@main
secrets: inherit
with:
additional-build-flags: "-x :googleapis.gcalendar-examples:build"
additional-test-flags: ${{ github.event.pull_request.head.repo.full_name != github.repository && '-x test' || ''}}
2 changes: 1 addition & 1 deletion ballerina/Ballerina.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
distribution = "2201.8.0"
org = "ballerinax"
name = "googleapis.gcalendar"
version = "4.0.2"
version = "5.0.0"
license = ["Apache-2.0"]
authors = ["Ballerina"]
keywords = ["Productivity/Calendars", "Cost/Free", "Vendor/Google", "Collaboration", "Enterprise IT", "Management"]
Expand Down
14 changes: 8 additions & 6 deletions ballerina/Dependencies.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

[ballerina]
dependencies-toml-version = "2"
distribution-version = "2201.8.4"
distribution-version = "2201.10.0"

[[package]]
org = "ballerina"
Expand Down Expand Up @@ -64,7 +64,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "http"
version = "2.10.17"
version = "2.10.19"
dependencies = [
{org = "ballerina", name = "auth"},
{org = "ballerina", name = "cache"},
Expand Down Expand Up @@ -96,7 +96,7 @@ modules = [
[[package]]
org = "ballerina"
name = "io"
version = "1.6.1"
version = "1.6.3"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.value"}
Expand Down Expand Up @@ -244,7 +244,7 @@ dependencies = [
[[package]]
org = "ballerina"
name = "observe"
version = "1.2.3"
version = "1.3.0"
dependencies = [
{org = "ballerina", name = "jballerina.java"}
]
Expand Down Expand Up @@ -277,6 +277,7 @@ version = "0.0.0"
scope = "testOnly"
dependencies = [
{org = "ballerina", name = "jballerina.java"},
{org = "ballerina", name = "lang.array"},
{org = "ballerina", name = "lang.error"}
]
modules = [
Expand Down Expand Up @@ -317,7 +318,7 @@ modules = [
[[package]]
org = "ballerinax"
name = "googleapis.gcalendar"
version = "4.0.2"
version = "5.0.0"
dependencies = [
{org = "ballerina", name = "constraint"},
{org = "ballerina", name = "http"},
Expand All @@ -329,6 +330,7 @@ dependencies = [
]
modules = [
{org = "ballerinax", packageName = "googleapis.gcalendar", moduleName = "googleapis.gcalendar"},
{org = "ballerinax", packageName = "googleapis.gcalendar", moduleName = "googleapis.gcalendar.mock"}
{org = "ballerinax", packageName = "googleapis.gcalendar", moduleName = "googleapis.gcalendar.mock"},
{org = "ballerinax", packageName = "googleapis.gcalendar", moduleName = "googleapis.gcalendar.oas"}
]

267 changes: 72 additions & 195 deletions ballerina/client.bal

Large diffs are not rendered by default.

475 changes: 475 additions & 0 deletions ballerina/modules/oas/client.bal

Large diffs are not rendered by default.

1,406 changes: 1,406 additions & 0 deletions ballerina/modules/oas/types.bal

Large diffs are not rendered by default.

217 changes: 217 additions & 0 deletions ballerina/modules/oas/utils.bal
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
// Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved.
//
// 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.

// AUTO-GENERATED FILE. DO NOT MODIFY.
// This file is auto-generated by the Ballerina OpenAPI tool.

import ballerina/url;

type SimpleBasicType string|boolean|int|float|decimal;

# Represents encoding mechanism details.
type Encoding record {
# Defines how multiple values are delimited
string style = FORM;
# Specifies whether arrays and objects should generate as separate fields
boolean explode = true;
# Specifies the custom content type
string contentType?;
# Specifies the custom headers
map<any> headers?;
};

enum EncodingStyle {
DEEPOBJECT, FORM, SPACEDELIMITED, PIPEDELIMITED
}

final Encoding & readonly defaultEncoding = {};

# Serialize the record according to the deepObject style.
#
# + parent - Parent record name
# + anyRecord - Record to be serialized
# + return - Serialized record as a string
isolated function getDeepObjectStyleRequest(string parent, record {} anyRecord) returns string {
string[] recordArray = [];
foreach [string, anydata] [key, value] in anyRecord.entries() {
if value is SimpleBasicType {
recordArray.push(parent + "[" + key + "]" + "=" + getEncodedUri(value.toString()));
} else if value is SimpleBasicType[] {
recordArray.push(getSerializedArray(parent + "[" + key + "]" + "[]", value, DEEPOBJECT, true));
} else if value is record {} {
string nextParent = parent + "[" + key + "]";
recordArray.push(getDeepObjectStyleRequest(nextParent, value));
} else if value is record {}[] {
string nextParent = parent + "[" + key + "]";
recordArray.push(getSerializedRecordArray(nextParent, value, DEEPOBJECT));
}
recordArray.push("&");
}
_ = recordArray.pop();
return string:'join("", ...recordArray);
}

# Serialize the record according to the form style.
#
# + parent - Parent record name
# + anyRecord - Record to be serialized
# + explode - Specifies whether arrays and objects should generate separate parameters
# + return - Serialized record as a string
isolated function getFormStyleRequest(string parent, record {} anyRecord, boolean explode = true) returns string {
string[] recordArray = [];
if explode {
foreach [string, anydata] [key, value] in anyRecord.entries() {
if value is SimpleBasicType {
recordArray.push(key, "=", getEncodedUri(value.toString()));
} else if value is SimpleBasicType[] {
recordArray.push(getSerializedArray(key, value, explode = explode));
} else if value is record {} {
recordArray.push(getFormStyleRequest(parent, value, explode));
}
recordArray.push("&");
}
_ = recordArray.pop();
} else {
foreach [string, anydata] [key, value] in anyRecord.entries() {
if value is SimpleBasicType {
recordArray.push(key, ",", getEncodedUri(value.toString()));
} else if value is SimpleBasicType[] {
recordArray.push(getSerializedArray(key, value, explode = false));
} else if value is record {} {
recordArray.push(getFormStyleRequest(parent, value, explode));
}
recordArray.push(",");
}
_ = recordArray.pop();
}
return string:'join("", ...recordArray);
}

# Serialize arrays.
#
# + arrayName - Name of the field with arrays
# + anyArray - Array to be serialized
# + style - Defines how multiple values are delimited
# + explode - Specifies whether arrays and objects should generate separate parameters
# + return - Serialized array as a string
isolated function getSerializedArray(string arrayName, anydata[] anyArray, string style = "form", boolean explode = true) returns string {
string key = arrayName;
string[] arrayValues = [];
if anyArray.length() > 0 {
if style == FORM && !explode {
arrayValues.push(key, "=");
foreach anydata i in anyArray {
arrayValues.push(getEncodedUri(i.toString()), ",");
}
} else if style == SPACEDELIMITED && !explode {
arrayValues.push(key, "=");
foreach anydata i in anyArray {
arrayValues.push(getEncodedUri(i.toString()), "%20");
}
} else if style == PIPEDELIMITED && !explode {
arrayValues.push(key, "=");
foreach anydata i in anyArray {
arrayValues.push(getEncodedUri(i.toString()), "|");
}
} else if style == DEEPOBJECT {
foreach anydata i in anyArray {
arrayValues.push(key, "[]", "=", getEncodedUri(i.toString()), "&");
}
} else {
foreach anydata i in anyArray {
arrayValues.push(key, "=", getEncodedUri(i.toString()), "&");
}
}
_ = arrayValues.pop();
}
return string:'join("", ...arrayValues);
}

# Serialize the array of records according to the form style.
#
# + parent - Parent record name
# + value - Array of records to be serialized
# + style - Defines how multiple values are delimited
# + explode - Specifies whether arrays and objects should generate separate parameters
# + return - Serialized record as a string
isolated function getSerializedRecordArray(string parent, record {}[] value, string style = FORM, boolean explode = true) returns string {
string[] serializedArray = [];
if style == DEEPOBJECT {
int arayIndex = 0;
foreach var recordItem in value {
serializedArray.push(getDeepObjectStyleRequest(parent + "[" + arayIndex.toString() + "]", recordItem), "&");
arayIndex = arayIndex + 1;
}
} else {
if !explode {
serializedArray.push(parent, "=");
}
foreach var recordItem in value {
serializedArray.push(getFormStyleRequest(parent, recordItem, explode), ",");
}
}
_ = serializedArray.pop();
return string:'join("", ...serializedArray);
}

# Get Encoded URI for a given value.
#
# + value - Value to be encoded
# + return - Encoded string
isolated function getEncodedUri(anydata value) returns string {
string|error encoded = url:encode(value.toString(), "UTF8");
if encoded is string {
return encoded;
} else {
return value.toString();
}
}

# Generate query path with query parameter.
#
# + queryParam - Query parameter map
# + encodingMap - Details on serialization mechanism
# + return - Returns generated Path or error at failure of client initialization
isolated function getPathForQueryParam(map<anydata> queryParam, map<Encoding> encodingMap = {}) returns string|error {
string[] param = [];
if queryParam.length() > 0 {
param.push("?");
foreach var [key, value] in queryParam.entries() {
if value is () {
_ = queryParam.remove(key);
continue;
}
Encoding encodingData = encodingMap.hasKey(key) ? encodingMap.get(key) : defaultEncoding;
if value is SimpleBasicType {
param.push(key, "=", getEncodedUri(value.toString()));
} else if value is SimpleBasicType[] {
param.push(getSerializedArray(key, value, encodingData.style, encodingData.explode));
} else if value is record {} {
if encodingData.style == DEEPOBJECT {
param.push(getDeepObjectStyleRequest(key, value));
} else {
param.push(getFormStyleRequest(key, value, encodingData.explode));
}
} else {
param.push(key, "=", value.toString());
}
param.push("&");
}
_ = param.pop();
}
string restOfPath = string:'join("", ...param);
return restOfPath;
}
Loading

0 comments on commit 7d96eab

Please sign in to comment.