forked from JaCoB1123/bookstack-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapters.go
46 lines (39 loc) · 1.08 KB
/
chapters.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
package main
import (
"fmt"
"strconv"
)
type chaptersResponse struct {
Data []chapter `json:"data"`
}
type chapter struct {
ID int `json:"id"`
BookID int `json:"book_id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
}
func (c chapter) String() string {
return strconv.Itoa(c.BookID) + ": " + c.Name
}
func (client bookStackClient) GetChapters() (*chaptersResponse, error) {
resp, err := client.R().
SetResult(chaptersResponse{}).
Get("/api/chapters")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("get of chapters: %s", resp)
}
return resp.Result().(*chaptersResponse), nil
}
func (client bookStackClient) CreateChapter(bookID int, name string) (*chapter, error) {
resp, err := client.R().
SetBody(chapter{
BookID: bookID,
Name: name}).
SetResult(chapter{}).
Post("/api/chapters")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("create chapter: %s", resp)
}
return resp.Result().(*chapter), nil
}