diff --git a/net/README.md b/net/README.md index 2cbd091..273db30 100644 --- a/net/README.md +++ b/net/README.md @@ -23,7 +23,7 @@ Send an HTTP request, and map the response body and headers into a Map that can - **headers (Text)** - Optional, a JSON object containing key-value pairs, each will be added as headers to the HTTP Request - Output parameters: - **Type**: *[choose one of your application's case types]* - - **Cardinality**: Single or Multiple, depending on what the REST service returns (single json object vs json array of objects) + - **Cardinality**: Single object - Note: JSON Transform rule will be required ### JSON Transform rule configuration @@ -32,13 +32,20 @@ Send an HTTP request, and map the response body and headers into a Map that can - **Name**: the same name as your function (not required, just easier for author) - **Purpose**: Deserialize (JSON to Pega Object) - **Library**: Same as the case type chosen for the Function rule's output parameter **Type** -- **Top level structure**: Same as the cardinality chosen for your Function Rule's output parameter +- **Top level structure**: Single object 2. Add example JSON response: - **System name**: any identifier you want -- **JSON sample**: ```{"responseHeaders":..json object with name=value json elements..},{"responseBody":..json object/array for the expected JSON response}``` +- **JSON sample**: ```{"responseStatus":..json object with name=value json elements..},"responseHeaders":..json object with name=value json elements..},{"responseBody":..json object/array for the expected JSON response}``` - For example, a GET call to https://api.lyrics.ovh/v1/beatles/Yesterday would need to use a JSON sample like this to properly define the JSON transform: ``` { + "responseStatus": { + "protocolMinorVersion": "1", + "reason": "OK", + "protocolName": "HTTP", + "protocolMajorVersion": "1", + "statusCode": "200" + }, "responseBody":{ "lyrics":"Yesterday, \nall my troubles seemed so far away,\r\nNow it looks as though they\u0027re here to stay,\r\nOh I believe in yesterday.\r\nSuddenly, \n\nI\u0027m not half the man I used to be,\n\nThere\u0027s a shadow hanging over me.\n\nOh yesterday came suddenly.\n\n\n\nWhy she had to go? \n\nI don\u0027t know she woldn\u0027t say.\n\nI said something wrong, \n\nnow I long for yesterday.\n\n\n\nYesterday, \n\nlove was such an easy game to play,\n\nNow I need a place to hide away,\n\nOh I believe in yesterday.\n\n\n\nWhy she had to go? \n\nI don\u0027t know she woldn\u0027t say.\n\nI said something wrong, \n\nnow I long for yesterday.\n\n\n\nYesterday, \n\nlove was such an easy game to play,\n\nNow I need a place to hide away,\n\nOh I believe in yesterday.\n\n\n\n\n\n(Thanks to Beatles4ever for correcting these lyrics)" }, diff --git a/net/src/main/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeaders.java b/net/src/main/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeaders.java index 6d10c84..7fa3d37 100644 --- a/net/src/main/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeaders.java +++ b/net/src/main/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeaders.java @@ -88,6 +88,14 @@ public static Response send(Map inputMap) throws IOException { try (CloseableHttpResponse response1 = httpClient.execute(request)) { HttpEntity entity1 = response1.getEntity(); + // Get the response status + CFresponse.responseStatus = new HashMap<>(); + CFresponse.responseStatus.put("statusCode", Integer.toString(response1.getStatusLine().getStatusCode())); + CFresponse.responseStatus.put("reason", response1.getStatusLine().getReasonPhrase()); + CFresponse.responseStatus.put("protocolName", response1.getStatusLine().getProtocolVersion().getProtocol()); + CFresponse.responseStatus.put("protocolMajorVersion", Integer.toString(response1.getStatusLine().getProtocolVersion().getMajor())); + CFresponse.responseStatus.put("protocolMinorVersion", Integer.toString(response1.getStatusLine().getProtocolVersion().getMajor())); + // Get response headers out of response, then construct a map out of them Header[] allHeaders = response1.getAllHeaders(); CFresponse.responseHeaders = new HashMap<>(); diff --git a/net/src/main/java/com/pega/launchpad/net/Response.java b/net/src/main/java/com/pega/launchpad/net/Response.java index 1e70597..f9adb19 100644 --- a/net/src/main/java/com/pega/launchpad/net/Response.java +++ b/net/src/main/java/com/pega/launchpad/net/Response.java @@ -7,6 +7,7 @@ * POJO for a REST response that contains both the response body (as a Map) and the response headers (as a Map of Strings) */ public class Response { + public Map responseStatus; public Object responseBody; public Map responseHeaders; diff --git a/net/src/test/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeadersTest.java b/net/src/test/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeadersTest.java index ecb204c..e22a607 100644 --- a/net/src/test/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeadersTest.java +++ b/net/src/test/java/com/pega/launchpad/net/HttpRequestWithMappedResponseHeadersTest.java @@ -46,4 +46,15 @@ void getLyrics() throws Exception { Response r = HttpRequestWithMappedResponseHeaders.send(inputMap); System.out.println("getLyrics:" + r); } + + @Test + void getLyricsBadResponse() throws Exception { + Map inputMap = new HashMap<>(); + inputMap.put("method", "get"); + inputMap.put("url", "https://api.lyrics.ovh/v1/beatles/Tomorrow"); + inputMap.put("body", "{}"); + Response r = HttpRequestWithMappedResponseHeaders.send(inputMap); + System.out.println("getLyricsBadResponse:" + r); + } + } \ No newline at end of file