Skip to content

Commit

Permalink
Get our API key from GCP secret manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
vengroff committed Dec 22, 2024
1 parent 814129c commit d466182
Show file tree
Hide file tree
Showing 4 changed files with 888 additions and 25 deletions.
12 changes: 5 additions & 7 deletions rem-with-nextjs/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import Head from 'next/head'

import { useState } from 'react';
import { FormEvent, useState } from 'react';
import './globals.css';

import serverSavings from './serverSavings';
Expand All @@ -13,8 +13,6 @@ async function clientSavings(address : string, currentFuel : string) {

const expectedSavings = await serverSavings(address, currentFuel);

console.log(`Client savings: ${expectedSavings}`);

return expectedSavings
}

Expand All @@ -33,7 +31,7 @@ function AddressForm() {
const [savings, setSavings] = useState("")
const [hidden, setHidden] = useState(true)

const onFuelChange = (event) => {
const onFuelChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setCurrentFuel(event.target.value)
setHidden(true)
}
Expand All @@ -47,7 +45,7 @@ function AddressForm() {
*
* @param {Event} event - the change event.
*/
const handleSubmit = (event) => {
const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();

const expectedSavings = clientSavings(address, currentFuel);
Expand Down Expand Up @@ -119,8 +117,8 @@ export default function Home() {
</div>
<div id="content">
<div>
We're here to help you save money by electrifying your home. Enter your address and select the fuel you
currently use to heat your home and we'll tell you how much you can save by having us install a heat pump!
We&apos;re here to help you save money by electrifying your home. Enter your address and select the fuel you
currently use to heat your home and we&apos;ll tell you how much you can save by having us install a heat pump!
</div>
<AddressForm>
</AddressForm>
Expand Down
33 changes: 26 additions & 7 deletions rem-with-nextjs/app/serverSavings.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
'use server';

import axios from "axios";
import SecretManagerServiceClient from '@google-cloud/secret-manager';

export default async function serverSavings(address : string, currentFuel: string) {
async function accessRaApiKey() {
// Set the name of the GCP project where you use the secret manager.
const project = 'XXXXXXXXXXXX';

// Set your secret name here. This should refer to a secret you set
// up in the GCP secret manager. See https://cloud.google.com/security/products/secret-manager?hl=en
// for details.
const secretName = 'ra-api-key';

const name = `projects/${project}/secrets/${secretName}/versions/latest`;

const secretManagerClient = new SecretManagerServiceClient.SecretManagerServiceClient();

const [accessResponse] = await secretManagerClient.accessSecretVersion({
name,
});

console.log("On the server side.")
const apiKey = accessResponse.payload.data.toString('utf8');

return apiKey
};

const RA_API_KEY = await accessRaApiKey()

export default async function serverSavings(address : string, currentFuel: string) {

const upgrade = 'high_eff_hp_elec_backup';

// This is the URL for the REM API.
const remApiURL = "https://api.rewiringamerica.org/api/v1/rem/address";

// If you don't have an API key, please register at
// https://rewiring.link/api-signup to get one.
const apiKey = "INSERT_YOUR_API_KEY_HERE";

let expectedSavings = "123";

await axios
.get(
remApiURL,
{
params: {address: address, heating_fuel: currentFuel, upgrade: upgrade},
headers: {Authorization: "Bearer " + apiKey}
headers: {Authorization: "Bearer " + RA_API_KEY}
}
)
.then(
Expand Down
Loading

0 comments on commit d466182

Please sign in to comment.