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

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

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

type Client interface {
	GetDepartures(trainStop string) (departures []model.Departure, 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),
	}
}