import { parse } from "@libs/xml";
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();
const result = parse(xml);
return { response, result };
};
// 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) =>
apiFetch(`
${new Date().toISOString()}
${RequestorRef}
${new Date().toISOString()}
1220
${textInput}
stop
5
`);