-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
53 lines (42 loc) · 1.25 KB
/
handlers.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
package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
)
func EstimateHandler(w http.ResponseWriter, r *http.Request) {
estimateRequest := &EstimateRequest{}
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, estimateRequest); err != nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(422)
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
startAddress := estimateRequest.StartAddress
startAddress.Geocode()
endAddress := estimateRequest.EndAddress
endAddress.Geocode()
estimateResponse := &EstimateResponse{}
for _, u := range GetUberEstimates(&startAddress, &endAddress).Prices {
e := Estimate{u.LocalizedDisplayName, u.Estimate}
estimateResponse.AddUberEstimate(e)
}
for _, l := range GetLyftEstimates(&startAddress, &endAddress).CostEstimates {
e := Estimate{l.DisplayName, formatLyftEstimate(l.EstimatedCostCentsMin, l.EstimatedCostCentsMax)}
estimateResponse.AddLyftEstimate(e)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(estimateResponse); err != nil {
panic(err)
}
}