2020-04-29 15:59:18 -04:00
|
|
|
package geddit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListingsService handles communication with the link (post)
|
|
|
|
// related methods of the Reddit API
|
|
|
|
type ListingsService interface {
|
|
|
|
Get(ctx context.Context, ids ...string) (*Listing, *Response, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListingsServiceOp implements the Vote interface
|
|
|
|
type ListingsServiceOp struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ListingsService = &ListingsServiceOp{}
|
|
|
|
|
|
|
|
type listingRoot struct {
|
|
|
|
Kind string `json:"kind,omitempty"`
|
|
|
|
Data *struct {
|
|
|
|
Dist int `json:"dist"`
|
|
|
|
Children []map[string]interface{} `json:"children,omitempty"`
|
|
|
|
After string `json:"after,omitempty"`
|
|
|
|
Before string `json:"before,omitempty"`
|
|
|
|
} `json:"data,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listing holds various types of things that all come from the Reddit API
|
2020-05-03 17:31:35 -04:00
|
|
|
// type Listing struct {
|
|
|
|
// Links []*Submission `json:"links,omitempty"`
|
|
|
|
// Comments []*Comment `json:"comments,omitempty"`
|
|
|
|
// Subreddits []*Subreddit `json:"subreddits,omitempty"`
|
|
|
|
// }
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
// Get gets a list of things based on their IDs
|
|
|
|
// Only links, comments, and subreddits are allowed
|
2020-05-03 17:31:35 -04:00
|
|
|
// todo: only links, comments, subreddits
|
2020-04-29 15:59:18 -04:00
|
|
|
func (s *ListingsServiceOp) Get(ctx context.Context, ids ...string) (*Listing, *Response, error) {
|
|
|
|
type query struct {
|
|
|
|
IDs []string `url:"id,comma"`
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "api/info"
|
|
|
|
path, err := addOptions(path, query{ids})
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
root := new(rootListing)
|
2020-04-29 15:59:18 -04:00
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
2020-05-03 17:31:35 -04:00
|
|
|
return root.Data, resp, nil
|
2020-04-29 15:59:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// todo: do by_id next
|