-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbook.go
56 lines (47 loc) · 1005 Bytes
/
book.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
package bibliotheca
import (
"encoding/json"
"errors"
"log"
"net/http"
"net/url"
"time"
)
type Book struct {
Id string
Title string
Author string
ISBN string
}
// item a helper for getting raw data on an item
func item(id string, library *url.URL) (map[string]interface{}, error) {
client := http.Client{
Timeout: time.Second * 30,
}
resp, err := client.Get(library.String() + "/Item/GetItem?id=" + id)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var body map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
return nil, err
} else if body["ErrorCode"] != nil {
return nil, errors.New(body["ErrorMessage"].(string))
}
return body, nil
}
func NewBook(id string, library *url.URL) (*Book, error) {
i, err := item(id, library)
if err != nil {
return nil, err
}
log.Println(i)
return &Book{
Id: id,
Title: i["Title"].(string),
Author: i["Authors"].(string),
ISBN: i["ISBN"].(string),
}, nil
}