diff --git a/README.md b/README.md index 3d2a8a2..742673c 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ baatoMap := baato.Baato(accessToken) # Usage +For a detailed usage instructions, please visit documentation for the Baato-Go-Client avaiable [here](https://docs.baato.io). Quick usage examples are shown here. + ## Geocode (Search) API ```go // intialize geocoding request options @@ -81,6 +83,20 @@ You can use this API to get nearby places around a point that is interesting for fmt.Println(nearbyplaces.Data) ``` +## Directions API + +```go +// intialize directions request options. PointsArray represents points that we should pass through. + directionsRequest := directions.DirectionsRequestOptions{ + PointsArray: []string{"27.6733433,85.2763307", "27.67444444,85.28047222"}, + Mode: "bike", + } + + directions, _ := baatoMap.Directions.GetDirections(directionsRequest) + + fmt.Println(directions.Data) + +``` diff --git a/examples/directions.go b/examples/directions.go new file mode 100644 index 0000000..858411f --- /dev/null +++ b/examples/directions.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + + baato "github.com/baato/baato-go-client/lib" + "github.com/baato/baato-go-client/lib/directions" +) + +func main() { + + // initialize Baato core module + accessToken := "YOUR BAATO ACCESS TOKEN" // Get Baato token from environment + baatoMap := baato.Baato(accessToken) + + // intialize directions request options + directionsRequest := directions.DirectionsRequestOptions{ + PointsArray: []string{"27.6733433,85.2763307", "27.67444444,85.28047222"}, + Mode: "bike", + } + + directions, _ := baatoMap.Directions.GetDirections(directionsRequest) + + fmt.Println(directions.Data) + +} diff --git a/examples/nearby_places.go b/examples/nearby_places.go index f515283..15b25dc 100644 --- a/examples/nearby_places.go +++ b/examples/nearby_places.go @@ -10,7 +10,7 @@ import ( func main() { // initialize Baato core module - accessToken := "bpk.S6SPi8mRVrdrgmukO4jN-MUuNCGwoqTsLggG55uIAeMQ" // Get Baato token from environment + accessToken := "YOUR BAATO ACCESS TOKEN" // Get Baato token from environment baatoMap := baato.Baato(accessToken) // intialize nearby places request options diff --git a/lib/baato.go b/lib/baato.go index 283bed5..72c542a 100644 --- a/lib/baato.go +++ b/lib/baato.go @@ -2,6 +2,7 @@ package baato import ( "github.com/baato/baato-go-client/lib/commons" + "github.com/baato/baato-go-client/lib/directions" "github.com/baato/baato-go-client/lib/geocode" "github.com/baato/baato-go-client/lib/nearby" reversegeocode "github.com/baato/baato-go-client/lib/reverse_geocode" @@ -13,6 +14,7 @@ type BaatoAPI struct { ReverseGeocode *reversegeocode.ReverseGeocode Geocode *geocode.Geocode NearbyPlaces *nearby.NearbyPlaces + Directions *directions.Directions } func Baato(accessToken string) *BaatoAPI { @@ -25,6 +27,7 @@ func Baato(accessToken string) *BaatoAPI { instance.ReverseGeocode = reversegeocode.NewReverseGeocode(instance.commons) instance.Geocode = geocode.NewGeocode(instance.commons) instance.NearbyPlaces = nearby.NewNearbyPlaces(instance.commons) + instance.Directions = directions.NewDirections(instance.commons) return instance } diff --git a/lib/commons/types.go b/lib/commons/types.go index c8f7292..444acd1 100644 --- a/lib/commons/types.go +++ b/lib/commons/types.go @@ -59,6 +59,17 @@ type NearbyPlaceFeature struct { open string `json:"open"` } +type DirectionsFeature struct { + EncodedPolyline string `json:"encodedPolyline"` + DistanceInMeters float64 `json:"distanceInMeters"` + TimeInMs int `json:"timeInMs"` + InstructionList interface{} `json:"instructionList"` +} + type NearbyPlacesData struct { Data []NearbyPlaceFeature `json:"data"` } + +type DirectionsData struct { + Data []DirectionsFeature `json:"data"` +} diff --git a/lib/directions/directions.go b/lib/directions/directions.go new file mode 100644 index 0000000..e94c8fd --- /dev/null +++ b/lib/directions/directions.go @@ -0,0 +1,53 @@ +package directions + +import ( + "github.com/baato/baato-go-client/lib/commons" + "github.com/google/go-querystring/query" +) + +const ( + apiLabel = "directions" +) + +type Directions struct { + commons *commons.Commons +} + +type DirectionsRequestOptions struct { + PointsArray []string `url:"pointsArray"` + Mode string `url:"mode"` + Alternatives bool `url:"alternatives"` + Instructions bool `url:"instructions"` +} + +type DirectionsResponse struct { + Timestamp string + Status int + Message string + *commons.DirectionsData +} + +func NewDirections(commons *commons.Commons) *Directions { + return &Directions{commons} +} + +func (d *Directions) GetDirections(request DirectionsRequestOptions) (*DirectionsResponse, error) { + + values, error := query.Values(request) + + // deserialize points array into each points[] query param + pointsArray := request.PointsArray + for _, s := range pointsArray { + values.Add("points[]", s) + } + + if error != nil { + return nil, error + } + + resp := DirectionsResponse{} + + d.commons.BaatoAPIRequest(apiLabel, &values, &resp) + + return &resp, error +}