Back to official API
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
nemunaire 2024-07-26 12:47:08 +02:00
commit 3e39c5e5c0
6 changed files with 202 additions and 301 deletions

View file

@ -1,10 +1,7 @@
package api
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/gin-gonic/gin"
)
@ -63,85 +60,13 @@ type PGDestination struct {
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
return nil, 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)
pgd, err := getDestinations(string(c.Param("code")))
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
return