-
Notifications
You must be signed in to change notification settings - Fork 2
/
pages.go
65 lines (56 loc) · 1.63 KB
/
pages.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
package main
import (
"fmt"
"strconv"
)
type pagesResponse struct {
Data []page `json:"data"`
}
type page struct {
ID int `json:"id"`
BookID int `json:"book_id"`
ChapterID int `json:"chapter_id"`
Name string `json:"name"`
Slug string `json:"slug"`
Description string `json:"description"`
Markdown string `json:"markdown"`
}
func (p page) String() string {
return strconv.Itoa(p.ChapterID) + ": " + p.Name
}
func (client bookStackClient) GetPages() (*pagesResponse, error) {
resp, err := client.R().
SetResult(pagesResponse{}).
Get("/api/pages")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("get of pagers: %s", resp)
}
return resp.Result().(*pagesResponse), nil
}
func (client bookStackClient) CreatePage(chapterID int, name string, content []byte) (*page, error) {
resp, err := client.R().
SetBody(page{
ChapterID: chapterID,
Name: name,
Markdown: string(content)}).
SetResult(page{}).
Post("/api/pages")
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("create page: %s", resp)
}
return resp.Result().(*page), nil
}
type pageContentRequest struct {
Markdown string `json:"markdown"`
}
func (client bookStackClient) UpdatePageContent(pageID int, content []byte) (*page, error) {
resp, err := client.R().
SetBody(pageContentRequest{
Markdown: string(content)}).
SetResult(page{}).
Put("/api/pages/" + strconv.Itoa(pageID))
if err != nil || resp.StatusCode() > 399 {
return nil, fmt.Errorf("update page: %s", resp)
}
return resp.Result().(*page), nil
}