Open NipunaRanasinghe opened 4 weeks ago
The generated util function should be changed like this:
isolated function createBodyParts(record {|anydata...;|} anyRecord, map<Encoding> encodingMap = {}) returns mime:Entity[]|error {
mime:Entity[] entities = [];
foreach [string, anydata] [key, value] in anyRecord.entries() {
Encoding encodingData = encodingMap.hasKey(key) ? encodingMap.get(key) : {};
mime:Entity entity = new mime:Entity();
if value is record {byte[] fileContent; string fileName;} {
entity.setContentDisposition(mime:getContentDispositionObject(string `form-data; name=${key}; filename=${value.fileName}`));
entity.setByteArray(value.fileContent);
} else if value is byte[] {
entity.setContentDisposition(mime:getContentDispositionObject(string `form-data; name=${key};`));
entity.setByteArray(value);
} else if value is SimpleBasicType {
entity.setContentDisposition(mime:getContentDispositionObject(string `form-data; name=${key};`));
entity.setText(value.toString());
} else if value is SimpleBasicType[] {
foreach SimpleBasicType member in value {
entity.setContentDisposition(mime:getContentDispositionObject(string `form-data; name=${key};`));
entity.setText(member.toString());
}
} else if value is record {} {
entity.setContentDisposition(mime:getContentDispositionObject(string `form-data; name=${key};`));
entity.setJson(value.toJson());
} else if value is record {}[] {
foreach record{} member in value {
entity.setContentDisposition(mime:getContentDispositionObject(string `form-data; name=${key};`));
entity.setJson(member.toJson());
}
}
if encodingData?.contentType is string {
check entity.setContentType(encodingData?.contentType.toString());
}
map<any>? headers = encodingData?.headers;
if headers is map<any> {
foreach var [headerName, headerValue] in headers.entries() {
if headerValue is SimpleBasicType {
entity.setHeader(headerName, headerValue.toString());
}
}
}
entities.push(entity);
}
return entities;
}
In addition to that, this function can be split into small parts to reduce the cognitive complexity
There is a option to specify whether we need to pass the array value as individual key-value pairs(explode: true
- default option) or a single form data with array element(explode: false
). So we need to check that option before applying this change
isolated function createBodyParts(record {|anydata...;|} anyRecord, map<Encoding> encodingMap = {})
returns mime:Entity[]|error {
mime:Entity[] entities = [];
foreach [string, anydata] [key, value] in anyRecord.entries() {
Encoding encodingData = encodingMap.hasKey(key) ? encodingMap.get(key) : {};
string contentDisposition = string `form-data; name=${key};`;
if value is record {byte[] fileContent; string fileName;} {
string fileContentDisposition = string `${contentDisposition} filename=${value.fileName}`;
mime:Entity entity = check constructEntity(fileContentDisposition, encodingData,
value.fileContent);
entities.push(entity);
} else if value is byte[] {
mime:Entity entity = check constructEntity(contentDisposition, encodingData, value);
entities.push(entity);
} else if value is SimpleBasicType {
mime:Entity entity = check constructEntity(contentDisposition, encodingData,
value.toString());
entities.push(entity);
} else if value is SimpleBasicType[] {
if encodingData.explode {
foreach SimpleBasicType member in value {
mime:Entity entity = check constructEntity(contentDisposition, encodingData,
member.toString());
entities.push(entity);
}
} else {
string[] valueStrArray = from SimpleBasicType val in value
select val.toString();
mime:Entity entity = check constructEntity(contentDisposition, encodingData,
string:'join(",", ...valueStrArray));
entities.push(entity);
}
} else if value is record {} {
mime:Entity entity = check constructEntity(contentDisposition, encodingData,
value.toString());
entities.push(entity);
} else if value is record {}[] {
if encodingData.explode {
foreach record {} member in value {
mime:Entity entity = check constructEntity(contentDisposition, encodingData,
member.toString());
entities.push(entity);
}
} else {
string[] valueStrArray = from record {} val in value
select val.toJsonString();
mime:Entity entity = check constructEntity(contentDisposition, encodingData,
string:'join(",", ...valueStrArray));
entities.push(entity);
}
}
}
return entities;
}
isolated function constructEntity(string contentDisposition, Encoding encoding,
string|byte[]|record {} data) returns mime:Entity|error {
mime:Entity entity = new mime:Entity();
entity.setContentDisposition(mime:getContentDispositionObject(contentDisposition));
if data is byte[] {
entity.setByteArray(data);
} else if data is string {
entity.setText(data);
} else {
entity.setJson(data.toJson());
}
check populateEncodingInfo(entity, encoding);
return entity;
}
isolated function populateEncodingInfo(mime:Entity entity, Encoding encoding) returns error? {
if encoding?.contentType is string {
check entity.setContentType(encoding?.contentType.toString());
}
map<any>? headers = encoding?.headers;
if headers is map<any> {
foreach var [headerName, headerValue] in headers.entries() {
if headerValue is SimpleBasicType {
entity.setHeader(headerName, headerValue.toString());
}
}
}
}
Description: $subject.
Steps to reproduce:
Refer to the resource function generated in https://github.com/ballerina-platform/module-ballerinax-openai.audio/blob/c0f76de8faafb37ed394dc2ea9d51d3033240d00/ballerina/client.bal#L72
I gives the error below when trying to invoke the same resource function with valid parameters
Affected Versions:
OS, DB, other environment details and versions:
Related Issues (optional):
Suggested Labels (optional):
Suggested Assignees (optional):