Create WikiService

Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
Vartan Benohanian
2020-09-03 23:25:16 -04:00
parent ffcc906c07
commit 9d5132f15c
7 changed files with 523 additions and 34 deletions
+32 -26
View File
@@ -6,19 +6,21 @@ import (
)
const (
kindComment = "t1"
kindUser = "t2"
kindPost = "t3"
kindMessage = "t4"
kindSubreddit = "t5"
kindTrophy = "t6"
kindListing = "Listing"
kindKarmaList = "KarmaList"
kindTrophyList = "TrophyList"
kindUserList = "UserList"
kindMore = "more"
kindModAction = "modaction"
kindMulti = "LabeledMulti"
kindComment = "t1"
kindUser = "t2"
kindPost = "t3"
kindMessage = "t4"
kindSubreddit = "t5"
kindTrophy = "t6"
kindListing = "Listing"
kindKarmaList = "KarmaList"
kindTrophyList = "TrophyList"
kindUserList = "UserList"
kindMore = "more"
kindModAction = "modaction"
kindMulti = "LabeledMulti"
kindWikiPageListing = "wikipagelisting"
kindWikiPageSettings = "wikipagesettings"
)
type anchor interface {
@@ -94,6 +96,10 @@ func (t *thing) UnmarshalJSON(b []byte) error {
v = new(trophyList)
case kindKarmaList:
v = new([]*SubredditKarma)
case kindWikiPageListing:
v = new([]string)
case kindWikiPageSettings:
v = new(WikiPageSettings)
default:
return fmt.Errorf("unrecognized kind: %q", t.Kind)
}
@@ -168,6 +174,19 @@ func (t *thing) Karma() ([]*SubredditKarma, bool) {
return *v, ok
}
func (t *thing) WikiPages() ([]string, bool) {
v, ok := t.Data.(*[]string)
if !ok {
return nil, ok
}
return *v, ok
}
func (t *thing) WikiPageSettings() (v *WikiPageSettings, ok bool) {
v, ok = t.Data.(*WikiPageSettings)
return
}
// listing is a list of things coming from the Reddit API.
// It also contains the after/before anchors useful for subsequent requests.
type listing struct {
@@ -263,21 +282,8 @@ type things struct {
Multis []*Multi
}
// init initializes or clears the listing.
func (t *things) init() {
t.Comments = make([]*Comment, 0)
t.Mores = make([]*More, 0)
t.Users = make([]*User, 0)
t.Posts = make([]*Post, 0)
t.Subreddits = make([]*Subreddit, 0)
t.ModActions = make([]*ModAction, 0)
t.Multis = make([]*Multi, 0)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *things) UnmarshalJSON(b []byte) error {
t.init()
var things []thing
if err := json.Unmarshal(b, &things); err != nil {
return err