-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
171 lines (135 loc) · 3.56 KB
/
client.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package place
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)
const postcodeEndpoint = "/search/places/v1/postcode?"
type Doer interface {
Do(*http.Request) (*http.Response, error)
}
type Client struct {
baseUrl string
apiKey string
doer Doer
}
type addressDetails struct {
Address string `json:"ADDRESS"`
SubBuildingName string `json:"SUB_BUILDING_NAME"`
BuildingName string `json:"BUILDING_NAME"`
BuildingNumber string `json:"BUILDING_NUMBER"`
ThoroughFareName string `json:"THOROUGHFARE_NAME"`
DependentLocality string `json:"DEPENDENT_LOCALITY"`
Town string `json:"POST_TOWN"`
Postcode string `json:"POSTCODE"`
}
type postcodeLookupResponse struct {
Results []ResultSet `json:"results"`
Error BadRequestError `json:"error"`
}
type ResultSet struct {
AddressDetails addressDetails `json:"DPA"`
}
type BadRequestError struct {
Statuscode int `json:"statuscode"`
Message string `json:"message"`
}
func (b BadRequestError) Error() string {
return b.Message
}
func NewClient(baseUrl, apiKey string, httpClient Doer) *Client {
return &Client{
baseUrl: baseUrl,
apiKey: apiKey,
doer: httpClient,
}
}
func (c *Client) LookupPostcode(ctx context.Context, postcode string) ([]Address, error) {
query := url.Values{
"postcode": {strings.ReplaceAll(postcode, " ", "")},
"key": {c.apiKey},
}
reqUrl := c.baseUrl + postcodeEndpoint + query.Encode()
req, err := http.NewRequestWithContext(ctx, "GET", reqUrl, nil)
if err != nil {
return []Address{}, err
}
req.Header.Add("accept", "application/json")
resp, err := c.doer.Do(req)
if err != nil {
return []Address{}, err
}
defer resp.Body.Close()
var postcodeLookupResponse postcodeLookupResponse
if err := json.NewDecoder(resp.Body).Decode(&postcodeLookupResponse); err != nil {
return []Address{}, err
}
if postcodeLookupResponse.Error.Statuscode == http.StatusBadRequest {
return []Address{}, postcodeLookupResponse.Error
}
var addresses []Address
for _, resultSet := range postcodeLookupResponse.Results {
addresses = append(addresses, resultSet.AddressDetails.transformToAddress())
}
return addresses, nil
}
type Address struct {
Line1 string `json:"line1"`
Line2 string `json:"line2"`
Line3 string `json:"line3"`
TownOrCity string `json:"town"`
Postcode string `json:"postcode"`
Country string `json:"country"`
}
func (a Address) Encode() string {
x, _ := json.Marshal(a)
return string(x)
}
func (a Address) Lines() []string {
var parts []string
if a.Line1 != "" {
parts = append(parts, a.Line1)
}
if a.Line2 != "" {
parts = append(parts, a.Line2)
}
if a.Line3 != "" {
parts = append(parts, a.Line3)
}
if a.TownOrCity != "" {
parts = append(parts, a.TownOrCity)
}
if a.Postcode != "" {
parts = append(parts, a.Postcode)
}
return parts
}
func (a Address) String() string {
return strings.Join(a.Lines(), ", ")
}
func (ad *addressDetails) transformToAddress() Address {
a := Address{}
if len(ad.BuildingName) > 0 {
if len(ad.SubBuildingName) > 0 {
a.Line1 = fmt.Sprintf("%s, %s", ad.SubBuildingName, ad.BuildingName)
} else {
a.Line1 = ad.BuildingName
}
if len(ad.BuildingNumber) > 0 {
a.Line2 = fmt.Sprintf("%s %s", ad.BuildingNumber, ad.ThoroughFareName)
} else {
a.Line2 = ad.ThoroughFareName
}
a.Line3 = ad.DependentLocality
} else {
a.Line1 = fmt.Sprintf("%s %s", ad.BuildingNumber, ad.ThoroughFareName)
a.Line2 = ad.DependentLocality
}
a.TownOrCity = ad.Town
a.Postcode = ad.Postcode
a.Country = "GB"
return a
}