aboutsummaryrefslogtreecommitdiff
path: root/pkg/navitia_api_client/client.go
blob: bd36c6a464a000ec09c7271c6f2b46202c940572 (plain)
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
package navitia_api_client

import (
	"fmt"
	"net/http"
	"sync"
	"time"

	"git.adyxax.org/adyxax/trains/pkg/model"
)

type Client interface {
	GetDepartures(stop string) (departures []model.Departure, err error)
	GetStops() (stops []model.Stop, err error)
}

type NavitiaClient struct {
	baseURL    string
	httpClient *http.Client

	mutex sync.Mutex
	cache map[string]cachedResult
}

type cachedResult struct {
	ts     time.Time
	result interface{}
}

func NewClient(token string) Client {
	return &NavitiaClient{
		baseURL: fmt.Sprintf("https://%s@api.sncf.com/v1", token),
		httpClient: &http.Client{
			Timeout: time.Minute,
		},
		cache: make(map[string]cachedResult),
	}
}