-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostcodesio.go
More file actions
70 lines (57 loc) · 1.63 KB
/
postcodesio.go
File metadata and controls
70 lines (57 loc) · 1.63 KB
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
// Package postcodesio is a golang package to ease the use, of the Postcodes.io API
// The name of the repo and the package differ since golang has isse with hyphens
// in package names
//
// Peter Holt <peter.holt@dochq.co.uk>
package postcodesio
import (
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Basic holder for API information
const (
ApiBaseUrl = "https://api.postcodes.io"
)
var (
// Package wide container for the HTTP client
httpClient *http.Client
)
// init manages the disabling of TLS verification as this can cause issues on
// some K8 clusters, as the URL is hard coded, there is no issue with this
// since any other attempt at compromise would not map to struct
// I'd love to remove this, but all the time GKE instances miss loads of root CA's
// this has to be the solution for now.
//
// Peter Holt <peter.holt@dochq.co.uk>
func init() {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient = &http.Client{Timeout: 10 * time.Second, Transport: tr}
}
// Lookup function to perform a postcode lookup, A postcode is provided as a parameter
// the postcode is searched and the corresponding retsult is sent back to the
// calling function
//
// Peter Holt <peter.holt@dochq.co.uk>
func Lookup(code string) (ResultSingle, error) {
var res ResultSingle
var err error
if code == "" {
err = fmt.Errorf("Postcode empty")
return res, err
}
resp, err := httpClient.Get(ApiBaseUrl + "/postcodes/" + code)
if err != nil {
return res, err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return res, err
}
return res, nil
}