|
1
|
|
|
package solr |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"bytes" |
|
5
|
|
|
"context" |
|
6
|
|
|
"encoding/json" |
|
7
|
|
|
"github.com/google/go-querystring/query" |
|
8
|
|
|
"io/ioutil" |
|
9
|
|
|
"log" |
|
10
|
|
|
"net/http" |
|
11
|
|
|
"net/url" |
|
12
|
|
|
"os" |
|
13
|
|
|
) |
|
14
|
|
|
|
|
15
|
|
|
const ( |
|
16
|
|
|
DefaultHost = "http://127.0.0.1:8983" |
|
17
|
|
|
DefaultContentType = "application/json" |
|
18
|
|
|
) |
|
19
|
|
|
|
|
20
|
|
|
type Client struct { |
|
21
|
|
|
client *http.Client |
|
22
|
|
|
baseURL *url.URL |
|
23
|
|
|
Document DocumentAPI |
|
24
|
|
|
Collection CollectionAPI |
|
25
|
|
|
Config ConfigAPI |
|
26
|
|
|
onRequestCompleted RequestCompletionCallback |
|
27
|
|
|
username string |
|
28
|
|
|
password string |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
type RequestCompletionCallback func(*http.Request, *http.Response) |
|
32
|
|
|
|
|
33
|
|
|
func NewClient() Client { |
|
34
|
|
|
|
|
35
|
|
|
httpClient := http.DefaultClient |
|
36
|
|
|
baseURL, _ := url.Parse(DefaultHost) |
|
37
|
|
|
|
|
38
|
|
|
client := Client{ |
|
39
|
|
|
client: httpClient, |
|
40
|
|
|
baseURL: baseURL, |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
client.Initialize() |
|
44
|
|
|
|
|
45
|
|
|
return client |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
func (c *Client) Initialize() { |
|
49
|
|
|
document := DocumentAPI{client: c} |
|
50
|
|
|
c.Document = document |
|
51
|
|
|
collection := CollectionAPI{client: c} |
|
52
|
|
|
c.Collection = collection |
|
53
|
|
|
config := ConfigAPI{client: c} |
|
54
|
|
|
c.Config = config |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
func (c *Client) SetHttpClient(httpClient *http.Client) *Client { |
|
58
|
|
|
c.client = httpClient |
|
59
|
|
|
c.Initialize() |
|
60
|
|
|
return c |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
func (c *Client) SetBasicAuth(username string, password string) *Client { |
|
64
|
|
|
c.username = username |
|
65
|
|
|
c.password = password |
|
66
|
|
|
c.Initialize() |
|
67
|
|
|
return c |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
func (c *Client) SetBaseURL(baseURL string) *Client{ |
|
71
|
|
|
c.baseURL, _ = url.Parse(baseURL) |
|
72
|
|
|
c.Initialize() |
|
73
|
|
|
return c |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
func (c *Client) NewUpload(ctx context.Context, urlStr string, filepath string, queryStrings interface{}) (*Response, error) { |
|
77
|
|
|
u, err := c.baseURL.Parse(urlStr) |
|
78
|
|
|
if err != nil { |
|
79
|
|
|
return nil, err |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
params, _ := query.Values(queryStrings) |
|
83
|
|
|
u.RawQuery = params.Encode() |
|
84
|
|
|
|
|
85
|
|
|
file, err := os.Open(filepath) |
|
86
|
|
|
if err != nil { |
|
87
|
|
|
log.Fatal(err) |
|
88
|
|
|
} |
|
89
|
|
|
defer file.Close() |
|
90
|
|
|
|
|
91
|
|
|
resp, err := http.Post(u.String(), "application/octet-stream", file) |
|
92
|
|
|
if err != nil { |
|
93
|
|
|
return nil, err |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
b, err := ioutil.ReadAll(resp.Body) |
|
97
|
|
|
defer resp.Body.Close() |
|
98
|
|
|
if err != nil { |
|
99
|
|
|
return nil, err |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
response := Response{HttpResponse: resp} |
|
103
|
|
|
|
|
104
|
|
|
err = json.Unmarshal(b, &response) |
|
105
|
|
|
if err != nil { |
|
106
|
|
|
return nil, err |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return &response, nil |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body interface{}, queryStrings interface{}, headers *map[string]string) (*http.Request, error) { |
|
113
|
|
|
u, err := c.baseURL.Parse(urlStr) |
|
114
|
|
|
if err != nil { |
|
115
|
|
|
return nil, err |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
buf := new(bytes.Buffer) |
|
119
|
|
|
if body != nil { |
|
120
|
|
|
err = json.NewEncoder(buf).Encode(body) |
|
121
|
|
|
if err != nil { |
|
122
|
|
|
return nil, err |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
params, _ := query.Values(queryStrings) |
|
127
|
|
|
u.RawQuery = params.Encode() |
|
128
|
|
|
|
|
129
|
|
|
req, err := http.NewRequest(method, u.String(), buf) |
|
130
|
|
|
if err != nil { |
|
131
|
|
|
return nil, err |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
req.Header.Add("Content-Type", DefaultContentType) |
|
135
|
|
|
|
|
136
|
|
|
if headers != nil { |
|
137
|
|
|
for key, value := range *headers { |
|
138
|
|
|
req.Header.Set(key, value) |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
if c.username != "" && c.password != "" { |
|
143
|
|
|
req.SetBasicAuth(c.username, c.password) |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return req, nil |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
func (c *Client) NewRequestUpload(ctx context.Context, method, urlStr string, body interface{}, queryStrings interface{}) (*http.Request, error) { |
|
150
|
|
|
u, err := c.baseURL.Parse(urlStr) |
|
151
|
|
|
if err != nil { |
|
152
|
|
|
return nil, err |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
buf := new(bytes.Buffer) |
|
156
|
|
|
if body != nil { |
|
157
|
|
|
err = json.NewEncoder(buf).Encode(body) |
|
158
|
|
|
if err != nil { |
|
159
|
|
|
return nil, err |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
params, _ := query.Values(queryStrings) |
|
164
|
|
|
u.RawQuery = params.Encode() |
|
165
|
|
|
|
|
166
|
|
|
req, err := http.NewRequest(method, u.String(), buf) |
|
167
|
|
|
if err != nil { |
|
168
|
|
|
return nil, err |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
req.Header.Add("Content-Type", DefaultContentType) |
|
172
|
|
|
req.Header.Add("Accept", DefaultContentType) |
|
173
|
|
|
|
|
174
|
|
|
if c.username != "" && c.password != "" { |
|
175
|
|
|
req.SetBasicAuth(c.username, c.password) |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return req, nil |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
func (c *Client) Do(ctx context.Context, req *http.Request) (*Response, error) { |
|
182
|
|
|
req = req.WithContext(ctx) |
|
183
|
|
|
resp, err := c.client.Do(req) |
|
184
|
|
|
if err != nil { |
|
185
|
|
|
return nil, err |
|
186
|
|
|
} |
|
187
|
|
|
if c.onRequestCompleted != nil { |
|
188
|
|
|
c.onRequestCompleted(req, resp) |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
defer func() { |
|
192
|
|
|
if rerr := resp.Body.Close(); err == nil { |
|
193
|
|
|
err = rerr |
|
194
|
|
|
} |
|
195
|
|
|
}() |
|
196
|
|
|
|
|
197
|
|
|
b, err := ioutil.ReadAll(resp.Body) |
|
198
|
|
|
defer resp.Body.Close() |
|
199
|
|
|
if err != nil { |
|
200
|
|
|
return nil, err |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
response := Response{HttpResponse: resp} |
|
204
|
|
|
|
|
205
|
|
|
err = json.Unmarshal(b, &response) |
|
206
|
|
|
if err != nil { |
|
207
|
|
|
return nil, err |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return &response, nil |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
//func (c *Client) Do(ctx context.Context, req *http.Request) (*Response, error) { |
|
214
|
|
|
// req = req.WithContext(ctx) |
|
215
|
|
|
// resp, err := c.client.Do(req) |
|
216
|
|
|
// if err != nil { |
|
217
|
|
|
// return nil, err |
|
218
|
|
|
// } |
|
219
|
|
|
// if c.onRequestCompleted != nil { |
|
220
|
|
|
// c.onRequestCompleted(req, resp) |
|
221
|
|
|
// } |
|
222
|
|
|
// |
|
223
|
|
|
// defer func() { |
|
224
|
|
|
// if rerr := resp.Body.Close(); err == nil { |
|
225
|
|
|
// err = rerr |
|
226
|
|
|
// } |
|
227
|
|
|
// }() |
|
228
|
|
|
// |
|
229
|
|
|
// response := Response{HttpResponse: resp} |
|
230
|
|
|
// |
|
231
|
|
|
// err = json.NewDecoder(resp.Body).Decode(&response) |
|
232
|
|
|
// if err != nil { |
|
233
|
|
|
// return nil, err |
|
234
|
|
|
// } |
|
235
|
|
|
// |
|
236
|
|
|
// return &response, nil |
|
237
|
|
|
//} |
|
238
|
|
|
|
|
239
|
|
|
func (c *Client) OnRequestCompleted(rc RequestCompletionCallback) { |
|
240
|
|
|
c.onRequestCompleted = rc |
|
241
|
|
|
} |