import { parse } from "@libs/xml"; import logger from "@lib/logger.ts"; const TOKEN = Deno.env.get("API_TOKEN"); const RequestorRef = "Caroster.io"; const apiFetch = async ( query: string ): Promise<{ response: Response; result: any }> => { const headers = new Headers(); headers.append("Authorization", `Bearer ${TOKEN}`); headers.append("Content-Type", "application/xml"); const response = await fetch("https://api.opentransportdata.swiss/ojp20", { headers, method: "POST", body: query, }); const xml = await response.text(); try { const result = parse(xml); return { response, result }; } catch (error) { logger.error(xml); logger.error(error); return { response, result: null }; } }; // Doc: https://opentransportdata.swiss/fr/cookbook/stopeventservice/ export const getStopEventService = ( stopPlaceRef: string, depArrTime: Date = new Date() ) => apiFetch(` ${new Date().toISOString()} ${RequestorRef} ${new Date().toISOString()} 1220 ${stopPlaceRef} ${depArrTime.toISOString()} 5 departure false false full all `); // Doc: https://opentransportdata.swiss/fr/cookbook/OJPLocationInformationRequest/ export const getLocationInformationRequest = ( textInput?: string, coordinates?: Coordinates ) => apiFetch(` ${new Date().toISOString()} ${RequestorRef} ${new Date().toISOString()} 1220 ${textInput ? "${textInput}" : ""} ${ coordinates ? ` ${coordinates.longitude} ${coordinates.latitude} ` : "" } stop 5 23009621:2 `);