govitia / navitia

A client library in Go for the Navitia API (navitia.io) (v2 WIP)
The Unlicense
6 stars 2 forks source link
api-client go navitia navitia-api public-transportation transit transportation

navitia is a Go client for the navitia API for public transit & transportation

Build Status Go Report Card GoDoc

This is govitia/navitia golang API.

Dependencies

Install

go get -u github.com/govitia/navitia

Coverage

Changelog

Changelog

Contributing

Contributing guide

Getting started

Creating a new session

First, you should have an API key from navitia.io, if you don't already have one, it's this way !

session, err := navitia.New(APIKEY)

Finding places

import(
    "github.com/govitia/navitia"
    "github.com/govitia/navitia/types"
    "context"
)

// Create a request
req := navitia.PlacesRequest{
    Query: "10 rue du caire, Paris",
    Types: []string{"address"},
    Count: 1,
}

// Execute it
res, _ := session.Places(context.Background(),req)

// Create a variable to store it
var myPlace types.Place

// Check if there are enough results, and then assign the first element as your place
if places := res.Places; len(places) != 0 {
    myPlace = res.Places
}

Calculating a journey

import(
    "github.com/govitia/navitia"
    "fmt"
    "context"
)

// Create a request, having already found two corresponding places
request := navitia.JourneyRequest{
    From: myPlace,
    To: thatOtherPlace,
}

// Execute it
res, _ := session.Journeys(context.Background(),request)

// Print it (JourneysResults implements Stringer)
fmt.Println(res)

Paging

Unfortunately, paging isn't supported by Regions nor by Places requests. You'll have to play with the PlacesRequest.Count value in the latter case. We'll use a Journey to showcase the paging:


// Obtain a journey like last time...

// Create a value to store the paginated result
var paginated *JourneyResults = res

// Iterate by checking if the Paging.Next function is not nil !
for paginated.Paging.Next != nil {
    // Create a new JourneyResults to hold the data
    p := JourneyResults{}

    // Call the function
    _ = paginated.Paging.Next(ctx, testSession, &p)

    // Assign a pointer to the previously created data structure to paginated
    paginated = &p
}

Obviously, you'll want to stop paginating at some point, and most importantly do something with the value. An example is on the way !

Scoping

When you wish to make some requests requiring a specific coverage, or have more meaningful results in global requests, you create a Scope

import (
    "github.com/govitia/navitia"
    "github.com/govitia/navitia/types"
    "context"
)

var (
    session *navitia.Session
    regionID types.ID
    req navitia.PlacesRequest
)

scope := session.Scope(regionID)

// Requests places in this scope
res, _ := scope.Places(context.Background(),req)

Going further

Obviously, this is a very simple example of what navitia can do, check out the documentation !