-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestclient.go
287 lines (234 loc) · 8.5 KB
/
restclient.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package chapi
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"github.com/jimsmart/chapi/ch"
)
var APIKey = ""
// BaseURL of Companies House REST API.
var BaseURL = "https://api.companieshouse.gov.uk"
// DefaultHTTPClient for requests.
//
// To use an http.Client with different settings, either override this global variable,
// or instantiate a Client (or RESTClient) instance with the appropriate local configuration.
var DefaultHTTPClient = &http.Client{}
// RESTClient provides low-level access to the Companies House API, with all methods returning
// the body of the HTTP response in []byte format (raw JSON).
//
// To work with this same API but with unmarshalled JSON structs instead, see Client.
//
// In all methods: should an otherwise successful HTTP request return a response containing
// a status that is not 2xx or 3xx, an error value of type RESTStatusError will be returned.
type RESTClient struct {
APIKey string
HTTPClient *http.Client
}
// RESTStatusError is returned as an error value if the http.Response
// has a StatusCode that is not 2xx or 3xx.
//
// If possible, any content found in the response body is unmarshalled into ErrorResource.
type RESTStatusError struct {
Status string
StatusCode int
Body []byte
ErrorResource *ch.ErrorResource
}
func (e *RESTStatusError) Error() string {
msg := e.Status
if len(e.Body) > 2 {
msg += fmt.Sprintf(" %s %+v", string(e.Body), e.ErrorResource)
}
return msg
}
// newRestStatusError creates a new instance of RESTStatusError,
// and attempts to unmarshall the body into an ErrorResource.
func newRESTStatusError(status string, statusCode int, body []byte) *RESTStatusError {
rerr := &RESTStatusError{
Status: status,
StatusCode: statusCode,
Body: body,
}
if len(body) == 0 {
return rerr
}
var r ch.ErrorResource
err := json.Unmarshal(body, &r)
if err != nil {
log.Printf("json.Unmarshal error %s for %s", err, body)
// panic(err)
return rerr
}
rerr.ErrorResource = &r
return rerr
}
//
func (c *RESTClient) Search(q string, limit, offset int) ([]byte, error) {
params := searchParams(q, limit, offset)
return c.request("/search", params)
}
func (c *RESTClient) SearchCompanies(q string, limit, offset int) ([]byte, error) {
params := searchParams(q, limit, offset)
return c.request("/search/companies", params)
}
func (c *RESTClient) SearchOfficers(q string, limit, offset int) ([]byte, error) {
params := searchParams(q, limit, offset)
return c.request("/search/officers", params)
}
func (c *RESTClient) SearchDisqualifiedOfficers(q string, limit, offset int) ([]byte, error) {
params := searchParams(q, limit, offset)
return c.request("/search/disqualified-officers", params)
}
func (c *RESTClient) CompanyProfile(companyNumber string) ([]byte, error) {
return c.request("/company/"+companyNumber, nil)
}
func (c *RESTClient) CompanyRegisteredOfficeAddress(companyNumber string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/registered-office-address", nil)
}
func (c *RESTClient) CompanyOfficers(companyNumber, registerType, sortOrder string, limit, offset int) ([]byte, error) {
params := listParams(limit, offset)
if len(registerType) != 0 {
params.Add("register_view", "true")
params.Add("register_type", registerType)
}
if len(sortOrder) != 0 {
params.Add("order_by", sortOrder)
}
return c.request("/company/"+companyNumber+"/officers", params)
}
func (c *RESTClient) CompanyFilingHistory(companyNumber, categoryFilter string, limit, offset int) ([]byte, error) {
params := listParams(limit, offset)
if len(categoryFilter) != 0 {
params.Add("category", categoryFilter)
}
return c.request("/company/"+companyNumber+"/filing-history", params)
}
func (c *RESTClient) CompanyFilingHistoryTransaction(companyNumber, transactionID string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/filing-history/"+transactionID, nil)
}
func (c *RESTClient) CompanyInsolvency(companyNumber string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/insolvency", nil)
}
func (c *RESTClient) CompanyCharges(companyNumber string, limit, offset int) ([]byte, error) {
params := listParams(limit, offset)
return c.request("/company/"+companyNumber+"/charges", params)
}
func (c *RESTClient) CompanyCharge(companyNumber, chargeID string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/charges/"+chargeID, nil)
}
func (c *RESTClient) OfficerAppointments(officerID string, limit, offset int) ([]byte, error) {
params := listParams(limit, offset)
return c.request("/officers/"+officerID+"/appointments", params)
}
func (c *RESTClient) OfficerNaturalDisqualifications(officerID string) ([]byte, error) {
return c.request("/disqualified-officers/natural/"+officerID, nil)
}
func (c *RESTClient) OfficerCorporateDisqualifications(officerID string) ([]byte, error) {
return c.request("/disqualified-officers/corporate/"+officerID, nil)
}
func (c *RESTClient) CompanyUKEstablishments(companyNumber string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/uk-establishments", nil)
}
func (c *RESTClient) PSCs(companyNumber string, registerView bool, limit, offset int) ([]byte, error) {
params := listParams(limit, offset)
if registerView {
params.Add("register_view", "true")
}
return c.request("/company/"+companyNumber+"/persons-with-significant-control", params)
}
func (c *RESTClient) PSCIndividual(companyNumber, pscID string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/persons-with-significant-control/individual/"+pscID, nil)
}
func (c *RESTClient) PSCCorporateEntity(companyNumber, pscID string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/persons-with-significant-control/corporate-entity/"+pscID, nil)
}
func (c *RESTClient) PSCLegalPerson(companyNumber, pscID string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/persons-with-significant-control/legal-person/"+pscID, nil)
}
func (c *RESTClient) PSCStatements(companyNumber string, registerView bool, limit, offset int) ([]byte, error) {
params := listParams(limit, offset)
if registerView {
params.Add("register_view", "true")
}
return c.request("/company/"+companyNumber+"/persons-with-significant-control-statements", params)
}
func (c *RESTClient) PSCStatement(companyNumber, statementID string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/persons-with-significant-control-statements/"+statementID, nil)
}
func (c *RESTClient) PSCSuperSecure(companyNumber, superSecureID string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/persons-with-significant-control/super-secure/"+superSecureID, nil)
}
func (c *RESTClient) CompanyRegisters(companyNumber string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/registers", nil)
}
func (c *RESTClient) CompanyExemptions(companyNumber string) ([]byte, error) {
return c.request("/company/"+companyNumber+"/exemptions", nil)
}
func (c *RESTClient) DocumentMetadata(documentID string) ([]byte, error) {
return c.request("/document/"+documentID, nil)
}
func (c *RESTClient) DocumentContent(documentID string) ([]byte, error) {
// TODO(js) Does DocumentContent need a param for setting Accept content-type header?
return c.request("/document/"+documentID+"/content", nil)
}
//
func (c *RESTClient) request(urlSlug string, params url.Values) ([]byte, error) {
apiKey := c.APIKey
if len(apiKey) == 0 {
apiKey = APIKey
if len(apiKey) == 0 {
panic("missing APIKey")
}
}
req, err := http.NewRequest("GET", BaseURL+urlSlug, nil)
if err != nil {
return nil, err
}
req.SetBasicAuth(apiKey, "")
if params != nil {
req.URL.RawQuery = params.Encode()
}
httpClient := c.HTTPClient
if httpClient == nil {
httpClient = DefaultHTTPClient
}
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
if !success(res) {
return nil, newRESTStatusError(res.Status, res.StatusCode, body)
}
return body, nil
}
// success returns true if Response.Status is 2xx or 3xx.
func success(r *http.Response) bool {
s := r.Status[:1]
return s == "2" || s == "3"
}
//
func listParams(limit, offset int) url.Values {
params := make(url.Values)
if limit != -1 {
params.Add("items_per_page", strconv.Itoa(limit))
}
if offset != -1 {
params.Add("start_index", strconv.Itoa(offset))
}
return params
}
func searchParams(q string, limit, offset int) url.Values {
params := listParams(limit, offset)
params.Add("q", q)
return params
}
//