-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcover.go
More file actions
106 lines (88 loc) · 2.01 KB
/
cover.go
File metadata and controls
106 lines (88 loc) · 2.01 KB
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
99
100
101
102
103
104
105
106
package openlibrary
import (
"errors"
"net/http"
"path"
)
type CoverKind string
const (
ISBNCover CoverKind = "isbn"
OCLCCover CoverKind = "oclc"
LCCNCover CoverKind = "lccn"
OLIDCover CoverKind = "olid"
IDCover CoverKind = "id"
)
type CoverSize string
const (
CoverSmall CoverSize = "S"
CoverMedium CoverSize = "M"
CoverLarge CoverSize = "L"
)
type CoverAPI struct {
openlibraryClient *Client
value string
kind CoverKind
size CoverSize
}
func (c *Client) Cover() *CoverAPI {
api := &CoverAPI{
openlibraryClient: c,
kind: ISBNCover,
size: CoverMedium,
}
api.openlibraryClient.changeBaseUrl(coverBaseURL)
return api
}
func (api *CoverAPI) ISBN(isbn string) *CoverAPI {
api.kind = ISBNCover
api.value = isbn
return api
}
func (api *CoverAPI) OCLC(oclc string) *CoverAPI {
api.kind = OCLCCover
api.value = oclc
return api
}
func (api *CoverAPI) LCCN(lccn string) *CoverAPI {
api.kind = LCCNCover
api.value = lccn
return api
}
func (api *CoverAPI) OLID(olid string) *CoverAPI {
api.kind = OLIDCover
api.value = olid
return api
}
func (api *CoverAPI) ID(id string) *CoverAPI {
api.kind = IDCover
api.value = id
return api
}
func (api *CoverAPI) Small() *CoverAPI {
api.size = CoverSmall
return api
}
func (api *CoverAPI) Medium() *CoverAPI {
api.size = CoverMedium
return api
}
func (api *CoverAPI) Large() *CoverAPI {
api.size = CoverLarge
return api
}
func (api *CoverAPI) Get() (imgBytes []byte, mimeType string, err error) {
if api.value == "" {
return imgBytes, mimeType, errors.New("No value")
}
endpoint := path.Join("/b", string(api.kind), api.value+"-"+string(api.size)) + ".jpg?default=false"
res, err := api.openlibraryClient.httpClient.R().Get(endpoint)
if err != nil {
return
}
if res.StatusCode() != 200 {
return
}
imgBytes = res.Bytes()
mimeType = http.DetectContentType(imgBytes)
return
}