-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathblogentry.go
83 lines (77 loc) · 2.3 KB
/
blogentry.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
package goforces
import (
"context"
"fmt"
"net/url"
"strconv"
)
//BlogEntry represents a Codeforces BlogEntry
type BlogEntry struct {
OriginalLocale string `json:"originalLocale"`
AllowViewHistory bool `json:"allowViewHistory"`
CreationTimeSeconds int `json:"creationTimeSeconds"`
Rating int `json:"rating"`
AuthorHandle string `json:"authorHandle"`
ModificationTimeSeconds int `json:"modificationTimeSeconds"`
ID int `json:"id"`
Title string `json:"title"`
Locale string `json:"locale"`
Content string `json:"content"`
Tags []string `json:"tags"`
}
//GetBlogEntryComments implements /blogEntry.comments
func (c *Client) GetBlogEntryComments(ctx context.Context, blogEntryID int) ([]Comment, error) {
c.Logger.Println("GetBlogEntryComments : ", blogEntryID)
v := url.Values{}
v.Add("blogEntryId", strconv.Itoa(blogEntryID))
spath := "/blogEntry.comments" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []Comment `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
return resp.Result, nil
}
//GetBlogEntryView implements /blogEntry.view
func (c *Client) GetBlogEntryView(ctx context.Context, blogEntryID int) (*BlogEntry, error) {
c.Logger.Println("GetBlogEntryView : ", blogEntryID)
v := url.Values{}
v.Add("blogEntryId", strconv.Itoa(blogEntryID))
spath := "/blogEntry.view" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type EntryViewResponse struct {
Status string `json:"status"`
Result BlogEntry `json:"result"`
}
var resp EntryViewResponse
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
return &resp.Result, nil
}