Skip to content

Commit

Permalink
add directions API support
Browse files Browse the repository at this point in the history
  • Loading branch information
drklrd committed Aug 7, 2022
1 parent 1050dca commit 47c49f9
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

```



Expand Down
26 changes: 26 additions & 0 deletions examples/directions.go
Original file line number Diff line number Diff line change
@@ -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)

}
2 changes: 1 addition & 1 deletion examples/nearby_places.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/baato.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -13,6 +14,7 @@ type BaatoAPI struct {
ReverseGeocode *reversegeocode.ReverseGeocode
Geocode *geocode.Geocode
NearbyPlaces *nearby.NearbyPlaces
Directions *directions.Directions
}

func Baato(accessToken string) *BaatoAPI {
Expand All @@ -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

}
11 changes: 11 additions & 0 deletions lib/commons/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
53 changes: 53 additions & 0 deletions lib/directions/directions.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 47c49f9

Please sign in to comment.