From 1564df519667362fdb0ef069e783c926b4ba8722 Mon Sep 17 00:00:00 2001 From: Isaac Sterling Date: Sat, 8 Mar 2025 14:34:48 -0800 Subject: [PATCH 1/2] fixing backend-fetch/polling merge test --- GoNOW/app/scripts/Polling.ts | 4 ++-- backend/index.ts | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/GoNOW/app/scripts/Polling.ts b/GoNOW/app/scripts/Polling.ts index b0a5114..f72a1b9 100644 --- a/GoNOW/app/scripts/Polling.ts +++ b/GoNOW/app/scripts/Polling.ts @@ -1,4 +1,4 @@ -import { RouteRequestBody } from '../../../backend/index'; +import { travelTimeRequestBody } from '../../../backend/index'; import { getNextEvent } from './Event'; import { getMyLocation } from './Geo'; @@ -37,7 +37,7 @@ export const poll = async (): Promise => { return; } else{ - const requestBody: RouteRequestBody = { + const requestBody: travelTimeRequestBody = { event: next_event, coordinates: current_location.coordinates, } diff --git a/backend/index.ts b/backend/index.ts index 13bd6de..c71b631 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -13,9 +13,15 @@ app.use(bodyParser.json()) const apiKey = process.env.API_KEY??''; -export interface RouteRequestBody { +export interface travelTimeRequestBody { event:Event, - coordinates:Coordinates; + coordinates:Coordinates, +} + +interface RouteRequestBody { + origin:Coordinates, + destination:Coordinates, + travelMode: string, } interface AutoscheduleRequestBody { From 4ed1e3cdcc19f00292b9a89355b1f3aac7cabd50 Mon Sep 17 00:00:00 2001 From: Isaac Sterling Date: Sat, 8 Mar 2025 14:39:12 -0800 Subject: [PATCH 2/2] corrections --- GoNOW/app/scripts/Polling.ts | 6 ++-- backend/index.ts | 53 +++++++++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/GoNOW/app/scripts/Polling.ts b/GoNOW/app/scripts/Polling.ts index f72a1b9..bb577c3 100644 --- a/GoNOW/app/scripts/Polling.ts +++ b/GoNOW/app/scripts/Polling.ts @@ -3,7 +3,7 @@ import { getNextEvent } from './Event'; import { getMyLocation } from './Geo'; interface routeResponse { - travelTime: number; + travelTime: number; } export const POLLING_INTERVAL_MIN = 15; // 15 minutes, minimum recommended interval for background fetch is 15 minutes @@ -38,8 +38,8 @@ export const poll = async (): Promise => { } else{ const requestBody: travelTimeRequestBody = { - event: next_event, - coordinates: current_location.coordinates, + event: next_event, + coordinates: current_location.coordinates, } try { diff --git a/backend/index.ts b/backend/index.ts index c71b631..d44c391 100644 --- a/backend/index.ts +++ b/backend/index.ts @@ -15,13 +15,13 @@ const apiKey = process.env.API_KEY??''; export interface travelTimeRequestBody { event:Event, - coordinates:Coordinates, + coordinates:Coordinates; } interface RouteRequestBody { - origin:Coordinates, - destination:Coordinates, - travelMode: string, + origin: Coordinates, + destination: Coordinates, + travelMode: string } interface AutoscheduleRequestBody { @@ -70,6 +70,51 @@ app.get('/api/poll', async (req: Request, res: Response) => { + //TODO + //parse incoming task for location + console.log(req.body) + const { origin, destination, travelMode } = req.body; + + //construct request + const url = 'https://routes.googleapis.com/directions/v2:computeRoutes'; + + const headers = new Headers({ + 'Content-Type': 'application/json', + 'X-Goog-Api-Key': apiKey, + 'X-Goog-FieldMask': '*' + }); + + const body = JSON.stringify({ + origin: { + location: { latLng: { latitude: origin.latitude, longitude: origin.longitude } } + }, + destination: { + location: { latLng: { latitude: destination.latitude, longitude: destination.longitude } } + }, + travelMode: travelMode + }); + + try { + const response = await fetch(url, { + method: 'POST', + headers: headers, + body: body + }); + + if (!response.ok) throw new Error(`HTTP Error: ${String(response.status)}`); + //const responseText = await response.text(); + //console.log('Raw response:', responseText); + const responseData = await response.json(); + console.log('response data', responseData); + res.status(200).send(responseData); + + } catch (error) { + console.error('Error fetching route:', error); + res.status(500).send(''); + } +}); + app.listen(PORT,() => { console.log(`Backend listening on port ${String(PORT)}`);