3 Commits bea53a39a7 ... 3285e2b3d5

Author SHA1 Message Date
  Adam 3285e2b3d5 send departures response in Bare 2 months ago
  Adam 568fd84539 handle errors in HTTP api 2 months ago
  Adam bea53a39a7 handle errors in HTTP api 2 months ago
2 changed files with 56 additions and 21 deletions
  1. 5 0
      api/structs/structs.go
  2. 51 21
      server/router.go

+ 5 - 0
api/structs/structs.go

@@ -31,3 +31,8 @@ type Departure struct {
 	IsRealtime bool
 	StopOrder  string
 }
+
+type SuccessDepartures struct {
+	Success    bool
+	Departures []Departure
+}

+ 51 - 21
server/router.go

@@ -7,8 +7,6 @@ import (
 	"notabug.org/apiote/bimba_server/traffic/feeds"
 	traffic_structs "notabug.org/apiote/bimba_server/traffic/structs"
 
-	"fmt"
-	"io"
 	"log"
 	"net/http"
 	"os"
@@ -34,8 +32,14 @@ func handleStops(w http.ResponseWriter, r *http.Request, feedName string) {
 func handleDepartures(w http.ResponseWriter, r *http.Request, feedName string, cfg config.Config, versions []feeds.Version, codeIndex traffic_structs.FeedCodeIndex, calendar traffic_structs.FeedCalendar) {
 	r.ParseForm()
 	code := traffic_structs.ID(r.Form.Get("code"))
+	if code == "" {
+		sendError(w, r, "code", "EMPTY", 400)
+		return
+	}
 	date := r.Form.Get("date")
 	// todo line := r.Form.Get("line")
+	// todo limit := r.Form.Get("limit")
+	limit := 12
 	versionCode := traffic_structs.Validity("")
 	departuresType := "full"
 	if date == "" {
@@ -43,7 +47,10 @@ func handleDepartures(w http.ResponseWriter, r *http.Request, feedName string, c
 		date = feedNow.Format("20060102")
 		departuresType = "hybrid"
 	}
-	feedTime, _ := time.ParseInLocation("20060102", date, cfg.EnabledFeeds[feedName].GetLocation())
+	feedTime, err := time.ParseInLocation("20060102", date, cfg.EnabledFeeds[feedName].GetLocation())
+	if err != nil {
+		sendError(w, r, "date", date, 400)
+	}
 	for _, v := range versions {
 		if !v.ValidFrom.After(feedTime) && !feedTime.After(v.ValidTill) {
 			versionCode = traffic_structs.Validity(v.String())
@@ -51,7 +58,7 @@ func handleDepartures(w http.ResponseWriter, r *http.Request, feedName string, c
 	}
 
 	if versionCode == "" {
-		send404(w, r, "date", date)
+		sendError(w, r, "date", date, 404)
 		return
 	}
 
@@ -60,37 +67,60 @@ func handleDepartures(w http.ResponseWriter, r *http.Request, feedName string, c
 
 	stopCodePresent := ix[code]
 	if !stopCodePresent.Present {
-		send404(w, r, "code", string(code))
+		sendError(w, r, "code", string(code), 404)
 		return
 	}
 
 	departures := traffic.GetDepartures(code, cfg.FeedsPath, feedName, versionCode, ix, cal, date, departuresType)
-
-	// this is only temporary
-	now := time.Now()
-	for _, departure := range departures {
-		departureTime := time.Date(now.Year(), now.Month(), now.Day(), int(departure.Time.Hour), int(departure.Time.Minute), int(departure.Time.Second), 0, now.Location())
-		dayOffset, _ := time.ParseDuration(fmt.Sprintf("%dh", departure.Time.DayOffset*24))
-		departureTime = departureTime.Add(dayOffset)
-		timeTo := int(departureTime.Sub(now).Minutes())
-		line := fmt.Sprintf("%s -> %s @ %02d:%02d:%02d (%d) RT:%v Status:%v\n", departure.Line, departure.Headsign, departure.Time.Hour, departure.Time.Minute, departure.Time.Second, timeTo, departure.IsRealtime, departure.Status)
-		io.WriteString(w, line)
+	limitHere := limit
+	if len(departures) < limit {
+		limitHere = len(departures)
 	}
-
+	departures = departures[:limitHere]
+	response := api_structs.SuccessDepartures {
+		Success: true,
+		Departures: departures,
+	}
+	bytes, err := bare.Marshal(&response)
+	if err != nil {
+		send500(w, r, "while marshaling departures")
+		return
+	}
+	w.Write(bytes)
 }
 
-func send404(w http.ResponseWriter, r *http.Request, id, value string) {
+func sendError(w http.ResponseWriter, r *http.Request, id, value string, code int) {
+	message := ""
+	switch code {
+	case 400:
+		message = value + " not valid as " + id
+	case 404:
+		message = value + " not found as " + id
+	}
 	response := api_structs.ErrorResponse{
 		Success: false,
 		Field:   id,
-		Message: value + " not found as " + id,
+		Message: message,
 	}
 	b, err := bare.Marshal(&response)
 	if err != nil {
 		w.WriteHeader(500)
 		return
 	}
-	w.WriteHeader(404)
+	w.WriteHeader(code)
+	w.Write(b)
+}
+
+func send500(w http.ResponseWriter, r *http.Request, message string) {
+	response := api_structs.ErrorResponse{
+		Success: false,
+		Message: message,
+	}
+	w.WriteHeader(500)
+	b, err := bare.Marshal(&response)
+	if err != nil {
+		return
+	}
 	w.Write(b)
 }
 
@@ -105,7 +135,7 @@ func Route(cfg config.Config, codeIndexes *traffic_structs.GlobalCodeIndex, cale
 			feedName := path[0]
 			v := *versions
 			if v[feedName] == nil {
-				send404(w, r, "feed", feedName)
+				sendError(w, r, "feed", feedName, 404)
 				return
 			}
 			if len(path) == 1 {
@@ -120,7 +150,7 @@ func Route(cfg config.Config, codeIndexes *traffic_structs.GlobalCodeIndex, cale
 				case "departures":
 					handleDepartures(w, r, feedName, cfg, v[feedName], ix[feedName], c[feedName])
 				default:
-					send404(w, r, "resource", resource)
+					sendError(w, r, "resource", resource, 404)
 				}
 			}
 		}