-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_space.go
More file actions
97 lines (78 loc) · 2.13 KB
/
api_space.go
File metadata and controls
97 lines (78 loc) · 2.13 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
package confluence
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
//根据SpaceKey获取空间的信息
func (cli *Client) SpaceByKey(key string) (Space, error) {
resp, err := cli.ApiGET("/space/"+key, nil)
if err != nil {
return Space{}, fmt.Errorf("执行请求失败: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return Space{}, fmt.Errorf("[%d]%s", resp.StatusCode, resp.Status)
}
var info Space
err = json.NewDecoder(resp.Body).Decode(&info)
if err != nil {
return Space{}, fmt.Errorf("解析响应失败: %s", err)
}
return info, nil
}
//获取空间特定类型的内容
func (cli *Client) SpaceContentByType(key, contentType string, start int) ([]Content, int, error) {
query := url.Values{
"start": {fmt.Sprintf("%d", start)},
"expand": {"body.storage,ancestors"},
}
resp, err := cli.ApiGET("/space/"+key+"/content/"+contentType, query)
if err != nil {
return nil, 0, fmt.Errorf("执行请求失败: %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, 0, fmt.Errorf("[%d]%s", resp.StatusCode, resp.Status)
}
var info struct {
PageResp
Results []Content
}
err = json.NewDecoder(resp.Body).Decode(&info)
if err != nil {
return nil, 0, fmt.Errorf("解析响应失败: %s", err)
}
//是否存在Next链接表示是否包含下一页
nextStart := 0
if info.Links.Next != "" {
nextStart = info.Start + info.Size
}
return info.Results, nextStart, nil
}
//获取空间所有的页面
func (cli *Client) AllSpacePages(key string) ([]Content, error) {
return cli.AllSpaceContents(key, ContentTypePage)
}
//获取空间所有的博客
func (cli *Client) AllSpaceBlogs(key string) ([]Content, error) {
return cli.AllSpaceContents(key, ContentTypeBlog)
}
//获取空间所有的内容
func (cli *Client) AllSpaceContents(key, contentType string) ([]Content, error) {
var pages []Content
start := 0
for {
contents, nextStart, err := cli.SpaceContentByType(key, contentType, start)
if err != nil {
return nil, err
}
pages = append(pages, contents...)
if nextStart <= 0 {
break
}
start = nextStart
}
return pages, nil
}