forked from JaCoB1123/bookstack-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
books.go
40 lines (33 loc) · 928 Bytes
/
books.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
package main
import "fmt"
type booksResponse struct {
Data []book `json:"data"`
}
type book struct {
ID int `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
}
func (b book) String() string {
return b.Name
}
func (client bookStackClient) GetBooks() (*booksResponse, error) {
resp, err := client.R().
SetResult(booksResponse{}).
Get("/api/books")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("get of books: %w", err)
}
return resp.Result().(*booksResponse), nil
}
func (client bookStackClient) CreateBook(name string) (*book, error) {
resp, err := client.R().
SetBody(book{Name: name}).
SetResult(book{}).
Post("/api/books")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("create book: %w", err)
}
return resp.Result().(*book), nil
}