Go library for accessing the Reddit API.
Find a file
Vartan Benohanian 011cd2a78b Add tests
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2020-08-18 23:12:35 -04:00
.github/workflows
examples
testdata Add tests 2020-08-18 23:12:35 -04:00
.gitignore
account.go Add store_visits method, about/edited in moderation service 2020-08-13 17:23:39 -04:00
account_test.go Use require instead of assert for tests 2020-08-11 16:21:07 -04:00
collection.go Finish some todos 2020-08-17 22:01:04 -04:00
collection_test.go Finish some todos 2020-08-17 22:01:04 -04:00
comment.go Add tests 2020-08-18 23:12:35 -04:00
comment_test.go Add tests 2020-08-18 23:12:35 -04:00
emoji.go Add tests 2020-08-11 19:10:23 -04:00
emoji_test.go Use require instead of assert for tests 2020-08-11 16:21:07 -04:00
errors.go Finish some todos 2020-08-17 22:01:04 -04:00
flair.go Tweak FlairService 2020-08-09 13:11:18 -04:00
flair_test.go Use require instead of assert for tests 2020-08-11 16:21:07 -04:00
go.mod
go.sum
LICENSE
listings.go
listings_test.go Finish some todos 2020-08-17 22:01:04 -04:00
Makefile
message.go Finish MessageService 2020-08-17 21:13:33 -04:00
message_test.go Finish MessageService 2020-08-17 21:13:33 -04:00
moderation.go Add Options struct for listing mod actions 2020-08-13 18:54:21 -04:00
moderation_test.go Add Options struct for listing mod actions 2020-08-13 18:54:21 -04:00
multi.go Rename PrivateMessageService to MessageService, add Delete method to it 2020-08-17 16:52:26 -04:00
multi_test.go Use require instead of assert for tests 2020-08-11 16:21:07 -04:00
post-and-comment.go
post.go Add tests 2020-08-18 23:12:35 -04:00
post_test.go Add tests 2020-08-18 23:12:35 -04:00
README.md Update README.md, remove one of the flair methods 2020-08-06 19:26:47 -04:00
reddit-oauth.go Rename files 2020-08-06 17:43:29 -04:00
reddit-options.go Rename files 2020-08-06 17:43:29 -04:00
reddit-options_test.go Use require instead of assert for tests 2020-08-11 16:21:07 -04:00
reddit.go Finish some todos 2020-08-17 22:01:04 -04:00
reddit_test.go Rename PrivateMessageService to MessageService, add Delete method to it 2020-08-17 16:52:26 -04:00
subreddit.go Get duplicates of a post 2020-08-14 11:29:13 -04:00
subreddit_test.go Finish some todos 2020-08-17 22:01:04 -04:00
things.go Add tests 2020-08-18 23:12:35 -04:00
timestamp.go
timestamp_test.go
user.go Get duplicates of a post 2020-08-14 11:29:13 -04:00
user_test.go Finish some todos 2020-08-17 22:01:04 -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 r/golang's top 5 posts of all time.
result, _, err := client.Subreddit.Top(context.Background(), "golang", &reddit.ListPostOptions{
    ListOptions: reddit.ListOptions{
        Limit: 5,
    },
    Time: "all",
})
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.