update the geocoding api (#93)

This commit is contained in:
Asim Aslam
2021-05-05 10:37:23 +01:00
committed by GitHub
parent 059724e9cd
commit 5894e0a994
5 changed files with 482 additions and 156 deletions

View File

@@ -22,10 +22,9 @@ type Geocoding struct {
Maps *maps.Client
}
// Geocode an address
func (g *Geocoding) Geocode(ctx context.Context, req *pb.Address, rsp *pb.Address) error {
func (g *Geocoding) Lookup(ctx context.Context, req *pb.LookupRequest, rsp *pb.LookupResponse) error {
// query google maps
results, err := g.Maps.Geocode(ctx, &maps.GeocodingRequest{Address: toString(req)})
results, err := g.Maps.Geocode(ctx, &maps.GeocodingRequest{Address: req.Address})
if err != nil {
logger.Errorf("Error geocoding: %v", err)
return ErrDownstream
@@ -34,24 +33,28 @@ func (g *Geocoding) Geocode(ctx context.Context, req *pb.Address, rsp *pb.Addres
return ErrNoResults
}
rsp.Address = new(pb.Address)
rsp.Location = new(pb.Location)
// return the result
serializeResult(results[0], rsp)
serializeResult(results[0], rsp.Address, rsp.Location)
return nil
}
// Reverse geocode an address
func (g *Geocoding) Reverse(ctx context.Context, req *pb.Coordinates, rsp *pb.Address) error {
func (g *Geocoding) Reverse(ctx context.Context, req *pb.ReverseRequest, rsp *pb.ReverseResponse) error {
// validate the request
if req.Latitude == nil {
if req.Latitude == 0.0 {
return ErrMissingLatitude
}
if req.Longitude == nil {
if req.Longitude == 0.0 {
return ErrMissingLongitude
}
// query google maps
results, err := g.Maps.ReverseGeocode(ctx, &maps.GeocodingRequest{
LatLng: &maps.LatLng{Lat: req.Latitude.Value, Lng: req.Longitude.Value},
LatLng: &maps.LatLng{Lat: req.Latitude, Lng: req.Longitude},
})
if err != nil {
logger.Errorf("Error geocoding: %v", err)
@@ -61,8 +64,11 @@ func (g *Geocoding) Reverse(ctx context.Context, req *pb.Coordinates, rsp *pb.Ad
return ErrNoResults
}
rsp.Address = new(pb.Address)
rsp.Location = new(pb.Location)
// return the result
serializeResult(results[0], rsp)
serializeResult(results[0], rsp.Address, rsp.Location)
return nil
}
@@ -77,8 +83,9 @@ func toString(a *pb.Address) string {
return strings.Join(comps, ", ")
}
func serializeResult(r maps.GeocodingResult, a *pb.Address) {
func serializeResult(r maps.GeocodingResult, a *pb.Address, l *pb.Location) {
var street, number string
for _, c := range r.AddressComponents {
for _, t := range c.Types {
switch t {
@@ -99,6 +106,6 @@ func serializeResult(r maps.GeocodingResult, a *pb.Address) {
}
a.LineOne = strings.Join([]string{number, street}, " ")
a.Latitude = r.Geometry.Location.Lat
a.Longitude = r.Geometry.Location.Lng
l.Latitude = r.Geometry.Location.Lat
l.Longitude = r.Geometry.Location.Lng
}

View File

@@ -9,7 +9,6 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/protobuf/types/known/wrapperspb"
"googlemaps.github.io/maps"
"github.com/micro/services/geocoding/handler"
@@ -148,17 +147,31 @@ func TestGeocoding(t *testing.T) {
}
h := &handler.Geocoding{Maps: m}
var rsp pb.Address
err = h.Geocode(context.TODO(), tc.Address, &rsp)
var rsp pb.LookupResponse
address := tc.Address.LineOne
if len(tc.Address.LineTwo) > 0 {
address = fmt.Sprintf("%s, %s", address, tc.Address.LineTwo)
}
if len(tc.Address.Postcode) > 0 {
address = fmt.Sprintf("%s, %s", address, tc.Address.Postcode)
}
if len(tc.Address.Country) > 0 {
address = fmt.Sprintf("%s, %s", address, tc.Address.Country)
}
err = h.Lookup(context.TODO(), &pb.LookupRequest{
Address: address,
Postcode: tc.Address.Postcode,
Country: tc.Address.Country,
}, &rsp)
assert.Equal(t, tc.MapQuery, query)
assert.Equal(t, tc.Error, err)
if tc.Result != nil {
assert.Equal(t, tc.Result.LineOne, rsp.LineOne)
assert.Equal(t, tc.Result.LineTwo, rsp.LineTwo)
assert.Equal(t, tc.Result.City, rsp.City)
assert.Equal(t, tc.Result.Country, rsp.Country)
assert.Equal(t, tc.Result.Postcode, rsp.Postcode)
assert.Equal(t, tc.Result.LineOne, rsp.Address.LineOne)
assert.Equal(t, tc.Result.LineTwo, rsp.Address.LineTwo)
assert.Equal(t, tc.Result.City, rsp.Address.City)
assert.Equal(t, tc.Result.Country, rsp.Address.Country)
assert.Equal(t, tc.Result.Postcode, rsp.Address.Postcode)
}
})
}
@@ -171,34 +184,34 @@ func TestReverseGeocoding(t *testing.T) {
ResponseBody string
ResponseCode int
Error error
Latitude *wrapperspb.DoubleValue
Longitude *wrapperspb.DoubleValue
Latitude float64
Longitude float64
Result *pb.Address
}{
{
Name: "Missing longitude",
Latitude: &wrapperspb.DoubleValue{Value: 51.522214},
Latitude: 51.522214,
Error: handler.ErrMissingLongitude,
},
{
Name: "Missing latitude",
Longitude: &wrapperspb.DoubleValue{Value: -0.113565},
Longitude: -0.113565,
Error: handler.ErrMissingLatitude,
},
{
Name: "Invalid address",
ResponseBody: noResultsReponse,
ResponseCode: http.StatusOK,
Latitude: &wrapperspb.DoubleValue{Value: 999.999999},
Longitude: &wrapperspb.DoubleValue{Value: 999.999999},
Latitude: 999.999999,
Longitude: 999.999999,
Error: handler.ErrNoResults,
},
{
Name: "Valid address",
ResponseBody: validReponse,
ResponseCode: http.StatusOK,
Latitude: &wrapperspb.DoubleValue{Value: 51.522214},
Longitude: &wrapperspb.DoubleValue{Value: -0.113565},
Latitude: 51.522214,
Longitude: -0.113565,
Result: &pb.Address{
LineOne: "160 Grays Inn Road",
LineTwo: "Holborn",
@@ -208,8 +221,8 @@ func TestReverseGeocoding(t *testing.T) {
},
{
Name: "Maps error",
Latitude: &wrapperspb.DoubleValue{Value: 51.522214},
Longitude: &wrapperspb.DoubleValue{Value: -0.113565},
Latitude: 51.522214,
Longitude: -0.113565,
ResponseCode: http.StatusInternalServerError,
Error: handler.ErrDownstream,
ResponseBody: "{}",
@@ -237,23 +250,23 @@ func TestReverseGeocoding(t *testing.T) {
}
h := &handler.Geocoding{Maps: m}
var rsp pb.Address
err = h.Reverse(context.TODO(), &pb.Coordinates{
var rsp pb.ReverseResponse
err = h.Reverse(context.TODO(), &pb.ReverseRequest{
Latitude: tc.Latitude, Longitude: tc.Longitude,
}, &rsp)
assert.Equal(t, tc.Error, err)
if tc.Latitude != nil && tc.Longitude != nil {
assert.Equal(t, fmt.Sprintf("%f", tc.Latitude.Value), lat)
assert.Equal(t, fmt.Sprintf("%f", tc.Longitude.Value), lng)
if tc.Latitude != 0.0 && tc.Longitude != 0.0 {
assert.Equal(t, fmt.Sprintf("%f", tc.Latitude), lat)
assert.Equal(t, fmt.Sprintf("%f", tc.Longitude), lng)
}
if tc.Result != nil {
assert.Equal(t, tc.Result.LineOne, rsp.LineOne)
assert.Equal(t, tc.Result.LineTwo, rsp.LineTwo)
assert.Equal(t, tc.Result.City, rsp.City)
assert.Equal(t, tc.Result.Country, rsp.Country)
assert.Equal(t, tc.Result.Postcode, rsp.Postcode)
assert.Equal(t, tc.Result.LineOne, rsp.Address.LineOne)
assert.Equal(t, tc.Result.LineTwo, rsp.Address.LineTwo)
assert.Equal(t, tc.Result.City, rsp.Address.City)
assert.Equal(t, tc.Result.Country, rsp.Address.Country)
assert.Equal(t, tc.Result.Postcode, rsp.Address.Postcode)
}
})
}