package api import ( "encoding/json" "fmt" "net/http" "net/url" "github.com/gin-gonic/gin" ) type IDFMSchedule struct { Line struct { Id string `json:"id"` Label string `json:"label"` Name string `json:"name"` ShortName string `json:"shortName"` Companies []struct { Id string `json:"id"` Label string `json:"label"` } `json:"companies"` Network struct { Id string `json:"id"` Label string `json:"label"` } `json:"network"` Mode string `json:"mode"` ModeLabel string `json:"modeLabel"` Realtime bool `json:"realtime"` UFR bool `json:"ufr"` Visual bool `json:"visual"` Sound bool `json:"sound"` Color string `json:"color"` TextColor string `json:"textColor"` } `json:"line"` Schedules []struct { RouteId string `json:"routeId"` From string `json:"from"` To string `json:"to"` First string `json:"first"` Last string `json:"last"` } `json:"schedules"` Plans []struct { Link string `json:"link"` Label string `json:"label"` } `json:"plans"` ScheduleDocs []struct { Link string `json:"link"` Label string `json:"label"` } `json:"scheduleDocs"` CurrentIT []struct { Id string `json:"id"` Title string `json:"title"` Message string `json:"message"` ImpactStartTime IDFMTime `json:"impactStartTime"` ImpactEndTime IDFMTime `json:"impactEndTime"` Severity int `json:"severity"` Type int `json:"type"` } `json:"currentIT"` } type PGDestination struct { Name string `json:"name"` Way string `json:"way"` } func getSchedules(code string) (*IDFMSchedule, error) { rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "schedules") if err != nil { return nil, err } requrl, err := url.Parse(rurl) if err != nil { return nil, err } reqquery := url.Values{} reqquery.Add("complete", "false") requrl.RawQuery = reqquery.Encode() req, err := http.NewRequest("GET", requrl.String(), nil) if err != nil { return nil, err } req.Header.Add("Accept", "application/json") req.Header.Add("apikey", IDFM_TOKEN) res, err := http.DefaultClient.Do(req) if err != nil { return nil, err } defer res.Body.Close() if res.StatusCode >= 400 { return nil, fmt.Errorf("Schedule not found") } var schedules IDFMSchedule dec := json.NewDecoder(res.Body) if err = dec.Decode(&schedules); err != nil { return nil, err } return &schedules, nil } func getDestinations(code string) ([]PGDestination, error) { schedule, err := getSchedules(code) if err != nil { return nil, err } var pgd []PGDestination destination: for i, s := range schedule.Schedules { for _, d := range pgd { if d.Name == s.To { continue destination } } way := "R" if i%2 == 0 { way = "A" } pgd = append(pgd, PGDestination{ Name: s.To, Way: way, }) } return pgd, nil } func declareDestinationsRoutes(router *gin.RouterGroup) { router.GET("/destinations/:type/:code", func(c *gin.Context) { t := convertLineType(string(c.Param("type"))) code := convertCode(t, string(c.Param("code"))) pgd, err := getDestinations(code) if err != nil { c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()}) return } c.JSON(http.StatusOK, APIResult(c, map[string][]PGDestination{ "destinations": pgd, })) }) }