Skip to content

Commit

Permalink
Merge pull request #96 from MadhukaHarith92/basic-client
Browse files Browse the repository at this point in the history
Add header map to sendReceive and sendOnly functons
  • Loading branch information
madhukaw authored Aug 22, 2023
2 parents 007b34e + 135bb19 commit f44f52a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 9 deletions.
10 changes: 6 additions & 4 deletions ballerina/soap.bal
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ public isolated client class Client {
#
# + body - SOAP request body as an `XML` or `mime:Entity[]` to work with SOAP attachments
# + action - SOAP action as a `string`
# + headers - SOAP headers as a `map<string|string[]>`
# + return - If successful, returns the response. Else, returns an error
remote function sendReceive(xml|mime:Entity[] body, string action) returns xml|mime:Entity[]|Error {
return sendReceive(self.soapVersion, body, self.soapClient, action);
remote function sendReceive(xml|mime:Entity[] body, string action, map<string|string[]> headers = {}) returns xml|mime:Entity[]|Error {
return sendReceive(self.soapVersion, body, self.soapClient, action, headers);
}

# Fires and forgets requests. Sends the request without the possibility of any response from the
Expand All @@ -72,8 +73,9 @@ public isolated client class Client {
#
# + body - SOAP request body as an `XML` or `mime:Entity[]` to work with SOAP attachments
# + action - SOAP action as a `string`
# + headers - SOAP headers as a `map<string|string[]>`
# + return - If successful, returns `nil`. Else, returns an error
remote function sendOnly(xml|mime:Entity[] body, string action) returns Error? {
return sendOnly(self.soapVersion, body, self.soapClient, action);
remote function sendOnly(xml|mime:Entity[] body, string action, map<string|string[]> headers = {}) returns Error? {
return sendOnly(self.soapVersion, body, self.soapClient, action, headers);
}
}
15 changes: 10 additions & 5 deletions ballerina/soap_utils.bal
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import ballerina/mime;
# + soapAction - SOAP action
# + body - SOAP request body as an `XML` or `mime:Entity[]` to work with soap attachments
# + soapVersion - The SOAP version of the request
# + headers - SOAP headers as a `map<string|string[]>`
# + return - The SOAP Request sent as `http:Request`
function createHttpRequest(SoapVersion soapVersion, xml|mime:Entity[] body, string soapAction)
function createHttpRequest(SoapVersion soapVersion, xml|mime:Entity[] body, string soapAction, map<string|string[]> headers = {})
returns http:Request {
http:Request req = new;
if body is xml {
Expand All @@ -43,6 +44,10 @@ returns http:Request {
req.setHeader(mime:CONTENT_TYPE, mediaType.toString());
}
}
foreach string key in headers.keys() {
req.addHeader(key, headers[key].toBalString());
}

return req;
}

Expand All @@ -61,8 +66,8 @@ function createSoapResponse(http:Response response, SoapVersion soapVersion) ret

string path = "";

function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction) returns xml|Error {
http:Request req = createHttpRequest(soapVersion, body, soapAction);
function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction, map<string|string[]> headers = {}) returns xml|Error {
http:Request req = createHttpRequest(soapVersion, body, soapAction, headers);
http:Response response;
do {
response = check httpClient->post(path, req);
Expand All @@ -76,8 +81,8 @@ function sendReceive(SoapVersion soapVersion, xml|mime:Entity[] body, http:Clien
}
}

function sendOnly(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction) returns Error? {
http:Request req = createHttpRequest(SOAP11, body, soapAction);
function sendOnly(SoapVersion soapVersion, xml|mime:Entity[] body, http:Client httpClient, string soapAction, map<string|string[]> headers = {}) returns Error? {
http:Request req = createHttpRequest(SOAP11, body, soapAction, headers);
do {
http:Response _ = check httpClient->post(path, req);
} on fail var err {
Expand Down
42 changes: 42 additions & 0 deletions ballerina/tests/basic_client_test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,45 @@ function testSendOnly12() returns error? {

_ = check soapClient->sendOnly(body, "http://tempuri.org/Add");
}

@test:Config {}
function testSendReceive11WithHeaders() returns error? {
Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL");

xml body = xml `<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;

xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add", {foo: ["bar1", "bar2"]});

xml expected = xml `<soap:Body xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><AddResponse xmlns="http://tempuri.org/"><AddResult>5</AddResult></AddResponse></soap:Body>`;
test:assertEquals(response, expected);
}

@test:Config {}
function testSendReceive12WithHeaders() returns error? {
Client soapClient = check new ("http://www.dneonline.com/calculator.asmx?WSDL", soapVersion = SOAP12);

xml body = xml `<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
<soap:Body>
<quer:Add xmlns:quer="http://tempuri.org/">
<quer:intA>2</quer:intA>
<quer:intB>3</quer:intB>
</quer:Add>
</soap:Body>
</soap:Envelope>`;

xml|mime:Entity[] response = check soapClient->sendReceive(body, "http://tempuri.org/Add", {foo: ["bar1", "bar2"]});

xml expected = xml `<soap:Body xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><AddResponse xmlns="http://tempuri.org/"><AddResult>5</AddResult></AddResponse></soap:Body>`;
test:assertEquals(response, expected);
}

0 comments on commit f44f52a

Please sign in to comment.