-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
335 lines (269 loc) · 10 KB
/
main.go
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
"context"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
)
/*
* readRpaForecast reads in the XML returned from the RPA Luas forecast API and converts it into a struct of type rpaForecastModel.
* It returns an instance of this rpaForecastModel.
*/
func readRpaForecast(reader io.Reader) (rpaForecastModel, error) {
var rpaForecast rpaForecastModel
if err := xml.NewDecoder(reader).Decode(&rpaForecast); err != nil {
return rpaForecast, err
}
return rpaForecast, nil
}
/*
* readRpaFareCalc reads in the XML returned from the RPA Luas fares API and converts it into a struct of type rpaFareCalcModel.
* It returns an instance of this rpaFareCalcModel.
*/
func readRpaFareCalc(reader io.Reader) (rpaFareCalcModel, error) {
var rpaFareCalc rpaFareCalcModel
if err := xml.NewDecoder(reader).Decode(&rpaFareCalc); err != nil {
return rpaFareCalc, err
}
return rpaFareCalc, nil
}
/*
* createGoLuasForecast converts an rpaForecastModel object into a goLuasForecastModel object. The GoLuas forecast model is
* different in structure to that used in the RPA API.
* It returns a GoLuas forecast.
*/
func createGoLuasForecast(rpaForecast rpaForecastModel) goLuasForecastModel {
forecast := goLuasForecastModel{}
forecast.Message = rpaForecast.Message
forecast.Status.Inbound.Message = rpaForecast.Directions[0].StatusMessage
forecast.Status.Inbound.ForecastsEnabled = rpaForecast.Directions[0].ForecastsEnabled
forecast.Status.Inbound.OperatingNormally = rpaForecast.Directions[0].OperatingNormally
forecast.Status.Outbound.Message = rpaForecast.Directions[1].StatusMessage
forecast.Status.Outbound.ForecastsEnabled = rpaForecast.Directions[1].ForecastsEnabled
forecast.Status.Outbound.OperatingNormally = rpaForecast.Directions[1].OperatingNormally
/*
* directionIndex 0: Inbound tram.
* directionIndex 1: Outbound tram.
*/
for directionIndex := 0; directionIndex <= 1; directionIndex++ {
var direction string
switch directionIndex {
case 0:
direction = "Inbound"
case 1:
direction = "Outbound"
default:
panic(errors.New("direction is neither Inbound nor Outbound"))
}
for tramIndex := range rpaForecast.Directions[directionIndex].Trams {
if rpaForecast.Directions[directionIndex].Trams[tramIndex].DueMins != "" {
tram := goLuasTramModel{}
tram.Direction = direction
tram.DueMinutes = rpaForecast.Directions[directionIndex].Trams[tramIndex].DueMins
tram.Destination = rpaForecast.Directions[directionIndex].Trams[tramIndex].Destination
forecast.Trams = append(forecast.Trams, tram)
}
}
}
return forecast
}
/*
* getStop performs a simple DynamoDB query, looking for a stop ID and returning the corresponding document with all stop details.
* It returns a stopModel and any errors collected during the DynamoDB query process.
*/
func getStop(stopID string) (stopModel, error) {
awsSession, err := session.NewSession(&aws.Config{
Region: aws.String(awsRegion),
})
dynamoDBService := dynamodb.New(awsSession)
result, err := dynamoDBService.GetItem(&dynamodb.GetItemInput{
TableName: aws.String(dynamoDBTable),
Key: map[string]*dynamodb.AttributeValue{
"shortName": {
S: aws.String(stopID),
},
},
})
if err != nil {
msg := fmt.Sprintf("Error getting item from DynamoDB table with shortName:%s", stopID)
log.Printf("%s %s: %s", logPrefixError, msg, err.Error())
}
stop := stopModel{}
err = dynamodbattribute.UnmarshalMap(result.Item, &stop)
if err != nil {
msg := "Failed to unmarshal record"
log.Printf("%s %s: %s", logPrefixError, msg, err)
return stopModel{}, err
}
return stop, nil
}
/*
* getStopForecast requests a forecast from the RPA API for a given stop ID and converts it into a GoLuas forecast JSON oject.
* It returns a JSON object representing a GoLuas forecast model, and any errors encountered during the process.
*/
func getStopForecast(rpaForecastURL, stopID string) ([]byte, error) {
msg := fmt.Sprintf("Stop forecast requested for param station=%s", stopID)
log.Printf("%s %s", logPrefixInfo, msg)
response, err := http.Get(
fmt.Sprintf(
"%saction=forecast&stop=%s",
rpaForecastURL,
stopID,
),
)
if err != nil {
msg := "Error establishing HTTP connection to RPA API"
log.Printf("%s %s: %s", logPrefixError, msg, err)
} else {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("Error reading response body: %s", err)
}
bodyStr := string(body)
bodyReader := strings.NewReader(bodyStr)
rpaForecast, err := readRpaForecast(bodyReader)
goLuasForecast := createGoLuasForecast(rpaForecast)
goLuasForecastJSON, err := json.Marshal(&goLuasForecast)
return goLuasForecastJSON, nil
}
return nil, err
}
/*
* getFares requests fares data from the RPA API for a given pair of stops and number of adults and children, and converts it into
* JSON.
* It returns a JSON object representing a standard RPA fare calculation, and any errors encountered during the process.
*/
func getFares(rpaForecastURL, farecalcFrom, farecalcTo, farecalcAdults, farecalcChildren string) ([]byte, error) {
msg := fmt.Sprintf(
"Fare calculation requested for params from=%s,to=%s,adults=%s,children=%s",
farecalcFrom, farecalcTo, farecalcAdults, farecalcChildren,
)
log.Printf("%s %s", logPrefixInfo, msg)
response, err := http.Get(
fmt.Sprintf(
"%saction=farecalc&from=%s&to=%s&adults=%s&children=%s",
rpaForecastURL,
farecalcFrom,
farecalcTo,
farecalcAdults,
farecalcChildren,
),
)
if err != nil {
msg := "Error establishing HTTP connection RPA API"
log.Printf("%s %s: %s", logPrefixError, msg, err)
} else {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
msg := "Error reading response body"
log.Printf("%s %s: %s", logPrefixError, msg, err)
}
bodyStr := string(body)
bodyReader := strings.NewReader(bodyStr)
rpaFareCalc, err := readRpaFareCalc(bodyReader)
/* In the case of a fare calculation, we just want the result (RpaFareCalc.Result). */
rpaFareCalcJSON, err := json.Marshal(&rpaFareCalc.Result)
if err != nil {
msg := "Error marshaling RPA fare calculation to JSON"
log.Printf("%s %s: %s", logPrefixError, msg, err)
} else {
return rpaFareCalcJSON, nil
}
}
return nil, err
}
/*
* createResponse is a simple wrapper function around the events.APIGatewayProxyResponse type. It is called to create a HTTP
* response from GoLuas and accepts an events.APIGatewayProxyRequest type, as well as a response body and status code.
* It returns an events.APIGatewayProxyResponse type.
*/
func createResponse(request events.APIGatewayProxyRequest, body string, statusCode int) events.APIGatewayProxyResponse {
response := events.APIGatewayProxyResponse{
Body: body,
StatusCode: statusCode,
}
requestID := request.RequestContext.RequestID
requestQueryStringParameters := request.QueryStringParameters
responseStatusCode := response.StatusCode
responseBody := response.Body
msg := fmt.Sprintf(
"Responding to request %s for parameters %s with status code %v and response object: %v",
requestID, requestQueryStringParameters, responseStatusCode, responseBody,
)
log.Printf("%s %s", logPrefixInfo, msg)
return response
}
/*
* handleRequest is the primary driver function of GoLuas. It handles a request and routes it to the appropriate function for
* further processing.
* It returns an events.APIGatewayProxyResponse type and an error representing any issues collected in downstream functions.
*/
func handleRequest(ctx context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
requestQueryStringParameters := request.QueryStringParameters
msg := fmt.Sprintf(
"Processing request data for request %s with query string parameters %s",
request.RequestContext.RequestID, requestQueryStringParameters,
)
log.Printf("%s %s", logPrefixInfo, msg)
ver := requestQueryStringParameters["ver"]
action := requestQueryStringParameters["action"]
stopID := requestQueryStringParameters["station"]
from := requestQueryStringParameters["from"]
to := requestQueryStringParameters["to"]
adults := requestQueryStringParameters["adults"]
children := requestQueryStringParameters["children"]
var rpaForecastURL string
if ver == "2" {
rpaForecastURL = rpaForecastURLV2
} else {
rpaForecastURL = rpaForecastURLV1
}
if action == "times" && stopID != "" {
stop, err := getStop(stopID)
if err != nil {
msg := fmt.Sprintf("Error getting stop for param station=%s", stopID)
log.Printf("%s %s: %s", logPrefixError, msg, err)
return createResponse(request, responseMessageGeneralTimesError, 500), err
}
if stop.DisplayName == "" {
msg := fmt.Sprintf("Stop not found for param station=%s", stopID)
log.Printf("%s %s", logPrefixInfo, msg)
return createResponse(request, responseMessageUnknownStop, 404), nil
}
stopForecast, err := getStopForecast(rpaForecastURL, stopID)
stopForecastStr := string(stopForecast)
return createResponse(request, stopForecastStr, 200), nil
} else if action == "farecalc" && from != "" && to != "" && adults != "" && children != "" {
fareCalc, err := getFares(rpaForecastURL, from, to, adults, children)
fareCalcStr := string(fareCalc)
if err != nil {
msg := fmt.Sprintf(
"Error getting fare calculation for params from=%s,to=%s,adults=%s,children=%s",
from, to, adults, children,
)
log.Printf("%s %s: %s", logPrefixError, msg, err)
return createResponse(request, responseMessageGeneralFaresError, 500), err
}
return createResponse(request, fareCalcStr, 200), nil
} else if action == "brewcoffee" { /* Why not? */
return createResponse(request, responseMessageImATeapot, 418), nil
}
return createResponse(request, responseMessageInvalidRequest, 400), nil
}
func main() {
lambda.Start(handleRequest)
}