-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Why? To make the code base more maintainable and readable. How? Split the geocode client, interface and api implememtation into differnt files. Split the tests into different files too.
- Loading branch information
CiaraTully
committed
Dec 31, 2023
1 parent
f9f03c1
commit a8cf11d
Showing
6 changed files
with
133 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package geocoder | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
type GeocodeAPI struct { | ||
baseURL string | ||
endpoint string | ||
apiKey string | ||
} | ||
|
||
func NewGeoCodeMaps() Geocoder { | ||
return &GeocodeAPI{baseURL: "https://geocode.maps.co", endpoint: "/search", apiKey: os.Getenv("GEOCODE_API_KEY")} | ||
} | ||
|
||
func (g *GeocodeAPI) fullURL() string { | ||
return g.baseURL + g.endpoint | ||
} | ||
|
||
func (g *GeocodeAPI) getPlace(address string) ([]Place, error) { | ||
queryParams := url.Values{} | ||
queryParams.Add("q", address) | ||
queryParams.Add("api_key", g.apiKey) | ||
fullURL := fmt.Sprintf("%s?%s", g.fullURL(), queryParams.Encode()) | ||
resp, err := http.Get(fullURL) | ||
if err != nil { | ||
print("Error retrieving location information") | ||
} | ||
defer resp.Body.Close() | ||
|
||
var places []Place | ||
if err := json.NewDecoder(resp.Body).Decode(&places); err != nil { | ||
fmt.Println("Error decoding JSON:", err) | ||
} | ||
return places, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package geocoder | ||
|
||
import ( | ||
"os" | ||
) | ||
|
||
const defualtBaseURL = "https://geocode.maps.co" | ||
const searchEndpoint = "/search" | ||
|
||
type GeocoderClient struct { | ||
geocoder Geocoder | ||
} | ||
|
||
func NewGeocoderClient(geocoder Geocoder) *GeocoderClient { | ||
if geocoder == nil { | ||
geocoder = &GeocodeAPI{baseURL: defualtBaseURL, endpoint: searchEndpoint, apiKey: os.Getenv("GEOCODE_API_KEY")} | ||
} | ||
return &GeocoderClient{geocoder: geocoder} | ||
} | ||
|
||
func (g *GeocoderClient) FindCoordinates(address string) ([]Place, error) { | ||
var places []Place | ||
places, err := g.geocoder.getPlace(address) | ||
return places, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package geocoder | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type MockGeocode struct { | ||
places []Place | ||
err error | ||
} | ||
|
||
func (m MockGeocode) getPlace(address string) ([]Place, error) { | ||
return m.places, m.err | ||
} | ||
func (m MockGeocode) fullURL() string { return "testing" } | ||
|
||
func TestFindCoordinates(t *testing.T) { | ||
type args struct { | ||
mockResponse []Place | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []Place | ||
wantErr error | ||
}{ | ||
{ | ||
name: "It delegates to the geocoder api and returns the response and error", | ||
args: args{mockResponse: []Place{{Latitude: "100", Longitude: "100", DisplayName: "Somewhere"}}}, | ||
want: []Place{{Latitude: "100", Longitude: "100", DisplayName: "Somewhere"}}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
mockGeocodeAPI := MockGeocode{places: tt.args.mockResponse} | ||
geocoderClient := &GeocoderClient{geocoder: mockGeocodeAPI} | ||
location, _ := geocoderClient.FindCoordinates("test address") | ||
assert.Equal(t, tt.want, location) | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package geocoder | ||
|
||
type Geocoder interface { | ||
getPlace(address string) ([]Place, error) | ||
fullURL() string | ||
} | ||
|
||
type Place struct { | ||
Latitude string `json:"lat"` | ||
Longitude string `json:"lon"` | ||
DisplayName string `json:"display_name"` | ||
} |