github.com/peferb/trafiklab@v0.2.4 (changelog)
Golang wrapper of Trafiklab.se which supplies traffic information about Swedish trains, buses, trams and ships.
Implemented to match the official specifications as much as possible while trying to keep it simple and small and also easy to use.
GO GET IT!
$ go get github.com/peferb/trafiklab@v0.2.4package main
import (
"bytes"
"encoding/json"
"github.com/peferb/trafiklab/sl"
. "github.com/peferb/trafiklab/sl/api"
"log"
"net/url"
"os"
)
func main() {
departuresApi := sl.NewApi(RealTimeDeparturesV4, JSON, "you api key here")
departures, err := departuresApi.GetBytes(url.Values{"siteid": {os.Args[1]}})
if err != nil {
log.Println(err)
return
}
prettyJson := &bytes.Buffer{}
err = json.Indent(prettyJson, departures, "", " ")
log.Println(prettyJson.String(), err)
// Example run:
// $ go run . 9710
}package main
import (
"github.com/peferb/trafiklab/sl"
. "github.com/peferb/trafiklab/sl/api"
"log"
"net/http"
)
func departuresHandler(w http.ResponseWriter, req *http.Request) {
departuresApi := sl.NewApi(RealTimeDeparturesV4, JSON, "your api key here")
bytes, err := departuresApi.GetBytes(req.URL.Query())
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
w.Write(bytes)
}
func main() {
http.HandleFunc("/realtime-departures", departuresHandler)
log.Println("Listening for requests at http://localhost:8000/")
log.Fatal(http.ListenAndServe(":8000", nil))
// Example url:
// localhost:8000/realtime-departures?siteid=9710
}- Sign up at trafiklab.se
- Generate keys
