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