1 package jschan
2
3 import (
4 "context"
5 "fmt"
6 "jschan/app/models"
7 "net/http"
8 "strconv"
9 )
10
11 type GetCatalogOptions struct {
12 Board string
13 }
14
15 type CatalogResponse []models.Post
16
17 func (c *Client) GetCatalog(ctx context.Context, options *GetCatalogOptions) (CatalogResponse, error) {
18
19 url := fmt.Sprintf("%s/%s/catalog.json", c.BaseURL, options.Board)
20
21 req, err := http.NewRequest(http.MethodGet, url, nil)
22 if err != nil {
23 return nil, err
24 }
25
26 req = req.WithContext(ctx)
27
28 res := CatalogResponse{}
29 if err := c.sendRequest(req, &res, nil); err != nil {
30 return nil, err
31 }
32
33 return res, nil
34
35 }
36
37 type GetIndexOptions struct {
38 Board string
39 Page int
40 }
41
42 type GetIndexResponse struct {
43 *models.Post
44 }
45
46 func (c *Client) GetIndex(ctx context.Context, options *GetIndexOptions) (*GetIndexResponse, error) {
47
48 pageString := strconv.Itoa(options.Page)
49 if pageString == "1" {
50 pageString = "index"
51 }
52
53 url := fmt.Sprintf("%s/%s/%s.json", c.BaseURL, options.Board, pageString)
54
55 req, err := http.NewRequest(http.MethodGet, url, nil)
56 if err != nil {
57 return nil, err
58 }
59
60 req = req.WithContext(ctx)
61
62 res := GetIndexResponse{}
63 if err := c.sendRequest(req, &res, nil); err != nil {
64 return nil, err
65 }
66
67 return &res, nil
68
69 }
70
71 type GetLogsListOptions struct {
72 Board string
73 }
74
75 type GetLogsListResponse []models.LogList
76
77 func (c *Client) GetLogsList(ctx context.Context, options *GetLogsListOptions) (GetLogsListResponse, error) {
78
79 url := fmt.Sprintf("%s/%s/logs.json", c.BaseURL, options.Board)
80
81 req, err := http.NewRequest(http.MethodGet, url, nil)
82 if err != nil {
83 return nil, err
84 }
85
86 req = req.WithContext(ctx)
87
88 res := GetLogsListResponse{}
89 if err := c.sendRequest(req, &res, nil); err != nil {
90 return nil, err
91 }
92
93 return res, nil
94
95 }
96
97 type GetLogsOptions struct {
98 Board string
99 Date models.LogDate
100 }
101
102 type GetLogsResponse []models.Log
103
104 func (c *Client) GetLogs(ctx context.Context, options *GetLogsOptions) (GetLogsResponse, error) {
105
106 url := fmt.Sprintf("%s/%s/logs/%s.json", c.BaseURL, options.Board, options.Date.String())
107
108 req, err := http.NewRequest(http.MethodGet, url, nil)
109 if err != nil {
110 return nil, err
111 }
112
113 req = req.WithContext(ctx)
114
115 res := GetLogsResponse{}
116 if err := c.sendRequest(req, &res, nil); err != nil {
117 return nil, err
118 }
119
120 return res, nil
121
122 }
123
124 type GetCustomPageOptions struct {
125 Board string
126 CustomPage string
127 }
128
129 type GetCustomPageResponse struct {
130 *models.CustomPage
131 }
132
133 func (c *Client) GetCustomPage(ctx context.Context, options *GetCustomPageOptions) (*GetCustomPageResponse, error) {
134
135 url := fmt.Sprintf("%s/%s/custompage/%s.json", c.BaseURL, options.Board, options.CustomPage)
136
137 req, err := http.NewRequest(http.MethodGet, url, nil)
138 if err != nil {
139 return nil, err
140 }
141
142 req = req.WithContext(ctx)
143
144 res := GetCustomPageResponse{}
145 if err := c.sendRequest(req, &res, nil); err != nil {
146 return nil, err
147 }
148
149 return &res, nil
150
151 }
152
153 type GetBannersOptions struct {
154 Board string
155 }
156
157 type GetBannersResponse []string
158
159 func (c *Client) GetBanners(ctx context.Context, options *GetBannersOptions) (GetBannersResponse, error) {
160
161 url := fmt.Sprintf("%s/%s/banners.json", c.BaseURL, options.Board)
162
163 req, err := http.NewRequest(http.MethodGet, url, nil)
164 if err != nil {
165 return nil, err
166 }
167
168 req = req.WithContext(ctx)
169
170 res := GetBannersResponse{}
171 if err := c.sendRequest(req, &res, nil); err != nil {
172 return nil, err
173 }
174
175 return res, nil
176
177 }
178
179 type GetSettingsOptions struct {
180 Board string
181 }
182
183 type GetSettingsResponse struct {
184 *models.BoardSettings
185 }
186
187 func (c *Client) GetSettings(ctx context.Context, options *GetSettingsOptions) (*GetSettingsResponse, error) {
188
189 url := fmt.Sprintf("%s/%s/settings.json", c.BaseURL, options.Board)
190
191 req, err := http.NewRequest(http.MethodGet, url, nil)
192 if err != nil {
193 return nil, err
194 }
195
196 req = req.WithContext(ctx)
197
198 res := GetSettingsResponse{}
199 if err := c.sendRequest(req, &res, nil); err != nil {
200 return nil, err
201 }
202
203 return &res, nil
204
205 }
206
View as plain text