-
Notifications
You must be signed in to change notification settings - Fork 1
/
query.go
74 lines (59 loc) · 1.53 KB
/
query.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
package crossref
import (
"encoding/json"
"io/ioutil"
log "github.com/sirupsen/logrus"
)
// QueryJSON gets the raw JSON from CrossRef. A search term must be specified,
// otherwise it will return an ErrEmptyQuery error. Use this if you want to
// manually manipulate the JSON data.
func (c *Client) QueryJSON(search string) ([]byte, error) {
log.WithFields(log.Fields{
"client": c,
}).Debug("Query requested")
if search == "" {
return nil, ErrEmptyQuery
}
req, err := c.newRequest("GET", api, nil)
if err != nil {
return nil, err
}
q := req.URL.Query()
q.Add("query", search)
q.Add("rows", "10")
q.Add("mailto", c.mailto)
q.Add("sort", "relevance")
req.URL.RawQuery = q.Encode()
log.WithFields(log.Fields{
"url": req.URL.String(),
}).Debug("Requesting information")
resp, err := c.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
raw, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return raw, nil
}
// Query returns a list of processed Work metadata from a search query. A
// search term must be specified, otherwise it will return an ErrEmptyQuery
// error. If no works are found, it returns an ErrZeroWorks error.
func (c *Client) Query(search string) ([]*Work, error) {
js, err := c.QueryJSON(search)
works := []*Work{}
if err != nil {
return works, err
}
var data queryJSON
json.Unmarshal(js, &data)
if data.Message.TotalResults == 0 {
return works, ErrZeroWorks
}
for _, work := range data.Message.Items {
work.parse()
}
return data.Message.Items, nil
}