Implement destinations
This commit is contained in:
parent
c458a32d7b
commit
a0659dd9bd
3 changed files with 210 additions and 43 deletions
|
|
@ -2,6 +2,7 @@ package api
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
|
@ -13,7 +14,7 @@ import (
|
|||
|
||||
const IDFM_BASEURL = "https://api-iv.iledefrance-mobilites.fr/"
|
||||
|
||||
type IDFMSchedule struct {
|
||||
type IDFMRealTime struct {
|
||||
NextDepartures struct {
|
||||
StatusCode int `json:"statusCode"`
|
||||
Data []struct {
|
||||
|
|
@ -52,10 +53,53 @@ func convertLineCode(code string) string {
|
|||
return strings.TrimSuffix(strings.Replace(code, "STIF:Line::", "IDFM:", 1), ":")
|
||||
}
|
||||
|
||||
func getRealTime(code, station string) (*IDFMRealTime, error) {
|
||||
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "stops", station, "realTime")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
requrl, err := url.Parse(rurl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
reqquery := url.Values{}
|
||||
reqquery.Add("MonitoringRef", station)
|
||||
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 IDFMRealTime
|
||||
|
||||
dec := json.NewDecoder(res.Body)
|
||||
if err = dec.Decode(&schedules); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &schedules, nil
|
||||
}
|
||||
|
||||
func declareSchedulesRoutes(router *gin.RouterGroup) {
|
||||
router.GET("/schedules/:type/:code/:station/:way", func(c *gin.Context) {
|
||||
t := convertLineType(string(c.Param("type")))
|
||||
code := convertLineType(string(c.Param("code")))
|
||||
code := string(c.Param("code"))
|
||||
station := string(c.Param("station"))
|
||||
way := string(c.Param("way"))
|
||||
|
||||
|
|
@ -79,51 +123,12 @@ func declareSchedulesRoutes(router *gin.RouterGroup) {
|
|||
}
|
||||
}
|
||||
|
||||
rurl, err := url.JoinPath(IDFM_BASEURL, "lines", code, "stops", station, "realTime")
|
||||
schedules, err := getRealTime(code, station)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
requrl, err := url.Parse(rurl)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
reqquery := url.Values{}
|
||||
reqquery.Add("MonitoringRef", station)
|
||||
requrl.RawQuery = reqquery.Encode()
|
||||
|
||||
req, err := http.NewRequest("GET", requrl.String(), nil)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
req.Header.Add("Accept", "application/json")
|
||||
req.Header.Add("apikey", IDFM_TOKEN)
|
||||
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode >= 400 {
|
||||
c.AbortWithStatusJSON(res.StatusCode, APIResult(c, nil))
|
||||
return
|
||||
}
|
||||
|
||||
var schedules IDFMSchedule
|
||||
|
||||
dec := json.NewDecoder(res.Body)
|
||||
if err = dec.Decode(&schedules); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
pgs := []PGSchedule{}
|
||||
for _, vehicule := range schedules.NextDepartures.Data {
|
||||
if (way == "A" && vehicule.Sens == "-1") || (way == "R" && vehicule.Sens == "1") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue