Help with GTMetrix Download PDF Report

Hello!

I’m trying to create a Workflow that I can run a test with GTMetrix and save the reports in my Google Drive but I’m struggling with an HTTP authentication they require for that.

I’ve successfully configured the trigger and actions of GTMetrix run a test and report.

I’ve added an Google Drive action of ‘Upload file’ so I can provide an URL to send this file to my Google Drive.

GTMetrix report provides an URL like this “https://gtmetrix.com/api/2.0/reports/MY_REPORT_ID/resources/report.pdf” so I could use it on Google Drive step. But when we access this URL, it requires an authentication.

I know I could use this URL to authenticate: “https://USERNAME:PASSWORD@gtmetrix.com/api/2.0/reports/******/resources/report.pdf” and this would work.

But how could I modify this field I’ve got in the Pipedream Workflow to use in the action?

I appreciate any suggestion. Thank you.

Hi @rafael,

To achieve the task of downloading a GTMetrix report and uploading it to Google Drive with the required authentication, you can use a custom code step in your Pipedream workflow. This step will handle the authentication to GTMetrix, download the report, and then upload it to Google Drive. Here’s how you can do it:

  1. Download the GTMetrix report using HTTP authentication: You’ll first need to download the report from GTMetrix using the provided URL and your GTMetrix API key for authentication.

  2. Upload the downloaded file to Google Drive: Once you have the report, you can then upload it to Google Drive using the Google Drive API.

Here’s an example of how you can structure this custom code step in your workflow:

import { axios } from "@pipedream/platform";
import fs from "fs";
import path from "path";

export default defineComponent({
  props: {
    gtmetrix: {
      type: "app",
      app: "gtmetrix",
    },
    google: {
      type: "app",
      app: "google",
    },
  },
  async run({ steps, $ }) {
    // Define the GTMetrix report URL
    const reportUrl = `https://gtmetrix.com/api/2.0/reports/MY_REPORT_ID/resources/report.pdf`;

    // Download the GTMetrix report
    const reportResponse = await axios($, {
      url: reportUrl,
      method: "GET",
      responseType: "arraybuffer",
      auth: {
        username: `${this.gtmetrix.$auth.api_key}`,
        password: ``,
      },
    });

    // Save the report to a temporary file
    const tempFilePath = path.join("/tmp", "gtmetrix_report.pdf");
    fs.writeFileSync(tempFilePath, reportResponse, "binary");

    // Upload the report to Google Drive
    const driveResponse = await axios($, {
      method: "POST",
      url: "https://www.googleapis.com/upload/drive/v3/files?uploadType=media",
      headers: {
        Authorization: `Bearer ${this.google.$auth.oauth_access_token}`,
        "Content-Type": "application/pdf",
      },
      params: {
        name: "gtmetrix_report.pdf",
      },
      data: fs.createReadStream(tempFilePath),
    });

    return driveResponse.data;
  },
});

Note: This example assumes you

I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double-check my answers and code! Learn how to build your own.