Go library for accessing the Reddit API.
Go to file
Vartan Benohanian 1eb75d163c Add embedded urls attribute to live thread updates
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2020-09-18 09:56:06 -04:00
.github/workflows Unexport post/comment service. Rename github workflow 2020-08-02 19:04:53 -04:00
examples Add Listing/KarmaList to thing struct, tweak anonymous structs 2020-09-01 22:35:28 -04:00
images Add logo in readme 2020-09-01 12:23:00 -04:00
reddit Add embedded urls attribute to live thread updates 2020-09-18 09:56:06 -04:00
testdata Add embedded urls attribute to live thread updates 2020-09-18 09:56:06 -04:00
.gitignore Initial commit 2020-04-23 20:10:02 -04:00
go.mod Add methods to ModerationService, attribute to Post, use go v1.15 2020-09-07 21:24:14 -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-09-14 11:43:26 +01:00


go-reddit logo

Actions Status Go Report Card PkgGoDev

Overview

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() {
    withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
    client, _ := reddit.NewClient(withCredentials)
}

You can pass in a number of options to NewClient to further configure the client (see reddit/reddit-options.go). For example, to use a custom HTTP client:

httpClient := &http.Client{Timeout: time.Second * 30}
client, _ := reddit.NewClient(withCredentials, reddit.WithHTTPClient(httpClient))

Read-Only Mode

The global DefaultClient variable is a valid, read-only client with limited access to the Reddit API, much like a logged out user. You can initialize your own via NewReadonlyClient:

client, _ := reddit.NewReadonlyClient()

Examples

Configure the client from environment variables.
client, _ := reddit.NewClient(reddit.FromEnv)
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.
posts, _, 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(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.