-
Notifications
You must be signed in to change notification settings - Fork 5
/
addr.go
83 lines (72 loc) · 2.1 KB
/
addr.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
package aklapi
import (
"bytes"
"encoding/json"
"errors"
"log"
"net/http"
"time"
)
var (
// defined as a variable so it can be overridden in tests.
addrURI = `https://www.aucklandcouncil.govt.nz/_vti_bin/ACWeb/ACservices.svc/GetMatchingPropertyAddresses`
)
// AddrRequest is the address request.
type AddrRequest struct {
ResultCount int `json:"ResultCount"`
SearchText string `json:"SearchText"`
RateKeyRequired bool `json:"RateKeyRequired"`
}
// AddrResponse is the address response.
type AddrResponse []Address
// AddrSuggestion is the address suggestion.
type Address struct {
ACRateAccountKey string `json:"ACRateAccountKey"`
Address string `json:"Address"`
Suggestion string `json:"Suggestion"`
}
func (s Address) String() string {
return "<" + s.Address + " (" + s.ACRateAccountKey + ")>"
}
// AddressLookup is a convenience function to get addresses.
func AddressLookup(addr string) (AddrResponse, error) {
return MatchingPropertyAddresses(&AddrRequest{SearchText: addr, RateKeyRequired: false, ResultCount: 10})
}
// MatchingPropertyAddresses wrapper around the AKL Council API.
func MatchingPropertyAddresses(addrReq *AddrRequest) (AddrResponse, error) {
cachedAr, ok := addrCache.Lookup(addrReq.SearchText)
if ok {
log.Printf("cached address result: %q", cachedAr)
return cachedAr, nil
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
if err := enc.Encode(addrReq); err != nil {
return nil, err
}
start := time.Now()
resp, err := http.Post(addrURI, "application/json; charset=UTF-8", &buf)
if err != nil {
return nil, err
}
defer resp.Body.Close()
log.Printf("address call complete in %s", time.Since(start))
dec := json.NewDecoder(resp.Body)
ar := AddrResponse{}
if err := dec.Decode(&ar); err != nil {
return nil, err
}
addrCache.Add(addrReq.SearchText, ar)
return ar, nil
}
func oneAddress(addr string) (*Address, error) {
resp, err := AddressLookup(addr)
if err != nil {
return nil, err
}
// need exactly one address to continue
if len(resp) != 1 {
return nil, errors.New("ambiguous or empty address results")
}
return &resp[0], nil
}