a14cb3a3c8
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com> |
||
---|---|---|
.github/workflows | ||
examples | ||
testdata | ||
.gitignore | ||
go.mod | ||
go.sum | ||
LICENSE | ||
Makefile | ||
README.md |
go-reddit
go-reddit is a Go client library for accessing the Reddit API.
You can view Reddit's official API documentation here.
Install
To get a specific version from the list of versions:
go get github.com/vartanbeno/go-reddit@vX.Y.Z
Or for the latest version:
go get github.com/vartanbeno/go-reddit
Usage
Make sure to have a Reddit app with a valid client id and secret. Here is a quick guide on how to create an app and get credentials.
package main
import "github.com/vartanbeno/go-reddit/reddit"
func main() {
credentials := &reddit.Credentials{
ID: "id",
Secret: "secret",
Username: "username",
Password: "password",
}
client, _ := reddit.NewClient(credentials)
}
You can pass in a number of options to NewClient
that further configure the client. For example, to use a custom HTTP client:
httpClient := &http.Client{Timeout: time.Second * 30}
client, _ := reddit.NewClient(credentials, reddit.WithHTTPClient(httpClient))
If this option is not used, it will be set to &http.Client{}
by default. More options are available in the reddit/reddit-options.go file.
Examples
Submit a comment.
comment, _, err := client.Comment.Submit(context.Background(), "t3_postid", "comment body")
if err != nil {
return err
}
fmt.Printf("Comment permalink: %s\n", comment.Permalink)
Upvote a post.
_, err := client.Post.Upvote(context.Background(), "t3_postid")
if err != nil {
return err
}
Get r/golang's top 5 posts of all time.
result, _, err := client.Subreddit.TopPosts(context.Background(), "golang", &reddit.ListPostOptions{
ListOptions: reddit.ListOptions{
Limit: 5,
},
Time: "all",
})
if err != nil {
return err
}
fmt.Printf("Received %d posts.\n", len(result.Posts))
More examples are available in the examples folder.
Design
The package design is heavily inspired from Google's GitHub API client and DigitalOcean's API client.
License
This project is licensed under the MIT License - see the LICENSE file for details.