From bd6bae6876bf086002cb7487786602c4bf231e1b Mon Sep 17 00:00:00 2001 From: Nisura Indisa <64645496+nisuraaa@users.noreply.github.com> Date: Wed, 20 Mar 2024 17:13:27 +0530 Subject: [PATCH] Update xml payload retrieving logic from HTTP response # this is to include the improvements suggested in https://github.com/ballerina-platform/ballerina-library/issues/5714 --- s3/client.bal | 37 +++++++++++++++++++++++++++++-------- s3/utils.bal | 7 ++++++- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/s3/client.bal b/s3/client.bal index ba4242d..b5e7bdd 100644 --- a/s3/client.bal +++ b/s3/client.bal @@ -19,6 +19,7 @@ import ballerina/http; import ballerina/io; import ballerina/regex; import ballerinax/'client.config; +import ballerina/xmldata; # Ballerina Amazon S3 connector provides the capability to access AWS S3 API. # This connector lets you to get authorized access to AWS S3 buckets and objects. @@ -61,7 +62,11 @@ public isolated client class Client { check generateSignature(self.accessKeyId, self.secretAccessKey, self.region, GET, SLASH, UNSIGNED_PAYLOAD, requestHeaders); http:Response httpResponse = check self.amazonS3->get(SLASH, requestHeaders); - xml xmlPayload = check httpResponse.getXmlPayload(); + string stringPayload = check httpResponse.getTextPayload(); + xml? xmlPayload = check xmldata:fromJson(stringPayload); + if (xmlPayload is ()) { + return error("Error occurred while parsing the response payload as XML"); + } if (httpResponse.statusCode == http:STATUS_OK) { return getBucketsList(xmlPayload); } @@ -135,7 +140,11 @@ public isolated client class Client { requestHeaders, queryParams = queryParamsMap); requestURI = string `${requestURI}${queryParamsStr}`; http:Response httpResponse = check self.amazonS3->get(requestURI, requestHeaders); - xml xmlPayload = check httpResponse.getXmlPayload(); + string stringPayload = check httpResponse.getTextPayload(); + xml? xmlPayload = check xmldata:fromJson(stringPayload); + if (xmlPayload is ()) { + return error("Error occurred while parsing the response payload as XML"); + } if (httpResponse.statusCode == http:STATUS_OK) { return getS3ObjectsList(xmlPayload); } @@ -171,7 +180,11 @@ public isolated client class Client { } return httpResponse.getByteStream(); } else { - xml xmlPayload = check httpResponse.getXmlPayload(); + string stringPayload = check httpResponse.getTextPayload(); + xml? xmlPayload = check xmldata:fromJson(stringPayload); + if (xmlPayload is ()) { + return error("Error occurred while parsing the response payload as XML"); + } return error(xmlPayload.toString()); } } @@ -383,11 +396,15 @@ public isolated client class Client { requestURI = string `${requestURI}${queryParamStr}`; http:Response httpResponse = check self.amazonS3->post(requestURI, request); - xml XMLPayload = check httpResponse.getXmlPayload(); + string stringPayload = check httpResponse.getTextPayload(); + xml? xmlPayload = check xmldata:fromJson(stringPayload); + if (xmlPayload is ()) { + return error("Error occurred while parsing the response payload as XML"); + } if httpResponse.statusCode == http:STATUS_OK { - return getUploadId(XMLPayload); + return getUploadId(xmlPayload); } else { - return error(XMLPayload.toString()); + return error(xmlPayload.toString()); } } @@ -441,8 +458,12 @@ public isolated client class Client { string ETag = check httpResponse.getHeader("ETag"); return {partNumber, ETag}; } else { - xml XMLPayload = check httpResponse.getXmlPayload(); - return error(XMLPayload.toString()); + string stringPayload = check httpResponse.getTextPayload(); + xml? xmlPayload = check xmldata:fromJson(stringPayload); + if (xmlPayload is ()) { + return error("Error occurred while parsing the response payload as XML"); + } + return error(xmlPayload.toString()); } } diff --git a/s3/utils.bal b/s3/utils.bal index 85c00c3..460ff4e 100644 --- a/s3/utils.bal +++ b/s3/utils.bal @@ -22,6 +22,7 @@ import ballerina/lang.array; import ballerina/regex; import ballerina/time; import ballerina/url; +import ballerina/xmldata; isolated function generateSignature(string accessKeyId, string secretAccessKey, string region, string httpVerb, string requestURI, string payload, map headers, http:Request? request = (), @@ -403,7 +404,11 @@ isolated function populateUploadPartHeaders(map requestHeaders, UploadPa isolated function handleHttpResponse(http:Response httpResponse) returns @tainted error? { int statusCode = httpResponse.statusCode; if (statusCode != http:STATUS_OK && statusCode != http:STATUS_NO_CONTENT) { - xml xmlPayload = check httpResponse.getXmlPayload(); + string stringPayload = check httpResponse.getTextPayload(); + xml? xmlPayload = check xmldata:fromJson(stringPayload); + if (xmlPayload is ()) { + return error("Error occurred while parsing the response payload as XML"); + } return error(xmlPayload.toString()); } }