Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in http response, return status fields as well #12

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions net/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public static Response send(Map<String, String> 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<>();
Expand Down
1 change: 1 addition & 0 deletions net/src/main/java/com/pega/launchpad/net/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> responseStatus;
public Object responseBody;
public Map<String, String> responseHeaders;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ void getLyrics() throws Exception {
Response r = HttpRequestWithMappedResponseHeaders.send(inputMap);
System.out.println("getLyrics:" + r);
}

@Test
void getLyricsBadResponse() throws Exception {
Map<String, String> 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);
}

}
Loading