Skip to content

Commit

Permalink
Update xml payload retrieving logic from HTTP response
Browse files Browse the repository at this point in the history
# this is to include the improvements suggested in ballerina-platform/ballerina-library#5714
  • Loading branch information
nisuraaa authored and NipunaRanasinghe committed Aug 29, 2024
1 parent 7d5cb95 commit bd6bae6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
37 changes: 29 additions & 8 deletions s3/client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -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());
}
}

Expand Down
7 changes: 6 additions & 1 deletion s3/utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> headers, http:Request? request = (),
Expand Down Expand Up @@ -403,7 +404,11 @@ isolated function populateUploadPartHeaders(map<string> 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());
}
}

0 comments on commit bd6bae6

Please sign in to comment.