Go library for accessing the Reddit API.
Go to file
Vartan Benohanian a0ad61f28c Add pipeline badge to readme
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2020-08-02 17:24:28 -04:00
.github/workflows Use GitHub Actions 2020-08-02 17:10:39 -04:00
examples Update README.md and license. Add examples 2020-08-02 15:59:25 -04:00
testdata Delete search service, move its methods to other services 2020-08-02 01:06:25 -04:00
.gitignore Initial commit 2020-04-23 20:10:02 -04:00
account_test.go Change readFileContents method signature 2020-07-21 21:56:32 -04:00
account.go Use individual structs for services 2020-07-21 23:05:24 -04:00
comment_test.go Create comment and flair directories in testdata/ 2020-07-29 13:22:47 -04:00
comment.go WIP: load more comments for a post 2020-07-29 14:11:06 -04:00
errors.go Add method to create request with form data 2020-07-11 14:12:03 -04:00
flair_test.go Create comment and flair directories in testdata/ 2020-07-29 13:22:47 -04:00
flair.go Use individual structs for services 2020-07-21 23:05:24 -04:00
go.mod Rename package to go-reddit 2020-07-11 13:49:07 -04:00
go.sum Use assert package for tests, much cleaner 2020-05-29 19:50:52 -04:00
LICENSE Update README.md and license. Add examples 2020-08-02 15:59:25 -04:00
listings.go Use individual structs for services 2020-07-21 23:05:24 -04:00
Makefile Rename package to go-reddit 2020-07-11 13:49:07 -04:00
moderation_test.go Change readFileContents method signature 2020-07-21 21:56:32 -04:00
moderation.go Use individual structs for services 2020-07-21 23:05:24 -04:00
multi_test.go Change readFileContents method signature 2020-07-21 21:56:32 -04:00
multi.go Use anonymous structs in UnmarshalJSON implementations 2020-07-30 13:01:18 -04:00
post_test.go Use anonymous structs in UnmarshalJSON implementations 2020-07-30 13:01:18 -04:00
post-and-comment.go Use individual structs for services 2020-07-21 23:05:24 -04:00
post.go Fix tests 2020-07-30 12:22:39 -04:00
README.md Add pipeline badge to readme 2020-08-02 17:24:28 -04:00
reddit_oauth.go Use string instead of slice for subreddits, edit comments 2020-08-02 13:42:53 -04:00
reddit_options_test.go Rename files 2020-07-21 09:27:34 -04:00
reddit_options.go Use string instead of slice for subreddits, edit comments 2020-08-02 13:42:53 -04:00
reddit_test.go Use string instead of slice for subreddits, edit comments 2020-08-02 13:42:53 -04:00
reddit.go Delete search service, move its methods to other services 2020-08-02 01:06:25 -04:00
search.go Edit comments, change field type 2020-08-02 15:33:06 -04:00
subreddit_test.go Use string instead of slice for subreddits, edit comments 2020-08-02 13:42:53 -04:00
subreddit.go Edit comments, change field type 2020-08-02 15:33:06 -04:00
things.go Edit comments, change field type 2020-08-02 15:33:06 -04:00
timestamp_test.go Rename package to go-reddit 2020-07-11 13:49:07 -04:00
timestamp.go Rename package to go-reddit 2020-07-11 13:49:07 -04:00
user_test.go Edit comments, change field type 2020-08-02 15:33:06 -04:00
user.go Edit comments, change field type 2020-08-02 15:33:06 -04:00
util.go Rename package to go-reddit 2020-07-11 13:49:07 -04:00

go-reddit

Actions Status

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"

func main() {
    withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
    client, _ := reddit.NewClient(nil, withCredentials)
}

The first argument (the one set to nil) is of type *http.Client. It will be used to make the requests. If nil, it will be set to &http.Client{}.

The WithCredentials option sets the credentials used to make requests to the Reddit API.

Examples

Configure the client from environment variables.
client, _ := reddit.NewClient(nil, reddit.FromEnv)
Upvote a post.
_, err := client.Post.Upvote(context.Background(), "t3_postid")
if err != nil {
    fmt.Printf("Something bad happened: %v\n", err)
    return err
}
Get a subreddit's top 5 posts of all time.
result, _, err := client.Subreddit.Top(context.Background(), "golang", reddit.SetLimit(5), reddit.FromAllTime)
if err != nil {
    fmt.Printf("Something bad happened: %v\n", err)
    return err
}
fmt.Printf("Received %d posts.\n", len(result.Posts))

More examples are available in the examples folder.

Design

The package design and structure are 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.