-
Notifications
You must be signed in to change notification settings - Fork 0
/
boardlist.go
96 lines (75 loc) · 1.84 KB
/
boardlist.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
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
package jschan
import (
"context"
"fmt"
"net/http"
"net/url"
"github.com/jackbekket/jschan_go_api/app/models"
)
type GetWebringResponse struct {
*models.Webring
}
func (c *Client) GetWebring(ctx context.Context) (*GetWebringResponse, error) {
url := fmt.Sprintf("%s/webring.json", c.BaseURL)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetWebringResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return &res, nil
}
type GetBoardsResponse struct {
Boards []models.Board `json:"boards"`
Page int `json:"page"`
MaxPage int `json:"maxPage"`
}
type GetBoardsPublicOptions struct {
Search string
Sort string
SortDirection string
Page int
LocalFirst bool
Sites []string
}
func (c *Client) GetBoardsPublic(ctx context.Context, options *GetBoardsPublicOptions) (*GetBoardsResponse, error) {
page := 1
search := ""
sort := "popularity"
direction := "desc"
local_first := false
sites := []string{}
if options != nil {
search = options.Search
sort = options.Sort
direction = options.SortDirection
local_first = options.LocalFirst
sites = options.Sites
page = options.Page
}
query := url.Values{}
query.Set("search", search)
query.Set("page", fmt.Sprintf("%d", page))
query.Set("sort", sort)
query.Set("direction", direction)
if local_first {
query.Set("local_first", "true")
}
for _, site := range sites {
query.Add("sites", site)
}
url := fmt.Sprintf("%s/boards.json?%s", c.BaseURL, query.Encode())
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
res := GetBoardsResponse{}
if err := c.sendRequest(req, &res, nil); err != nil {
return nil, err
}
return &res, nil
}