-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from OneBusAway/refactor/sdk-intergration
refactor: Update all API fetches to use onebusaway-sdk
- Loading branch information
Showing
13 changed files
with
68 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"extends": "./.svelte-kit/tsconfig.json", | ||
"compilerOptions": {}, | ||
"include": ["src"] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
6 changes: 4 additions & 2 deletions
6
src/routes/api/oba/arrivals-and-departures-for-stop/[id]/+server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters