-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Update all API fetches to use onebusaway-sdk #35
Changes from 15 commits
536c4a4
124499f
dc65dad
e263243
f2d119e
47ecf1d
3a43aac
1ff8e57
626330c
96d7e32
091675d
628ebff
9c16162
e6218eb
5a5d525
8c5d4f5
df6329b
aaf57a6
f4e4161
176224d
966099c
d8ee6d4
cd7bacc
46a33b1
366fe9e
883bf46
ead98f8
5422d78
d9fb334
2b4e53d
17b0671
a82174e
8a294bf
c973c9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import oba from '../obaSdk'; | ||
import { error, json } from '@sveltejs/kit'; | ||
export default async function shape(shapeId) { | ||
const response = await oba.shape.retrieve(shapeId); | ||
|
||
if (response.code !== 200) { | ||
return error(500, 'Unable to fetch shape.'); | ||
} | ||
|
||
return json(response); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import oba from '../obaSdk'; | ||
import { error, json } from '@sveltejs/kit'; | ||
export default async function stopsForLocation(queryParams) { | ||
const response = await oba.stopsForLocation.list(queryParams); | ||
|
||
if (response.code !== 200) { | ||
error(500, 'Unable to fetch stops-for-location.'); | ||
} | ||
|
||
return json(response); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,10 @@ | ||
import oba from '../obaSdk'; | ||
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'; | ||
|
||
export async function GET({ params, url }) { | ||
const { tripId } = params; | ||
const vehicleId = url.searchParams.get('vehicleId'); | ||
const serviceDate = url.searchParams.get('serviceDate'); | ||
|
||
let apiURL = `${baseURL}/api/where/trip-details/${tripId}.json?key=${apiKey}`; | ||
|
||
if (vehicleId) apiURL += `&vehicleId=${vehicleId}`; | ||
if (serviceDate) apiURL += `&serviceDate=${serviceDate}`; | ||
|
||
const response = await fetch(apiURL); | ||
|
||
if (!response.ok) { | ||
export default async function tripDetails(tripID, queryParams) { | ||
const response = await oba.tripDetails.retrieve(tripID, queryParams); | ||
if (response.code !== 200) { | ||
error(500, 'Unable to fetch trip-details.'); | ||
return; | ||
} | ||
|
||
const data = await response.json(); | ||
return json(data); | ||
return json(response); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
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'; | ||
|
||
const oba = new onebusaway({ | ||
baseURL, | ||
apiKey | ||
}); | ||
|
||
export default oba; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop'; | ||
import arrivalsAndDeparturesForStop from '$lib/RestAPI/arrivalsAndDeparturesForStop'; | ||
|
||
/** @type {import('./$types').RequestHandler} */ | ||
|
||
export async function GET({ params }) { | ||
return arrivalDepartureAPI(params.id); | ||
const stopID = params.id; | ||
return arrivalsAndDeparturesForStop(stopID); | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,6 @@ | ||
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 shape from '../../../../../lib/RestAPI/shape.js'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Check out the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I was using the $lib alias, but I noticed that when I used it, the autocomplete and types for oba disappeared. So, I switched to a relative path as a temporary solution. However, after reading the docs, I think we can add a jsconfig.json file to define $lib, so the compiler will recognize it properly. |
||
export async function GET({ params }) { | ||
const { shapeId } = params; | ||
|
||
let apiURL = `${baseURL}/api/where/shape/${shapeId}.json?key=${apiKey}`; | ||
|
||
try { | ||
const response = await fetch(apiURL); | ||
console.log('response:', response); | ||
|
||
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.'); | ||
} | ||
const shapeId = params.shapeId; | ||
return shape(shapeId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not certain that the abstraction of having |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import stopAPI from '$lib/RestAPI/stop'; | ||
|
||
/** @type {import('./$types').RequestHandler} */ | ||
|
||
import stop from '../../../../../lib/RestAPI/stop'; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the $lib alias again, but import obaSdk and use that directly. |
||
export async function GET({ params }) { | ||
return stopAPI(params.stopID); | ||
const stopID = params.stopID; | ||
return stop(stopID); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,14 @@ | ||
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 stopsForLocation from '../../../../lib/RestAPI/stops-for-location.js'; | ||
|
||
/** @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'); | ||
|
||
if (!response.ok) { | ||
error(500, 'Unable to fetch stops-for-location.'); | ||
return; | ||
} | ||
const queryParams = { | ||
lat: lat, | ||
lon: lng | ||
}; | ||
|
||
const data = await response.json(); | ||
return json(data); | ||
return stopsForLocation(queryParams); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import obaSdk and use that directly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I replied in the last review about the options we have. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import stopAPI from '$lib/RestAPI/stop.js'; | ||
import arrivalDepartureAPI from '$lib/RestAPI/arrivalsAndDeparturesForStop.js'; | ||
import arrivalsAndDeparturesForStop from '../../../lib/RestAPI/arrivalsAndDeparturesForStop.js'; | ||
import stop from '../../../lib/RestAPI/stop.js'; | ||
|
||
export async function load({ params }) { | ||
const stopID = params.stopID; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this function will be a lot clearer if we use your obaSdk abstraction directly, instead of the layer on top of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was considering deleting the RestApi folder and using the SDK directly in the api/routes folder. Actually, I initially implemented it this way but reverted back because I noticed that src/routes/stops uses some functions from RestApi. If we want to use the SDK directly without any level of abstraction, we will need to check response.code !== 200 and handle errors in every file. So, I think the abstraction is beneficial for handling errors and other repetitive tasks. What do you think? To sum up the current options: 1 - Delete the RestApi folder and use the SDK directly in src/routes, adding error handling in every fetch throughout the app. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, we can use abstractions as level of error handling and make it reusable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a fair point. I spent some time thinking about this, and I think that a good middle ground would be to add a new function to obaSdk.js called
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a great solution. I was thinking that we could add a pattern and use it across all API fetching. Here's what I will do: 1- Complex APIs will have their logic separated into the RestApi folder. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have added the new modifications and removed the RestApi folder. |
||
const stopResponse = await stopAPI(stopID); | ||
const stopResponse = await stop(stopID); | ||
const stopBody = await stopResponse.json(); | ||
const arrivalsAndDeparturesResponse = await arrivalDepartureAPI(stopID); | ||
const arrivalsAndDeparturesResponse = await arrivalsAndDeparturesForStop(stopID); | ||
const arrivalsAndDeparturesResponseJSON = await arrivalsAndDeparturesResponse.json(); | ||
|
||
return { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import obaSdk and use that directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I replied in the last review about the options we have.