-
Notifications
You must be signed in to change notification settings - Fork 21
/
address.go
75 lines (64 loc) · 1.82 KB
/
address.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
package onfido
import (
"encoding/json"
"errors"
"net/url"
)
var (
// ErrEmptyPostcode means that an empty postcode param was passed
ErrEmptyPostcode = errors.New("empty postcode")
)
// Addresses represents a list of addresses from the Onfido API
type Addresses struct {
Addresses []*Address `json:"addresses"`
}
// Address represents an address from the Onfido API
type Address struct {
FlatNumber string `json:"flat_number"`
BuildingNumber string `json:"building_number"`
BuildingName string `json:"building_name"`
Street string `json:"street"`
SubStreet string `json:"sub_street"`
Town string `json:"town"`
State string `json:"state"`
Postcode string `json:"postcode"`
Country string `json:"country"`
// Applicant specific
StartDate string `json:"start_date,omitempty"`
EndDate string `json:"end_date,omitempty"`
}
// PickerIter represents an address picker iterator
type PickerIter struct {
*iter
}
// Address returns the current address on the iterator.
func (i *PickerIter) Address() *Address {
return i.Current().(*Address)
}
// PickAddresses retrieves the list of addresses matched against the provided postcode.
// see https://documentation.onfido.com/?shell#address-picker
func (c *Client) PickAddresses(postcode string) *PickerIter {
if postcode == "" {
return &PickerIter{&iter{
err: ErrEmptyPostcode,
}}
}
handler := func(body []byte) ([]interface{}, error) {
var a Addresses
if err := json.Unmarshal(body, &a); err != nil {
return nil, err
}
values := make([]interface{}, len(a.Addresses))
for i, v := range a.Addresses {
values[i] = v
}
return values, nil
}
params := make(url.Values)
params.Set("postcode", postcode)
return &PickerIter{&iter{
c: c,
nextURL: "addresses/pick?" + params.Encode(),
handler: handler,
}}
}