Skip to content

Commit

Permalink
Merge pull request #35 from OneBusAway/refactor/sdk-intergration
Browse files Browse the repository at this point in the history
refactor: Update all API fetches to use onebusaway-sdk
  • Loading branch information
aaronbrethorst authored Aug 30, 2024
2 parents 706f226 + c973c9e commit 157a972
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 132 deletions.
5 changes: 5 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {},
"include": ["src"]
}
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
"type": "module",
"dependencies": {
"@fortawesome/free-regular-svg-icons": "^6.6.0",
"onebusaway-sdk": "^0.1.0-alpha.31"
"onebusaway-sdk": "^1.0.1"
}
}
20 changes: 0 additions & 20 deletions src/lib/RestAPI/arrivalsAndDeparturesForStop.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/lib/RestAPI/tripDetails.js

This file was deleted.

13 changes: 6 additions & 7 deletions src/lib/RestAPI/stop.js → src/lib/obaSdk.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { error, json } from '@sveltejs/kit';

import onebusaway from 'onebusaway-sdk';
import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public';
import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private';
import onebusaway from 'onebusaway-sdk';
import { error, json } from '@sveltejs/kit';

const oba = new onebusaway({
baseURL,
apiKey
});

export default async function (stopID) {
const response = await oba.stop.retrieve(stopID);

export function handleOBAResponse(response, entityName) {
if (response.code !== 200) {
error(500, 'Unable to fetch arrivals-and-departures-for-stop.');
return error(500, `Unable to fetch ${entityName}.`);
}

return json(response);
}

export default oba;
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop';
import oba, { handleOBAResponse } from '$lib/obaSdk.js';

/** @type {import('./$types').RequestHandler} */
export async function GET({ params }) {
return arrivalDepartureAPI(params.id);
const stopID = params.id;
const response = await oba.arrivalAndDeparture.list(stopID);
return handleOBAResponse(response, 'arrivals-and-departures-for-stop');
}
24 changes: 5 additions & 19 deletions src/routes/api/oba/shape/[shapeId]/+server.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import { error, json } from '@sveltejs/kit';
import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public';
import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private';
import oba, { handleOBAResponse } from '$lib/obaSdk.js';

/** @type {import('./$types').RequestHandler} */
export async function GET({ params }) {
const { shapeId } = params;
const shapeId = params.shapeId;
const response = await oba.shape.retrieve(shapeId);

let apiURL = `${baseURL}/api/where/shape/${shapeId}.json?key=${apiKey}`;

try {
const response = await fetch(apiURL);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
return json(data);
} catch (err) {
console.error('Error fetching shape data:', err);
throw error(500, 'Unable to fetch shape data.');
}
return handleOBAResponse(response, 'shape');
}
8 changes: 6 additions & 2 deletions src/routes/api/oba/stop/[stopID]/+server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import stopAPI from '$lib/RestAPI/stop';
import oba, { handleOBAResponse } from '$lib/obaSdk';

/** @type {import('./$types').RequestHandler} */
export async function GET({ params }) {
return stopAPI(params.stopID);
const stopID = params.stopID;

const response = await oba.stop.retrieve(stopID);

return handleOBAResponse(response, 'stop');
}
25 changes: 10 additions & 15 deletions src/routes/api/oba/stops-for-location/+server.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import { error, json } from '@sveltejs/kit';

import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public';

import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private';
import oba, { handleOBAResponse } from '$lib/obaSdk';

/** @type {import('./$types').RequestHandler} */
export async function GET({ url }) {
const lat = url.searchParams.get('lat');
const lng = url.searchParams.get('lng');
const apiURL = `${baseURL}/api/where/stops-for-location.json?key=${apiKey}&lat=${lat}&lon=${lng}`;
const response = await fetch(apiURL);
const lat = +url.searchParams.get('lat');
const lng = +url.searchParams.get('lng');

const queryParams = {
lat: lat,
lon: lng
};

if (!response.ok) {
error(500, 'Unable to fetch stops-for-location.');
return;
}
const response = await oba.stopsForLocation.list(queryParams);

const data = await response.json();
return json(data);
return handleOBAResponse(response, 'stops-for-location');
}
44 changes: 14 additions & 30 deletions src/routes/api/oba/trip-details/[tripId]/+server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { error, json } from '@sveltejs/kit';
import { PUBLIC_OBA_SERVER_URL as baseURL } from '$env/static/public';
import { PRIVATE_OBA_API_KEY as apiKey } from '$env/static/private';
import oba, { handleOBAResponse } from '$lib/obaSdk.js';

export async function GET({ params, url }) {
const { tripId } = params;
Expand All @@ -11,34 +9,20 @@ export async function GET({ params, url }) {
const includeStatus = url.searchParams.get('includeStatus') || 'true';
const time = url.searchParams.get('time');

let apiURL = `${baseURL}/api/where/trip-details/${tripId}.json?key=${apiKey}`;
const queryParams = {
includeTrip,
includeSchedule,
includeStatus
};

if (serviceDate) apiURL += `&serviceDate=${serviceDate}`;
apiURL += `&includeTrip=${includeTrip}`;
apiURL += `&includeSchedule=${includeSchedule}`;
apiURL += `&includeStatus=${includeStatus}`;
if (time) apiURL += `&time=${time}`;

try {
const response = await fetch(apiURL);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();

// Ensure stops are included in the references
if (!data.data.references || !data.data.references.stops) {
// If stops are not included, we might need to fetch them separately
// This is a placeholder for that logic
data.data.references = data.data.references || {};
data.data.references.stops = []; // Fetch stops here if needed
}
if (time) {
queryParams.time = time;
}

return json(data);
} catch (err) {
console.error('Error fetching trip details:', err);
throw error(500, 'Unable to fetch trip details.');
if (serviceDate) {
queryParams.serviceDate = serviceDate;
}
const response = await oba.tripDetails.retrieve(tripId, queryParams);

return handleOBAResponse(response, 'trip-details');
}
17 changes: 11 additions & 6 deletions src/routes/stops/[stopID]/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import stopAPI from '$lib/RestAPI/stop.js';
import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop.js';
import oba, { handleOBAResponse } from '$lib/obaSdk.js';

export async function load({ params }) {
const stopID = params.stopID;
const stopResponse = await stopAPI(stopID);
const stopBody = await stopResponse.json();
const arrivalsAndDeparturesResponse = await arrivalDepartureAPI(stopID);
const arrivalsAndDeparturesResponseJSON = await arrivalsAndDeparturesResponse.json();

const stopResponse = await oba.stop.retrieve(stopID);
const stopBody = await handleOBAResponse(stopResponse, 'stop').json();

const arrivalsAndDeparturesResponse = await oba.arrivalAndDeparture.list(stopID);

const arrivalsAndDeparturesResponseJSON = await handleOBAResponse(
arrivalsAndDeparturesResponse,
'arrivals-and-departures-for-stop'
).json();

return {
stopID: params.stopID,
Expand Down
3 changes: 2 additions & 1 deletion svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ const config = {
adapter: adapter(),
alias: {
$components: './src/components',
$images: './src/assets/images'
$images: './src/assets/images',
$lib: './src/lib'
}
},
preprocess: vitePreprocess()
Expand Down

0 comments on commit 157a972

Please sign in to comment.