-
Notifications
You must be signed in to change notification settings - Fork 23
/
search.go
98 lines (92 loc) · 2.54 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package explorer
import (
"bytes"
"context"
"errors"
"fmt"
"net/http"
"github.com/jpfielding/gorets/pkg/config"
"github.com/jpfielding/gorets/pkg/rets"
)
// SearchArgs ...
type SearchArgs struct {
Connection config.Config `json:"connection"`
Resource string `json:"resource"`
Class string `json:"class"`
Format string `json:"format"`
Select string `json:"select"`
CountType int `json:"counttype"`
Offset int `json:"offset"`
Limit int `json:"limit"`
Query string `json:"query"`
QueryType string `json:"querytype"`
}
// SearchPage ...
type SearchPage struct {
Columns rets.Row `json:"columns"`
Rows []rets.Row `json:"rows"`
MaxRows bool `json:"maxrows"`
Count int `json:"count"`
Wirelog []byte `json:"wirelog,omitempty"`
}
// SearchService ...
type SearchService struct{}
// Run ....
func (ms SearchService) Run(r *http.Request, args *SearchArgs, reply *SearchPage) error {
fmt.Printf("search run params: %v\n", args)
if args.QueryType == "" {
args.QueryType = "DMQL2"
}
if args.Format == "" {
args.Format = "COMPACT_DECODED"
}
cfg := args.Connection
ctx := context.Background()
wirelog := bytes.Buffer{}
sess, err := cfg.Connect(ctx, &wirelog)
if err != nil {
return err
}
defer sess.Close()
fmt.Printf("%v\n", cfg)
return sess.Process(ctx, func(r rets.Requester, u rets.CapabilityURLs) error {
req := rets.SearchRequest{
URL: u.Search,
SearchParams: rets.SearchParams{
Select: args.Select,
Query: args.Query,
SearchType: args.Resource,
Class: args.Class,
Format: args.Format,
QueryType: args.QueryType,
Count: args.CountType,
Limit: args.Limit,
Offset: args.Offset,
},
}
fmt.Printf("Querying : %v\n", req)
result, err := rets.SearchCompact(ctx, r, req)
defer result.Close()
if err != nil {
reply.Wirelog = wirelog.Bytes()
return err
}
// non success rets codes should return an error
switch result.Response.Code {
case rets.StatusOK, rets.StatusNoRecords:
default: // shit hit the fan
fmt.Printf("Querying : %v\n", result.Response.Text)
return errors.New(result.Response.Text)
}
// opening the strea
reply.Columns = result.Columns
// too late to err in http here, need another solution
reply.MaxRows, err = result.ForEach(func(row rets.Row, err error) error {
reply.Rows = append(reply.Rows, row)
return err
})
reply.Count = result.Count
reply.Wirelog = wirelog.Bytes()
return err
})
}