Test Setup Failed
Push — master ( 18d1be...9c409f )
by Adriano
01:19
created

solr.*Client.Initialize   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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"
1 ignored issue
show
introduced by
exported const DefaultHost should have comment (or a comment on this block) or be unexported
Loading history...
14
	DefaultContentType = "application/json"
15
)
16
17
type Client struct {
1 ignored issue
show
introduced by
exported type Client should have comment or be unexported
Loading history...
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)
1 ignored issue
show
introduced by
exported type RequestCompletionCallback should have comment or be unexported
Loading history...
28
29
func NewClient() Client {
1 ignored issue
show
introduced by
exported function NewClient should have comment or be unexported
Loading history...
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() {
1 ignored issue
show
introduced by
exported method Client.Initialize should have comment or be unexported
Loading history...
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 {
2 ignored issues
show
introduced by
exported method Client.SetHttpClient should have comment or be unexported
Loading history...
introduced by
method SetHttpClient should be SetHTTPClient
Loading history...
52
	c.client = httpClient
53
	c.Initialize()
54
	return c
55
}
56
57
func (c *Client) SetBasicAuth(username string, password string) *Client {
1 ignored issue
show
introduced by
exported method Client.SetBasicAuth should have comment or be unexported
Loading history...
58
	c.username = username
59
	c.password = password
60
	c.Initialize()
61
	return c
62
}
63
64
func (c *Client) SetBaseURL(baseURL string) *Client{
1 ignored issue
show
introduced by
exported method Client.SetBaseURL should have comment or be unexported
Loading history...
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) {
1 ignored issue
show
introduced by
exported method Client.NewRequest should have comment or be unexported
Loading history...
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) {
1 ignored issue
show
introduced by
exported method Client.Do should have comment or be unexported
Loading history...
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) {
1 ignored issue
show
introduced by
exported method Client.OnRequestCompleted should have comment or be unexported
Loading history...
129
	c.onRequestCompleted = rc
130
}