-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to switch Prince PDF call from axios to fetch (#2018)
Co-authored-by: BearHanded <[email protected]>
- Loading branch information
1 parent
cbc362c
commit 56235fb
Showing
4 changed files
with
186 additions
and
627 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.