-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery.go
66 lines (56 loc) · 1.55 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
package gol
import (
"fmt"
)
type QueryURL struct {
url string
querytype string
author string
limit string
work string
title string
}
// QueryURL returns QueryURL type that holds OpenLibrary Query URL
func QueryUrl() QueryURL {
return QueryURL{url: "https://openlibrary.org/query.json?"}
}
// Construct returns the string represtation of the QueryURL
func (q QueryURL) Construct() string {
return fmt.Sprintf("%s%s%s%s%s%s", q.url, q.querytype, q.author, q.limit, q.work, q.title)
}
// Type returns a QueryURL with the type that has be passed as an arg
func (q QueryURL) Type(s string) QueryURL {
q.querytype = fmt.Sprintf("&type=/type/%s", s)
return q
}
// Author returns a QueryURL with the author that has be passed as an arg
func (q QueryURL) Author(author string) QueryURL {
q.author = fmt.Sprintf("&authors=/authors/%s", author)
return q
}
// Limit sets the number limit of the Query
func (q QueryURL) Limit(l int) QueryURL {
q.limit = fmt.Sprintf("&limit=%d", l)
return q
}
// Work sets the work key of the Query
func (q QueryURL) Work(w string) QueryURL {
q.work = fmt.Sprintf("&works=/works/%s", w)
return q
}
// Title sets the title key of the query
func (q QueryURL) Title(t string) QueryURL {
q.title = fmt.Sprintf("&title=%s", t)
return q
}
// Query returns the result of the query from a url -- Constructed by Construct()
func Query(url string) (Container, error) {
result, err := Request(url)
if err != nil {
return nil, err
}
if err := HasError(result); err != nil {
return nil, err
}
return result, nil
}