api/api.ts (view raw)
1import { parse } from "@libs/xml";
2import logger from "@lib/logger.ts";
3
4const TOKEN = Deno.env.get("API_TOKEN");
5const RequestorRef = "Caroster.io";
6
7const apiFetch = async (
8 query: string
9): Promise<{ response: Response; result: any }> => {
10 const headers = new Headers();
11 headers.append("Authorization", `Bearer ${TOKEN}`);
12 headers.append("Content-Type", "application/xml");
13 const response = await fetch("https://api.opentransportdata.swiss/ojp20", {
14 headers,
15 method: "POST",
16 body: query,
17 });
18 const xml = await response.text();
19 try {
20 const result = parse(xml);
21 return { response, result };
22 } catch (error) {
23 logger.error(xml);
24 logger.error(error);
25 return { response, result: null };
26 }
27};
28
29// Doc: https://opentransportdata.swiss/fr/cookbook/stopeventservice/
30export const getStopEventService = (
31 stopPlaceRef: string,
32 depArrTime: Date = new Date()
33) =>
34 apiFetch(`<?xml version="1.0" encoding="UTF-8"?>
35<OJP xmlns="http://www.vdv.de/ojp" xmlns:siri="http://www.siri.org.uk/siri" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vdv.de/ojp ../../../../OJP4/OJP.xsd">
36 <OJPRequest>
37 <siri:ServiceRequest>
38 <siri:RequestTimestamp>${new Date().toISOString()}</siri:RequestTimestamp>
39 <siri:RequestorRef>${RequestorRef}</siri:RequestorRef>
40 <OJPStopEventRequest>
41 <siri:RequestTimestamp>${new Date().toISOString()}</siri:RequestTimestamp>
42 <siri:MessageIdentifier>1220</siri:MessageIdentifier>
43 <Location>
44 <PlaceRef>
45 <siri:StopPointRef>${stopPlaceRef}</siri:StopPointRef>
46 </PlaceRef>
47 <DepArrTime>${depArrTime.toISOString()}</DepArrTime>
48 </Location>
49 <Params>
50 <NumberOfResults>5</NumberOfResults>
51 <StopEventType>departure</StopEventType>
52 <IncludePreviousCalls>false</IncludePreviousCalls>
53 <IncludeOnwardCalls>false</IncludeOnwardCalls>
54 <UseRealtimeData>full</UseRealtimeData>
55 <IncludeStopHierarchy>all</IncludeStopHierarchy>
56 </Params>
57 </OJPStopEventRequest>
58 </siri:ServiceRequest>
59 </OJPRequest>
60</OJP>`);
61
62// Doc: https://opentransportdata.swiss/fr/cookbook/OJPLocationInformationRequest/
63export const getLocationInformationRequest = (
64 textInput?: string,
65 coordinates?: Coordinates
66) =>
67 apiFetch(`<OJP xmlns="http://www.vdv.de/ojp" xmlns:siri="http://www.siri.org.uk/siri" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.vdv.de/ojp ../../../../Downloads/OJP-changes_for_v1.1%20(1)/OJP-changes_for_v1.1/OJP.xsd">
68 <OJPRequest>
69 <siri:ServiceRequest>
70 <siri:RequestTimestamp>${new Date().toISOString()}</siri:RequestTimestamp>
71 <siri:RequestorRef>${RequestorRef}</siri:RequestorRef>
72 <OJPLocationInformationRequest>
73 <siri:RequestTimestamp>${new Date().toISOString()}</siri:RequestTimestamp>
74 <siri:MessageIdentifier>1220</siri:MessageIdentifier>
75 <InitialInput>
76 ${textInput ? "<Name>${textInput}</Name>" : ""}
77 ${
78 coordinates
79 ? `
80 <GeoPosition>
81 <siri:Longitude>${coordinates.longitude}</siri:Longitude>
82 <siri:Latitude>${coordinates.latitude}</siri:Latitude>
83 </GeoPosition>
84 `
85 : ""
86 }
87 </InitialInput>
88 <Restrictions>
89 <Type>stop</Type>
90 <NumberOfResults>5</NumberOfResults>
91 <TopographicPlaceRef>23009621:2</TopographicPlaceRef>
92 </Restrictions>
93 </OJPLocationInformationRequest>
94 </siri:ServiceRequest>
95 </OJPRequest>
96</OJP>`);