11package aklapi
22
33import (
4- "bytes"
54 "encoding/json"
65 "errors"
76 "log"
87 "net/http"
8+ "strconv"
99 "time"
1010)
1111
1212var (
1313 // defined as a variable so it can be overridden in tests.
14- addrURI = `https://www.aucklandcouncil.govt.nz/_vti_bin/ACWeb/ACservices.svc/GetMatchingPropertyAddresses `
14+ addrURI = `https://www.aucklandcouncil.govt.nz/nextapi/property `
1515)
1616
1717// AddrRequest is the address request.
1818type AddrRequest struct {
19- ResultCount int `json:"ResultCount"`
20- SearchText string `json:"SearchText"`
21- RateKeyRequired bool `json:"RateKeyRequired"`
19+ PageSize int
20+ SearchText string
2221}
2322
24- // AddrResponse is the address response.
25- type AddrResponse []Address
26-
27- // AddrSuggestion is the address suggestion.
23+ // Address is the address and its unique identifier (rate account key).
2824type Address struct {
29- ACRateAccountKey string `json:"ACRateAccountKey"`
30- Address string `json:"Address"`
31- Suggestion string `json:"Suggestion"`
25+ ID string `json:"ID"`
26+ Address string `json:"Address"`
27+ }
28+
29+ // AddrResponse is the address response.
30+ type AddrResponse struct {
31+ Items []Address `json:"items"`
3232}
3333
3434func (s Address ) String () string {
35- return "<" + s .Address + " (" + s .ACRateAccountKey + ")>"
35+ return "<" + s .Address + " (" + s .ID + ")>"
3636}
3737
3838// AddressLookup is a convenience function to get addresses.
39- func AddressLookup (addr string ) (AddrResponse , error ) {
40- return MatchingPropertyAddresses (& AddrRequest {SearchText : addr , RateKeyRequired : false , ResultCount : 10 })
39+ func AddressLookup (addr string ) (* AddrResponse , error ) {
40+ return MatchingPropertyAddresses (& AddrRequest {SearchText : addr , PageSize : 10 })
4141}
4242
4343// MatchingPropertyAddresses wrapper around the AKL Council API.
44- func MatchingPropertyAddresses (addrReq * AddrRequest ) (AddrResponse , error ) {
44+ func MatchingPropertyAddresses (addrReq * AddrRequest ) (* AddrResponse , error ) {
4545 cachedAr , ok := addrCache .Lookup (addrReq .SearchText )
4646 if ok {
4747 log .Printf ("cached address result: %q" , cachedAr )
4848 return cachedAr , nil
4949 }
50- var buf bytes. Buffer
51- enc := json . NewEncoder ( & buf )
52- if err := enc . Encode ( addrReq ); err != nil {
50+
51+ req , err := http . NewRequest ( "GET" , addrURI , nil )
52+ if err != nil {
5353 return nil , err
5454 }
55+ q := req .URL .Query ()
56+ q .Add ("query" , addrReq .SearchText )
57+ if addrReq .PageSize > 0 {
58+ q .Add ("pageSize" , strconv .Itoa (addrReq .PageSize ))
59+ }
60+ req .URL .RawQuery = q .Encode ()
5561
5662 start := time .Now ()
57- resp , err := http .Post (addrURI , "application/json; charset=UTF-8" , & buf )
63+ client := & http.Client {}
64+ resp , err := client .Do (req )
5865 if err != nil {
5966 return nil , err
6067 }
6168 defer resp .Body .Close ()
6269 log .Printf ("address call complete in %s" , time .Since (start ))
6370
71+ if resp .StatusCode != http .StatusOK {
72+ return nil , errors .New ("address API returned status code: " + strconv .Itoa (resp .StatusCode ))
73+ }
74+
6475 dec := json .NewDecoder (resp .Body )
65- ar := AddrResponse {}
66- if err := dec .Decode (& ar ); err != nil {
76+ var apiResp AddrResponse
77+ if err := dec .Decode (& apiResp ); err != nil {
6778 return nil , err
6879 }
69- addrCache .Add (addrReq .SearchText , ar )
70- return ar , nil
80+
81+ addrCache .Add (addrReq .SearchText , & apiResp )
82+ return & apiResp , nil
7183}
7284
7385func oneAddress (addr string ) (* Address , error ) {
@@ -76,8 +88,8 @@ func oneAddress(addr string) (*Address, error) {
7688 return nil , err
7789 }
7890 // need exactly one address to continue
79- if len (resp ) != 1 {
91+ if len (resp . Items ) != 1 {
8092 return nil , errors .New ("ambiguous or empty address results" )
8193 }
82- return & resp [0 ], nil
94+ return & resp . Items [0 ], nil
8395}
0 commit comments