mirror of
https://github.com/Hestia-Homes/assessment-model.git
synced 2026-06-08 11:37:25 +00:00
implemented api for search logic
This commit is contained in:
parent
a4f6b00a1c
commit
a9fe146cdf
3 changed files with 67 additions and 29 deletions
|
|
@ -1,19 +0,0 @@
|
|||
export async function getEPCData(postcode: string) {
|
||||
const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`;
|
||||
|
||||
const headers = {
|
||||
Accept: "application/json",
|
||||
Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(url, { headers });
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
return await response.json();
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch EPC data:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
58
src/app/api/search/route.ts
Normal file
58
src/app/api/search/route.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
// export async function getEPCData(postcode: string) {
|
||||
// const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`;
|
||||
|
||||
// const headers = {
|
||||
// Accept: "application/json",
|
||||
// Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
|
||||
// };
|
||||
|
||||
// console.log("Fetching EPC data from:", headers);
|
||||
|
||||
// try {
|
||||
// const response = await fetch(url, { headers });
|
||||
// if (!response.ok) {
|
||||
// throw new Error(`HTTP error! status: ${response.status}`);
|
||||
// }
|
||||
// return await response.json();
|
||||
// } catch (error) {
|
||||
// console.error("Failed to fetch EPC data:", error);
|
||||
// throw error;
|
||||
// }
|
||||
// }
|
||||
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
const postcode = request.nextUrl.searchParams.get("postcode");
|
||||
|
||||
// Check if the postcode is provided
|
||||
if (!postcode) {
|
||||
return new NextResponse("Postcode parameter is missing", { status: 400 });
|
||||
}
|
||||
|
||||
const headers = {
|
||||
Accept: "application/json",
|
||||
Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
|
||||
};
|
||||
|
||||
try {
|
||||
const url = `https://epc.opendatacommunities.org/api/v1/domestic/search?postcode=${postcode}&size=1000`;
|
||||
|
||||
const headers = {
|
||||
Accept: "application/json",
|
||||
Authorization: `Basic ${process.env.EPC_AUTH_TOKEN ?? ""}`,
|
||||
};
|
||||
|
||||
const response = await fetch(url, {
|
||||
headers: headers,
|
||||
next: { revalidate: 60 },
|
||||
});
|
||||
const data = await response.json();
|
||||
|
||||
// Handle the response from the external API and return it
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
return NextResponse.json({ message: "Internal server error", status: 500 });
|
||||
}
|
||||
}
|
||||
|
|
@ -34,18 +34,17 @@ export default function Search() {
|
|||
return;
|
||||
};
|
||||
|
||||
const fetchData = async () => {
|
||||
const response = await fetch(`/api/addresses/${postcode}`);
|
||||
console.log(data);
|
||||
|
||||
if (!response.ok) {
|
||||
// handle error
|
||||
console.error("An error occurred while fetching data");
|
||||
return;
|
||||
async function fetchData() {
|
||||
try {
|
||||
const response = await fetch(`/api/search?postcode=${postcode}`);
|
||||
const data = await response.json();
|
||||
setData(data.message);
|
||||
} catch (error) {
|
||||
console.error("Error fetching data:", error);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
setData(data);
|
||||
};
|
||||
}
|
||||
|
||||
const submitProps = {
|
||||
buttonDisabled: buttonDisabled,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue