-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
90 lines (75 loc) · 1.98 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
package convert
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
)
const (
bgsAPIURL = "https://www.bgs.ac.uk/data/webservices/CoordConvert_LL_BNG.cfc"
)
// Client struct is used to create convert client to make conversion requests
type Client struct {
// Base URL for API requests.
BaseURL *url.URL
httpClient *http.Client
}
// Response is API response. This wraps the standard http.Response
// returned from API
/*
type Response struct {
Response *http.Response
Data json.RawMessage `json:"data,omitempty"`
}
*/
//NewClient will return a new cleint, if no http client is provied a default is created.
func NewClient(client *http.Client) *Client {
if client == nil {
client = &http.Client{}
}
c := &Client{httpClient: client}
c.BaseURL, _ = url.Parse(bgsAPIURL)
return c
}
// NewRequest creates an API request. A relative URL can be provided in urlPath,
// in which case it is resolved relative to the BaseURL of the Client.
func (c *Client) NewRequest(method, urlPath string) (*http.Request, error) {
u, err := c.BaseURL.Parse(urlPath)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, u.String(), nil)
if err != nil {
return nil, err
}
return req, nil
}
// Do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred.
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*http.Response, error) {
req = req.WithContext(ctx)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err != nil {
return resp, err
}
// StatusNoContent, No Content.
if resp.StatusCode != http.StatusNoContent {
body, err := ioutil.ReadAll(resp.Body)
if err != nil || body == nil {
return resp, err
}
if v != nil {
err = json.Unmarshal(body, v)
if err != nil {
return &http.Response{}, err
}
}
}
return &http.Response{}, err
}