ctucx.git: nimhafas

[nimlang] hafas-client library

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174 
175 
176 
177 
178 
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
import asynchttpserver
import types

proc parseError*(errstr: string): hafasException =
  case errstr:
  of "H_UNKNOWN":
    return hafasException(
      code: SERVER_ERROR,
      message: "unknown internal error",
      statusCode: Http500,
    )
  of "AUTH":
    return hafasException(
      code: ACCESS_DENIED,
      message: "invalid or missing authentication data",
      statusCode: Http401,
    )
  of "R0001":
    return hafasException(
      code: INVALID_REQUEST,
      message: "unknown method",
      statusCode: Http400,
    )
  of "R0002":
    return hafasException(
      code: INVALID_REQUEST,
      message: "invalid or missing request parameters",
      statusCode: Http400,
    )
  of "R0007":
    return hafasException(
      code: SERVER_ERROR,
      message: "internal communication error",
      statusCode: Http500,
    )
  of "R5000":
    return hafasException(
      code: ACCESS_DENIED,
      message: "access denied",
      statusCode: Http401,
    )
  of "S1":
    return hafasException(
      code: SERVER_ERROR,
      message: "journeys search: a connection to the backend server couldn\'t be established",
      statusCode: Http503,
    )
  of "LOCATION":
    return hafasException(
      code: INVALID_REQUEST,
      message: "location/stop not found",
      statusCode: Http400,
    )
  of "H390":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: departure/arrival station replaced",
      statusCode: Http400,
    )
  of "H410":
    return hafasException(
      code: SERVER_ERROR,
      message: "journeys search: incomplete response due to timetable change"
    )
  of "H455":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: prolonged stop",
      statusCode: Http400,
    )
  of "H460":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: stop(s) passed multiple times",
      statusCode: Http400,
    )
  of "H500":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: too many trains, connection is not complete",
      statusCode: Http400,
    )
  of "H890":
    return hafasException(
      code: NOT_FOUND,
      message: "journeys search unsuccessful",
      statusCode: Http404,
    )
  of "H891":
    return hafasException(
      code: NOT_FOUND,
      message: "journeys search: no route found, try with an intermediate stations",
      statusCode: Http404,
    )
  of "H892":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: query too complex, try less intermediate stations",
      statusCode: Http400,
    )
  of "H895":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: departure & arrival are too near",
      statusCode: Http400,
    )
  of "H899":
    return hafasException(
      code: SERVER_ERROR,
      message: "journeys search unsuccessful or incomplete due to timetable change"
    )
  of "H900":
    return hafasException(
      code: SERVER_ERROR,
      message: "journeys search unsuccessful or incomplete due to timetable change"
    )
  of "H9220":
    return hafasException(
      code: NOT_FOUND,
      message: "journeys search: no stations found close to the address",
      statusCode: Http400,
    )
  of "H9230":
    return hafasException(
      code: SERVER_ERROR,
      message: "journeys search: an internal error occured",
      statusCode: Http500,
    )
  of "H9240":
    return hafasException(
      code: NOT_FOUND,
      message: "journeys search unsuccessful",
      statusCode: Http404,
    )
  of "H9250":
    return hafasException(
      code: SERVER_ERROR,
      message: "journeys search: leg query interrupted",
      statusCode: Http500,
    )
  of "H9260":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: unknown departure station",
      statusCode: Http400,
    )
  of "H9280":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: unknown intermediate station",
      statusCode: Http400,
    )
  of "H9300":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: unknown arrival station",
      statusCode: Http400,
    )
  of "H9320":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: the input is incorrect or incomplete",
      statusCode: Http400,
    )
  of "H9360":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: error in a data field",
      statusCode: Http400,
    )
  of "H9380":
    return hafasException(
      code: INVALID_REQUEST,
      message: "journeys search: departure/arrival/intermediate station defined more than once",
      statusCode: Http400,
    )
  of "SQ001":
    return hafasException(
      code: SERVER_ERROR,
      message: "no departures/arrivals data available",
      statusCode: Http503,
    )
  of "SQ005":
    return hafasException(
      code: NOT_FOUND,
      message: "no trips found",
      statusCode: Http404,
    )
  of "TI001":
    return hafasException(
      code: SERVER_ERROR,
      message: "no trip info available",
      statusCode: Http503,
    )
  return hafasException(
    code: SERVER_ERROR,
    message: "unknown HAFAS exception " & errstr,
    statusCode: Http500,
  )