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
|
|
|
onRequestCompleted RequestCompletionCallback |
23
|
|
|
username string |
24
|
|
|
password string |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
type RequestCompletionCallback func(*http.Request, *http.Response) |
|
|
|
|
28
|
|
|
|
29
|
|
|
func NewClient() Client { |
|
|
|
|
30
|
|
|
|
31
|
|
|
httpClient := http.DefaultClient |
32
|
|
|
baseURL, _ := url.Parse(DefaultHost) |
33
|
|
|
|
34
|
|
|
client := Client{ |
35
|
|
|
client: httpClient, |
36
|
|
|
baseURL: baseURL, |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
client.Initialize() |
40
|
|
|
|
41
|
|
|
return client |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
func (c *Client) Initialize() { |
|
|
|
|
45
|
|
|
document := DocumentAPI{client: c} |
46
|
|
|
c.Document = document |
47
|
|
|
collection := CollectionAPI{client: c} |
48
|
|
|
c.Collection = collection |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
func (c *Client) SetHttpClient(httpClient *http.Client) *Client { |
|
|
|
|
52
|
|
|
c.client = httpClient |
53
|
|
|
c.Initialize() |
54
|
|
|
return c |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
func (c *Client) SetBasicAuth(username string, password string) *Client { |
|
|
|
|
58
|
|
|
c.username = username |
59
|
|
|
c.password = password |
60
|
|
|
c.Initialize() |
61
|
|
|
return c |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
func (c *Client) SetBaseURL(baseURL string) *Client{ |
|
|
|
|
65
|
|
|
c.baseURL, _ = url.Parse(baseURL) |
66
|
|
|
c.Initialize() |
67
|
|
|
return c |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body interface{}, queryStrings interface{}) (*http.Request, error) { |
|
|
|
|
71
|
|
|
u, err := c.baseURL.Parse(urlStr) |
72
|
|
|
if err != nil { |
73
|
|
|
return nil, err |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
buf := new(bytes.Buffer) |
77
|
|
|
if body != nil { |
78
|
|
|
err = json.NewEncoder(buf).Encode(body) |
79
|
|
|
if err != nil { |
80
|
|
|
return nil, err |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
params, _ := query.Values(queryStrings) |
85
|
|
|
u.RawQuery = params.Encode() |
86
|
|
|
|
87
|
|
|
req, err := http.NewRequest(method, u.String(), buf) |
88
|
|
|
if err != nil { |
89
|
|
|
return nil, err |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
req.Header.Add("Content-Type", DefaultContentType) |
93
|
|
|
req.Header.Add("Accept", DefaultContentType) |
94
|
|
|
|
95
|
|
|
if c.username != "" && c.password != "" { |
96
|
|
|
req.SetBasicAuth(c.username, c.password) |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return req, nil |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
func (c *Client) Do(ctx context.Context, req *http.Request) (*Response, error) { |
|
|
|
|
103
|
|
|
req = req.WithContext(ctx) |
104
|
|
|
resp, err := c.client.Do(req) |
105
|
|
|
if err != nil { |
106
|
|
|
return nil, err |
107
|
|
|
} |
108
|
|
|
if c.onRequestCompleted != nil { |
109
|
|
|
c.onRequestCompleted(req, resp) |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
defer func() { |
113
|
|
|
if rerr := resp.Body.Close(); err == nil { |
114
|
|
|
err = rerr |
115
|
|
|
} |
116
|
|
|
}() |
117
|
|
|
|
118
|
|
|
response := Response{HttpResponse: resp} |
119
|
|
|
|
120
|
|
|
err = json.NewDecoder(resp.Body).Decode(&response) |
121
|
|
|
if err != nil { |
122
|
|
|
return nil, err |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return &response, nil |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
func (c *Client) OnRequestCompleted(rc RequestCompletionCallback) { |
|
|
|
|
129
|
|
|
c.onRequestCompleted = rc |
130
|
|
|
} |