mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-24 08:25:31 +00:00
add google eta to routing service
This commit is contained in:
@@ -25,11 +25,51 @@ type Google struct {
|
|||||||
Maps *maps.Client
|
Maps *maps.Client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Google) Eta(ctx context.Context, req *pb.EtaRequest, rsp *pb.EtaResponse) error {
|
func (r *Google) Directions(ctx context.Context, req *pb.DirectionsRequest, rsp *pb.DirectionsResponse) error {
|
||||||
// TODO: implement eta
|
// TODO: implement eta
|
||||||
return ErrUnimplemented
|
return ErrUnimplemented
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculate the ETAs for a route
|
||||||
|
func (r *Google) Eta(ctx context.Context, req *pb.EtaRequest, rsp *pb.EtaResponse) error {
|
||||||
|
// validate the request
|
||||||
|
if req.Origin == nil {
|
||||||
|
return ErrMissingOrigin
|
||||||
|
}
|
||||||
|
if req.Destination == nil {
|
||||||
|
return ErrMissingDestination
|
||||||
|
}
|
||||||
|
if err := validatePoint(req.Origin); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := validatePoint(req.Destination); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// construct the request
|
||||||
|
resp, err := r.Maps.DistanceMatrix(ctx, &maps.DistanceMatrixRequest{
|
||||||
|
Origins: []string{pointToString(req.Origin)},
|
||||||
|
Destinations: []string{pointToString(req.Destination)},
|
||||||
|
DepartureTime: "now",
|
||||||
|
Units: "UnitsMetric",
|
||||||
|
Mode: maps.TravelModeDriving,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// check the correct number of elements (route segments) were returned
|
||||||
|
// from the Google API
|
||||||
|
if len(resp.Rows[0].Elements) != 1 {
|
||||||
|
return errors.InternalServerError("routing.eta", "Invalid downstream response. Expected %v segments but got %v", 1, len(resp.Rows[0].Elements))
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the response duration in seconds
|
||||||
|
rsp.Duration = float64(resp.Rows[0].Elements[0].Duration.Seconds())
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Google) Route(ctx context.Context, req *pb.RouteRequest, rsp *pb.RouteResponse) error {
|
func (r *Google) Route(ctx context.Context, req *pb.RouteRequest, rsp *pb.RouteResponse) error {
|
||||||
// validate the request
|
// validate the request
|
||||||
if req.Origin == nil {
|
if req.Origin == nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user