1
|
|
|
package solr |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"bytes" |
5
|
|
|
"context" |
6
|
|
|
"encoding/json" |
7
|
|
|
"github.com/google/go-querystring/query" |
8
|
|
|
"net/http" |
9
|
|
|
"net/url" |
10
|
|
|
) |
11
|
|
|
|
12
|
|
|
const ( |
13
|
|
|
DefaultHost = "http://127.0.0.1:8983" |
|
|
|
|
14
|
|
|
DefaultContentType = "application/json" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
type Client struct { |
|
|
|
|
18
|
|
|
client *http.Client |
19
|
|
|
baseURL *url.URL |
20
|
|
|
Document DocumentAPI |
21
|
|
|
Collection CollectionAPI |
22
|
|
|
Config ConfigAPI |
23
|
|
|
onRequestCompleted RequestCompletionCallback |
24
|
|
|
username string |
25
|
|
|
password string |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
type RequestCompletionCallback func(*http.Request, *http.Response) |
|
|
|
|
29
|
|
|
|
30
|
|
|
func NewClient() Client { |
|
|
|
|
31
|
|
|
|
32
|
|
|
httpClient := http.DefaultClient |
33
|
|
|
baseURL, _ := url.Parse(DefaultHost) |
34
|
|
|
|
35
|
|
|
client := Client{ |
36
|
|
|
client: httpClient, |
37
|
|
|
baseURL: baseURL, |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
client.Initialize() |
41
|
|
|
|
42
|
|
|
return client |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
func (c *Client) Initialize() { |
|
|
|
|
46
|
|
|
document := DocumentAPI{client: c} |
47
|
|
|
c.Document = document |
48
|
|
|
collection := CollectionAPI{client: c} |
49
|
|
|
c.Collection = collection |
50
|
|
|
config := ConfigAPI{client: c} |
51
|
|
|
c.Config = config |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
func (c *Client) SetHttpClient(httpClient *http.Client) *Client { |
|
|
|
|
55
|
|
|
c.client = httpClient |
56
|
|
|
c.Initialize() |
57
|
|
|
return c |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
func (c *Client) SetBasicAuth(username string, password string) *Client { |
|
|
|
|
61
|
|
|
c.username = username |
62
|
|
|
c.password = password |
63
|
|
|
c.Initialize() |
64
|
|
|
return c |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
func (c *Client) SetBaseURL(baseURL string) *Client{ |
|
|
|
|
68
|
|
|
c.baseURL, _ = url.Parse(baseURL) |
69
|
|
|
c.Initialize() |
70
|
|
|
return c |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body interface{}, queryStrings interface{}, headers *map[string]string) (*http.Request, error) { |
|
|
|
|
74
|
|
|
u, err := c.baseURL.Parse(urlStr) |
75
|
|
|
if err != nil { |
76
|
|
|
return nil, err |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
buf := new(bytes.Buffer) |
80
|
|
|
if body != nil { |
81
|
|
|
err = json.NewEncoder(buf).Encode(body) |
82
|
|
|
if err != nil { |
83
|
|
|
return nil, err |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
params, _ := query.Values(queryStrings) |
88
|
|
|
u.RawQuery = params.Encode() |
89
|
|
|
|
90
|
|
|
req, err := http.NewRequest(method, u.String(), buf) |
91
|
|
|
if err != nil { |
92
|
|
|
return nil, err |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
req.Header.Add("Content-Type", DefaultContentType) |
96
|
|
|
req.Header.Add("Accept", DefaultContentType) |
97
|
|
|
|
98
|
|
|
if headers != nil { |
99
|
|
|
for key, value := range *headers { |
100
|
|
|
req.Header.Set(key, value) |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if c.username != "" && c.password != "" { |
105
|
|
|
req.SetBasicAuth(c.username, c.password) |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return req, nil |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
func (c *Client) NewRequestUpload(ctx context.Context, method, urlStr string, body interface{}, queryStrings interface{}) (*http.Request, error) { |
|
|
|
|
112
|
|
|
u, err := c.baseURL.Parse(urlStr) |
113
|
|
|
if err != nil { |
114
|
|
|
return nil, err |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
buf := new(bytes.Buffer) |
118
|
|
|
if body != nil { |
119
|
|
|
err = json.NewEncoder(buf).Encode(body) |
120
|
|
|
if err != nil { |
121
|
|
|
return nil, err |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
params, _ := query.Values(queryStrings) |
126
|
|
|
u.RawQuery = params.Encode() |
127
|
|
|
|
128
|
|
|
req, err := http.NewRequest(method, u.String(), buf) |
129
|
|
|
if err != nil { |
130
|
|
|
return nil, err |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
req.Header.Add("Content-Type", DefaultContentType) |
134
|
|
|
req.Header.Add("Accept", DefaultContentType) |
135
|
|
|
|
136
|
|
|
if c.username != "" && c.password != "" { |
137
|
|
|
req.SetBasicAuth(c.username, c.password) |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return req, nil |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
func (c *Client) Do(ctx context.Context, req *http.Request) (*Response, error) { |
|
|
|
|
144
|
|
|
req = req.WithContext(ctx) |
145
|
|
|
resp, err := c.client.Do(req) |
146
|
|
|
if err != nil { |
147
|
|
|
return nil, err |
148
|
|
|
} |
149
|
|
|
if c.onRequestCompleted != nil { |
150
|
|
|
c.onRequestCompleted(req, resp) |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
defer func() { |
154
|
|
|
if rerr := resp.Body.Close(); err == nil { |
155
|
|
|
err = rerr |
156
|
|
|
} |
157
|
|
|
}() |
158
|
|
|
|
159
|
|
|
response := Response{HttpResponse: resp} |
160
|
|
|
|
161
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response) |
162
|
|
|
if err != nil { |
163
|
|
|
return nil, err |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return &response, nil |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
func (c *Client) OnRequestCompleted(rc RequestCompletionCallback) { |
|
|
|
|
170
|
|
|
c.onRequestCompleted = rc |
171
|
|
|
} |