Skip to content

Commit a937fde

Browse files
authored
Search and replace policy (#56)
* Added ‘SearchAndReplace’ Apigee policy * Renamed to ‘SearchAndReplaceResponse’ * Fixed typo * Removed any ES6 syntax * Added SearchAndReplaceResponse Apigee policy to sandbox * Added JS file * Updated name * Moved flow in to the TargetEndpoint * Updated the search and replace JS * Debugging basePath and subdomain * Debug context * Debugging proxyRequest and targetRequest * WIP * Added timestamp * WIP * WIP * WIP * WIP * WIP * Using target.name in API host name * Removed switch statement * Using a request.url * Removed targetName * Updated JS * Added check for ‘api.nhs.uk’ too * Added api.nhs.uk * Added a check for ‘api’ * Added debugging for ‘requestUrl’ and ‘environmentSubdomain’ * Moved ‘item’ variable outside the ‘for’ loop * WIP: AssignMessage.CopyRequest * Commented out the ‘CopyRequest’ flow * Using ‘requestHostnameEnv’ in SearchAndReplaceResponse.js * Debugging ‘requestHostnameEnv’ variable * Removed debugging
1 parent cbcb793 commit a937fde

3 files changed

Lines changed: 53 additions & 72 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var response = context.getVariable("response.content");
2+
var json = JSON.parse(response);
3+
4+
if (json.place) {
5+
// Loop over each item in the search result array, re-writing the hostname to that of the initial request
6+
json.place.forEach(value => {
7+
var request_hostname = context.getVariable("request_hostname_env");
8+
// Annoyingly, Apigee's version of JS doesn't support regex look-behinds,
9+
// so we grab the env string including https:// prefix
10+
var hostname_regex = /(^https:\/\/([a-zA-Z\-]+\.)+)nhs\.uk/gm;
11+
var rewritten_url = value.url.replace(hostname_regex, "https://" + request_hostname);
12+
value.url = rewritten_url;
13+
// value.url = value.url.replace("api-version=1", "api-version=2");
14+
// value.url = value.url.replace("service-search", "service-search-api");
15+
});
16+
17+
// Update the response payload with the corrected urls
18+
context.setVariable("message.content", JSON.stringify(json));
19+
}
Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,67 @@
1-
// An example requestUrl would be https://internal-dev-sandbox.apis.ptl.api.platform.nhs.uk/nwca-48/conditions/
2-
var requestUrl = context.getVariable("request.url");
3-
var environmentSubdomain = requestUrl.split("/");
4-
environmentSubdomain = environmentSubdomain[2];
5-
environmentSubdomain = environmentSubdomain.split(".");
6-
environmentSubdomain = environmentSubdomain[0];
7-
8-
var apiVersion2Host;
9-
if (
10-
environmentSubdomain === "api" ||
11-
environmentSubdomain === "apis" ||
12-
environmentSubdomain === "prod"
13-
) {
14-
apiVersion2Host = "api.service.nhs.uk";
15-
} else {
16-
apiVersion2Host = environmentSubdomain + ".api.service.nhs.uk";
17-
}
1+
var requestHostnameEnv = context.getVariable("request_hostname_env");
182

193
var searchAndReplaceStrings = [
204
{
215
searchFor: "www.nhs.uk/conditions/",
22-
replaceWith: apiVersion2Host + "/nhs-website-content/conditions/",
6+
replaceWith: requestHostnameEnv + "/nhs-website-content/conditions/",
237
},
248
{
259
searchFor: "www.nhs.uk/live-well/",
26-
replaceWith: apiVersion2Host + "/nhs-website-content/live-well/",
10+
replaceWith: requestHostnameEnv + "/nhs-website-content/live-well/",
2711
},
2812
{
2913
searchFor: "www.nhs.uk/medicines/",
30-
replaceWith: apiVersion2Host + "/nhs-website-content/medicines/",
14+
replaceWith: requestHostnameEnv + "/nhs-website-content/medicines/",
3115
},
3216
{
3317
searchFor: "www.nhs.uk/nhs-services/",
34-
replaceWith: apiVersion2Host + "/nhs-website-content/nhs-services/",
18+
replaceWith: requestHostnameEnv + "/nhs-website-content/nhs-services/",
3519
},
3620
{
3721
searchFor: "www.nhs.uk/pregnancy/",
38-
replaceWith: apiVersion2Host + "/nhs-website-content/pregnancy/",
22+
replaceWith: requestHostnameEnv + "/nhs-website-content/pregnancy/",
3923
},
4024
{
4125
searchFor: "www.nhs.uk/common-health-questions/",
4226
replaceWith:
43-
apiVersion2Host + "/nhs-website-content/common-health-questions/",
27+
requestHostnameEnv + "/nhs-website-content/common-health-questions/",
4428
},
4529
{
4630
searchFor: "www.nhs.uk/mental-health/",
4731
replaceWith:
48-
apiVersion2Host + "api.service.nhs.uk/nhs-website-content/mental-health/",
32+
requestHostnameEnv +
33+
"api.service.nhs.uk/nhs-website-content/mental-health/",
4934
},
5035
{
5136
searchFor: "api.nhs.uk/conditions/",
52-
replaceWith: apiVersion2Host + "/nhs-website-content/conditions/",
37+
replaceWith: requestHostnameEnv + "/nhs-website-content/conditions/",
5338
},
5439
{
5540
searchFor: "api.nhs.uk/live-well/",
56-
replaceWith: apiVersion2Host + "/nhs-website-content/live-well/",
41+
replaceWith: requestHostnameEnv + "/nhs-website-content/live-well/",
5742
},
5843
{
5944
searchFor: "api.nhs.uk/medicines/",
60-
replaceWith: apiVersion2Host + "/nhs-website-content/medicines/",
45+
replaceWith: requestHostnameEnv + "/nhs-website-content/medicines/",
6146
},
6247
{
6348
searchFor: "api.nhs.uk/nhs-services/",
64-
replaceWith: apiVersion2Host + "/nhs-website-content/nhs-services/",
49+
replaceWith: requestHostnameEnv + "/nhs-website-content/nhs-services/",
6550
},
6651
{
6752
searchFor: "api.nhs.uk/pregnancy/",
68-
replaceWith: apiVersion2Host + "/nhs-website-content/pregnancy/",
53+
replaceWith: requestHostnameEnv + "/nhs-website-content/pregnancy/",
6954
},
7055
{
7156
searchFor: "api.nhs.uk/common-health-questions/",
7257
replaceWith:
73-
apiVersion2Host + "/nhs-website-content/common-health-questions/",
58+
requestHostnameEnv + "/nhs-website-content/common-health-questions/",
7459
},
7560
{
7661
searchFor: "api.nhs.uk/mental-health/",
7762
replaceWith:
78-
apiVersion2Host + "api.service.nhs.uk/nhs-website-content/mental-health/",
63+
requestHostnameEnv +
64+
"api.service.nhs.uk/nhs-website-content/mental-health/",
7965
},
8066
];
8167

@@ -87,9 +73,4 @@ for (var i = 0; i < searchAndReplaceStrings.length; i++) {
8773
responseContent = responseContent.replace(regex, item.replaceWith);
8874
}
8975

90-
responseContent = JSON.parse(responseContent);
91-
responseContent.requestUrl = requestUrl;
92-
responseContent.environmentSubdomain = environmentSubdomain;
93-
responseContent = JSON.stringify(responseContent);
94-
9576
context.setVariable("response.content", responseContent);
Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,67 @@
1-
// An example requestUrl would be https://internal-dev-sandbox.apis.ptl.api.platform.nhs.uk/nwca-48/conditions/
2-
var requestUrl = context.getVariable("request.url");
3-
var environmentSubdomain = requestUrl.split("/");
4-
environmentSubdomain = environmentSubdomain[2];
5-
environmentSubdomain = environmentSubdomain.split(".");
6-
environmentSubdomain = environmentSubdomain[0];
7-
8-
var apiVersion2Host;
9-
if (
10-
environmentSubdomain === "api" ||
11-
environmentSubdomain === "apis" ||
12-
environmentSubdomain === "prod"
13-
) {
14-
apiVersion2Host = "api.service.nhs.uk";
15-
} else {
16-
apiVersion2Host = environmentSubdomain + ".api.service.nhs.uk";
17-
}
1+
var requestHostnameEnv = context.getVariable("request_hostname_env");
182

193
var searchAndReplaceStrings = [
204
{
215
searchFor: "www.nhs.uk/conditions/",
22-
replaceWith: apiVersion2Host + "/nhs-website-content/conditions/",
6+
replaceWith: requestHostnameEnv + "/nhs-website-content/conditions/",
237
},
248
{
259
searchFor: "www.nhs.uk/live-well/",
26-
replaceWith: apiVersion2Host + "/nhs-website-content/live-well/",
10+
replaceWith: requestHostnameEnv + "/nhs-website-content/live-well/",
2711
},
2812
{
2913
searchFor: "www.nhs.uk/medicines/",
30-
replaceWith: apiVersion2Host + "/nhs-website-content/medicines/",
14+
replaceWith: requestHostnameEnv + "/nhs-website-content/medicines/",
3115
},
3216
{
3317
searchFor: "www.nhs.uk/nhs-services/",
34-
replaceWith: apiVersion2Host + "/nhs-website-content/nhs-services/",
18+
replaceWith: requestHostnameEnv + "/nhs-website-content/nhs-services/",
3519
},
3620
{
3721
searchFor: "www.nhs.uk/pregnancy/",
38-
replaceWith: apiVersion2Host + "/nhs-website-content/pregnancy/",
22+
replaceWith: requestHostnameEnv + "/nhs-website-content/pregnancy/",
3923
},
4024
{
4125
searchFor: "www.nhs.uk/common-health-questions/",
4226
replaceWith:
43-
apiVersion2Host + "/nhs-website-content/common-health-questions/",
27+
requestHostnameEnv + "/nhs-website-content/common-health-questions/",
4428
},
4529
{
4630
searchFor: "www.nhs.uk/mental-health/",
4731
replaceWith:
48-
apiVersion2Host + "api.service.nhs.uk/nhs-website-content/mental-health/",
32+
requestHostnameEnv +
33+
"api.service.nhs.uk/nhs-website-content/mental-health/",
4934
},
5035
{
5136
searchFor: "api.nhs.uk/conditions/",
52-
replaceWith: apiVersion2Host + "/nhs-website-content/conditions/",
37+
replaceWith: requestHostnameEnv + "/nhs-website-content/conditions/",
5338
},
5439
{
5540
searchFor: "api.nhs.uk/live-well/",
56-
replaceWith: apiVersion2Host + "/nhs-website-content/live-well/",
41+
replaceWith: requestHostnameEnv + "/nhs-website-content/live-well/",
5742
},
5843
{
5944
searchFor: "api.nhs.uk/medicines/",
60-
replaceWith: apiVersion2Host + "/nhs-website-content/medicines/",
45+
replaceWith: requestHostnameEnv + "/nhs-website-content/medicines/",
6146
},
6247
{
6348
searchFor: "api.nhs.uk/nhs-services/",
64-
replaceWith: apiVersion2Host + "/nhs-website-content/nhs-services/",
49+
replaceWith: requestHostnameEnv + "/nhs-website-content/nhs-services/",
6550
},
6651
{
6752
searchFor: "api.nhs.uk/pregnancy/",
68-
replaceWith: apiVersion2Host + "/nhs-website-content/pregnancy/",
53+
replaceWith: requestHostnameEnv + "/nhs-website-content/pregnancy/",
6954
},
7055
{
7156
searchFor: "api.nhs.uk/common-health-questions/",
7257
replaceWith:
73-
apiVersion2Host + "/nhs-website-content/common-health-questions/",
58+
requestHostnameEnv + "/nhs-website-content/common-health-questions/",
7459
},
7560
{
7661
searchFor: "api.nhs.uk/mental-health/",
7762
replaceWith:
78-
apiVersion2Host + "api.service.nhs.uk/nhs-website-content/mental-health/",
63+
requestHostnameEnv +
64+
"api.service.nhs.uk/nhs-website-content/mental-health/",
7965
},
8066
];
8167

@@ -87,9 +73,4 @@ for (var i = 0; i < searchAndReplaceStrings.length; i++) {
8773
responseContent = responseContent.replace(regex, item.replaceWith);
8874
}
8975

90-
responseContent = JSON.parse(responseContent);
91-
responseContent.requestUrl = requestUrl;
92-
responseContent.environmentSubdomain = environmentSubdomain;
93-
responseContent = JSON.stringify(responseContent);
94-
9576
context.setVariable("response.content", responseContent);

0 commit comments

Comments
 (0)