forked from codingsince1985/geo-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_geocoder.go
108 lines (94 loc) · 2.64 KB
/
http_geocoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package geo
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
// ErrTimeout occurs when no response returned within timeoutInSeconds
var ErrTimeout = errors.New("TIMEOUT")
var timeout = time.Second * 8
// ErrNoResult occurs when no result returned
var ErrNoResult = errors.New("NO_RESULT")
// Location is the output of Geocode
type Location struct {
Lat, Lng float64
}
// EndpointBuilder defines functions that build urls for geocode/reverse geocode
type EndpointBuilder interface {
GeocodeURL(string) string
ReverseGeocodeURL(Location) string
}
// ResponseParserFactory creates a new ResponseParser
type ResponseParserFactory func() ResponseParser
// ResponseParser defines functions that parse response of geocode/reverse geocode
type ResponseParser interface {
Location() Location
Address() string
}
// HTTPGeocoder has EndpointBuilder and ResponseParser
type HTTPGeocoder struct {
EndpointBuilder
ResponseParserFactory ResponseParserFactory
}
// Geocode returns location for address
func (g HTTPGeocoder) Geocode(address string) (Location, error) {
ch := make(chan Location, 1)
go func() {
responseParser := g.ResponseParserFactory()
response(g.GeocodeURL(url.QueryEscape(address)), responseParser)
ch <- responseParser.Location()
}()
select {
case location := <-ch:
return location, anyError(location)
case <-time.After(timeout):
return Location{}, ErrTimeout
}
}
// ReverseGeocode returns address for location
func (g HTTPGeocoder) ReverseGeocode(lat, lng float64) (string, error) {
ch := make(chan string, 1)
go func() {
responseParser := g.ResponseParserFactory()
response(g.ReverseGeocodeURL(Location{lat, lng}), responseParser)
ch <- responseParser.Address()
}()
select {
case address := <-ch:
return address, anyError(address)
case <-time.After(timeout):
return "", ErrTimeout
}
}
// Response gets response from url
func response(url string, obj ResponseParser) {
if req, err := http.NewRequest("GET", url, nil); err == nil {
if resp, err := (&http.Client{}).Do(req); err == nil {
defer resp.Body.Close()
if data, err := ioutil.ReadAll(resp.Body); err == nil {
// TODO: don't swallow json unmarshal errors
// currently it just treats an empty response as a ErrNoResult which
// is fine for now but we should have some logging or something to indicate
// failed json unmarshal
json.Unmarshal([]byte(strings.Trim(string(data), " []")), obj)
}
}
}
}
func anyError(v interface{}) (err error) {
switch v := v.(type) {
case Location:
if v.Lat == 0 && v.Lng == 0 {
return ErrNoResult
}
case string:
if v == "" {
return ErrNoResult
}
}
return
}