Go library for accessing the Reddit API.
Find a file
Vartan Benohanian aac3b48d6e Update readme
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2020-08-28 10:43:55 -04:00
.github/workflows Unexport post/comment service. Rename github workflow 2020-08-02 19:04:53 -04:00
examples Set HTTP client via option. Update readme, Makefile, go.sum 2020-08-26 23:13:34 -04:00
reddit Edit comments 2020-08-27 18:49:30 -04:00
testdata Add tests 2020-08-18 23:12:35 -04:00
.gitignore Initial commit 2020-04-23 20:10:02 -04:00
go.mod Rename package to go-reddit 2020-07-11 13:49:07 -04:00
go.sum Set HTTP client via option. Update readme, Makefile, go.sum 2020-08-26 23:13:34 -04:00
LICENSE Update README.md and license. Add examples 2020-08-02 15:59:25 -04:00
Makefile Set HTTP client via option. Update readme, Makefile, go.sum 2020-08-26 23:13:34 -04:00
README.md Update readme 2020-08-28 10:43:55 -04:00

go-reddit

Actions Status Go Report Card

Featured in issue 327 of Golang Weekly 🎉

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.