fixed Get on post service
This commit is contained in:
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -25,11 +26,20 @@ func run() (err error) {
|
||||
|
||||
client.OnRequestCompleted(logResponse)
|
||||
|
||||
client.Subreddit.Search(ctx, "programming", nil)
|
||||
client.Subreddit.SearchNames(ctx, "monitor")
|
||||
client.Subreddit.SearchPosts(ctx, "react", "webdev", nil)
|
||||
client.Subreddit.HotPosts(ctx, "golang", &reddit.ListOptions{Limit: 5})
|
||||
|
||||
posts, _, err := client.Subreddit.HotPosts(ctx, "golang", &reddit.ListOptions{Limit: 5})
|
||||
if err != nil {
|
||||
log.Fatal("Could not get hot posts:", err)
|
||||
}
|
||||
if len(posts) == 0 {
|
||||
log.Fatal("No posts")
|
||||
}
|
||||
details, resp, err := client.Post.Get(ctx, posts[0].ID, posts[0].SubredditName)
|
||||
if err != nil {
|
||||
log.Fatal("Could not get post details: Status code ", resp.Status)
|
||||
}
|
||||
b, _ := json.MarshalIndent(details, "", " ")
|
||||
log.Println("Got post details")
|
||||
log.Println(string(b))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -65,8 +65,8 @@ type SubmitLinkRequest struct {
|
||||
// Get a post with its comments.
|
||||
// id is the ID36 of the post, not its full id.
|
||||
// Example: instead of t3_abc123, use abc123.
|
||||
func (s *PostService) Get(ctx context.Context, id string) (*PostAndComments, *Response, error) {
|
||||
path := fmt.Sprintf("comments/%s", id)
|
||||
func (s *PostService) Get(ctx context.Context, id, subreddit string) (*PostAndComments, *Response, error) {
|
||||
path := fmt.Sprintf("r/%s/comments/%s/", subreddit, id)
|
||||
req, err := s.client.NewRequest(http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
||||
Reference in New Issue
Block a user