Test Setup Failed
Pull Request — master (#2)
by Adriano
01:24
created

solr.*DocumentAPI.Update   A

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nop 4
dl 0
loc 15
rs 9.95
c 0
b 0
f 0
1
package solr
2
3
import (
4
	"bytes"
5
	"context"
6
	"fmt"
7
	"io"
8
	"log"
9
	"mime/multipart"
10
	"net/http"
11
	"os"
12
	"path/filepath"
13
)
14
15
type Parameters struct {
1 ignored issue
show
introduced by
exported type Parameters should have comment or be unexported
Loading history...
16
	CommitWithin 	int 						`url:"commitWithin,omitempty"`
17
	Commit 			bool 						`url:"commit,omitempty"`
18
	Query 			string 						`url:"q,omitempty"`
19
	Delete 			interface{} 				`url:"delete,omitempty"`
20
	LiteralId 		string 						`url:"literal.id,omitempty"`
1 ignored issue
show
introduced by
struct field LiteralId should be LiteralID
Loading history...
21
}
22
23
type Delete struct {
1 ignored issue
show
introduced by
exported type Delete should have comment or be unexported
Loading history...
24
	Id 				string 						`json:"id,omitempty"`
1 ignored issue
show
introduced by
struct field Id should be ID
Loading history...
25
	Query 			string 						`json:"query,omitempty"`
26
}
27
28
type Document map[string]interface{}
1 ignored issue
show
introduced by
exported type Document should have comment or be unexported
Loading history...
29
30
type DocumentAPI struct {
1 ignored issue
show
introduced by
exported type DocumentAPI should have comment or be unexported
Loading history...
31
	client *Client
32
}
33
34
func (d *DocumentAPI) Select(ctx context.Context, collection string, query string) (*Response, error) {
1 ignored issue
show
introduced by
exported method DocumentAPI.Select should have comment or be unexported
Loading history...
35
	path := fmt.Sprintf("/solr/%s/select", collection)
36
37
	req, err := d.client.NewRequest(ctx, http.MethodGet, path, nil, &Parameters{
38
		Query:	query,
39
	}, nil)
40
	if err != nil {
41
		return nil, err
42
	}
43
44
	response, err := d.client.Do(ctx, req)
45
	if err != nil {
46
		return nil, err
47
	}
48
49
	return response, err
50
}
51
52
func (d *DocumentAPI) Update(ctx context.Context, collection string, docs []Document, params *Parameters) (*Response, error) {
1 ignored issue
show
introduced by
exported method DocumentAPI.Update should have comment or be unexported
Loading history...
53
54
	path := fmt.Sprintf("/api/collections/%s/update/json", collection)
55
56
	req, err := d.client.NewRequest(ctx, http.MethodPost, path, docs, params, nil)
57
	if err != nil {
58
		return nil, err
59
	}
60
61
	response, err := d.client.Do(ctx, req)
62
	if err != nil {
63
		return nil, err
64
	}
65
66
	return response, err
67
}
68
69
func (d *DocumentAPI) Commit(ctx context.Context, collection string) (*Response, error) {
1 ignored issue
show
introduced by
exported method DocumentAPI.Commit should have comment or be unexported
Loading history...
70
71
	path := fmt.Sprintf("/api/collections/%s/update/json", collection)
72
73
	req, err := d.client.NewRequest(ctx, http.MethodPost, path, nil, Parameters{
74
		Commit:       true,
75
	}, nil)
76
	if err != nil {
77
		return nil, err
78
	}
79
80
	response, err := d.client.Do(ctx, req)
81
	if err != nil {
82
		return nil, err
83
	}
84
85
	return response, err
86
}
87
88
func (d *DocumentAPI) Delete(ctx context.Context, collection string, delete Delete, params *Parameters) (*Response, error) {
1 ignored issue
show
introduced by
exported method DocumentAPI.Delete should have comment or be unexported
Loading history...
89
90
	path := fmt.Sprintf("/solr/%s/update", collection)
91
92
	req, err := d.client.NewRequest(ctx, http.MethodPost, path, map[string]interface{}{
93
		"delete": delete,
94
	}, params, nil)
95
	if err != nil {
96
		return nil, err
97
	}
98
99
	response, err := d.client.Do(ctx, req)
100
	if err != nil {
101
		return nil, err
102
	}
103
104
	return response, err
105
}
106
107
func (d *DocumentAPI) Extract(ctx context.Context, collection string, filename string, params *Parameters) (*Response, error) {
1 ignored issue
show
introduced by
exported method DocumentAPI.Extract should have comment or be unexported
Loading history...
108
	file, err := os.Open(filename)
109
	if err != nil {
110
		log.Fatal(err)
111
	}
112
	defer file.Close()
113
114
	body := &bytes.Buffer{}
115
	writer := multipart.NewWriter(body)
116
	part, err := writer.CreateFormFile("document", filepath.Base(file.Name()))
117
	if err != nil {
118
		log.Fatal(err)
119
	}
120
121
	_, err = io.Copy(part, file)
122
	if err != nil {
123
		return nil, err
124
	}
125
126
	err = writer.Close()
127
	if err != nil {
128
		return nil, err
129
	}
130
131
	path := fmt.Sprintf("/api/collections/%s/update/extract", collection)
132
133
	req, err := d.client.NewRequest(ctx, http.MethodPost, path, body, params, &map[string]string{
134
		"Content-Type": "application/octet-stream",
135
	})
136
	if err != nil {
137
		return nil, err
138
	}
139
140
	response, err := d.client.Do(ctx, req)
141
	if err != nil {
142
		return nil, err
143
	}
144
145
	return response, err
146
}