-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.go
75 lines (65 loc) · 1.58 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
package idealpostcodes
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
// Endpoint is the API base URI
const Endpoint = "https://api.ideal-postcodes.co.uk/v1"
// Client for https://ideal-postcodes.co.uk API
type Client struct {
endpoint string
apiKey string
httpClient *http.Client
}
// NewClient returns new Client instance
func NewClient(endpoint string, apiKey string, httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = new(http.Client)
}
return &Client{
endpoint: endpoint,
apiKey: apiKey,
httpClient: httpClient,
}
}
// GetPostcode returns list of addresses matching a postcode
func (c *Client) GetPostcode(postcode string) ([]*Address, error) {
// Prepare a request
payload := url.Values{}
payload.Add("api_key", c.apiKey)
req, err := http.NewRequest(
"GET",
fmt.Sprintf("%s/postcodes/%s?%s", c.endpoint, postcode, payload.Encode()),
nil,
)
if err != nil {
return nil, err
}
// Make the request
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
// Read the response data
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
// Unmarshall into our struct
getPostResponse := new(GetPostcodeResponse)
if err := json.Unmarshal(data, getPostResponse); err != nil {
// Log the response
log.Printf("%s", resp.Body)
return nil, err
}
// If the status code was not 200, return error with the message from response
if resp.StatusCode != http.StatusOK {
return nil, errors.New(getPostResponse.Message)
}
return getPostResponse.Result, nil
}