Skip to content

Commit

Permalink
Attempt to switch Prince PDF call from axios to fetch (#2018)
Browse files Browse the repository at this point in the history
Co-authored-by: BearHanded <[email protected]>
  • Loading branch information
benmartin-coforma and BearHanded authored Dec 19, 2023
1 parent cbc362c commit 56235fb
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 627 deletions.
67 changes: 53 additions & 14 deletions services/app-api/handlers/prince/pdf.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,62 @@
import handler from "../../libs/handler-lib";
import { aws4Interceptor } from "aws4-axios";
import axios from "axios";
import { StatusCodes } from "../../utils/constants/constants";
import { URL } from "url";
import { SignatureV4 } from "@smithy/signature-v4";
import { Sha256 } from "@aws-crypto/sha256-js";
import { fetch } from "cross-fetch"; // TODO remove this polyfill once QMR is on Node 18+

export const getPDF = handler(async (event, _context) => {
const interceptor = aws4Interceptor({
region: "us-east-1",
const body = event.body; // will be base64-encoded HTML, like "PGh0bWw..."
if (!body) {
throw new Error("Missing request body");
}

const {
// princeApiHost: hostname, // JUST the host name, no protocol, ex: "my-site.cms.gov"
// princeApiPath: path, // Needs leading slash, ex: "/doc-conv/508html-to-508pdf"
princeUrl,
AWS_ACCESS_KEY_ID: accessKeyId,
AWS_SECRET_ACCESS_KEY: secretAccessKey,
AWS_SESSION_TOKEN: sessionToken,
} = process.env;

if (
princeUrl === undefined ||
accessKeyId === undefined ||
secretAccessKey === undefined ||
sessionToken === undefined
) {
throw new Error("No config found to make request to PDF API");
}

const { hostname, pathname: path } = new URL(princeUrl);

const request = {
method: "POST",
protocol: "https",
hostname,
path,
headers: {
host: hostname, // Prince requires this to be signed
},
body,
};

const signer = new SignatureV4({
service: "execute-api",
region: "us-east-1",
credentials: { accessKeyId, secretAccessKey, sessionToken },
sha256: Sha256,
});

axios.interceptors.request.use(interceptor);
const signedRequest = await signer.sign(request);

try {
const pdf = await axios.post(process.env.princeUrl!, event.body!);
return {
status: StatusCodes.SUCCESS,
body: pdf.data,
};
} catch (err) {
console.log(err);
}
const response = await fetch(`https://${hostname}${path}`, signedRequest);

const base64EncodedPdfData = await response.json();

return {
status: StatusCodes.SUCCESS,
body: base64EncodedPdfData,
};
});
5 changes: 3 additions & 2 deletions services/app-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
"typescript": "^4.6.4"
},
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@smithy/signature-v4": "^2.0.18",
"aws-sdk": "^2.1310.0",
"aws4": "^1.11.0",
"aws4-axios": "^2.4.9",
"axios": "^1.6.0",
"cross-fetch": "^4.0.0",
"jwt-decode": "^3.1.2",
"kafkajs": "^1.16.0",
"prompt-sync": "^4.2.0",
Expand Down
Loading

0 comments on commit 56235fb

Please sign in to comment.