2020-09-01 12:21:28 -04:00
< div align = 'center' >
< br / >
< img src = './images/logo.png' alt = 'go-reddit logo' height = '150' >
---
< div id = 'badges' align = 'center' >
2020-04-23 20:10:02 -04:00
2020-09-01 00:02:12 -04:00
[![Actions Status ](https://github.com/vartanbeno/go-reddit/workflows/tests/badge.svg )](https://github.com/vartanbeno/go-reddit/actions)
[![Go Report Card ](https://goreportcard.com/badge/github.com/vartanbeno/go-reddit )](https://goreportcard.com/report/github.com/vartanbeno/go-reddit)
2020-08-02 17:24:28 -04:00
2020-09-01 12:21:28 -04:00
< / div >
< / div >
## Overview
2020-08-29 02:48:22 -04:00
**Featured in [issue 327 of Golang Weekly ](https://golangweekly.com/issues/327 ) 🎉**
2020-08-28 10:43:55 -04:00
2020-07-11 13:49:07 -04:00
go-reddit is a Go client library for accessing the Reddit API.
2020-04-23 22:57:47 -04:00
2020-08-02 15:59:25 -04:00
You can view Reddit's official API documentation [here ](https://www.reddit.com/dev/api/ ).
2020-04-23 22:57:47 -04:00
## Install
2020-07-11 13:49:07 -04:00
To get a specific version from the list of [versions ](https://github.com/vartanbeno/go-reddit/releases ):
2020-04-23 22:57:47 -04:00
```sh
2020-07-11 13:49:07 -04:00
go get github.com/vartanbeno/go-reddit@vX.Y.Z
2020-04-23 22:57:47 -04:00
```
Or for the latest version:
```sh
2020-07-11 13:49:07 -04:00
go get github.com/vartanbeno/go-reddit
2020-04-23 22:57:47 -04:00
```
2020-08-02 15:59:25 -04:00
## Usage
Make sure to have a Reddit app with a valid client id and secret. [Here ](https://github.com/reddit-archive/reddit/wiki/OAuth2-Quick-Start-Example#first-steps ) is a quick guide on how to create an app and get credentials.
```go
package main
2020-08-20 14:37:59 -04:00
import "github.com/vartanbeno/go-reddit/reddit"
2020-08-02 15:59:25 -04:00
func main() {
2020-08-29 02:48:22 -04:00
withCredentials := reddit.WithCredentials("id", "secret", "username", "password")
client, _ := reddit.NewClient(withCredentials)
2020-08-02 15:59:25 -04:00
}
```
2020-08-29 02:48:22 -04:00
You can pass in a number of options to `NewClient` to further configure the client (see [reddit/reddit-options.go ](reddit/reddit-options.go )). For example, to use a custom HTTP client:
2020-08-26 23:13:34 -04:00
```go
httpClient := & http.Client{Timeout: time.Second * 30}
2020-08-29 02:48:22 -04:00
client, _ := reddit.NewClient(withCredentials, reddit.WithHTTPClient(httpClient))
2020-08-26 23:13:34 -04:00
```
2020-08-29 02:48:22 -04:00
### 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` :
```go
client, _ := reddit.NewReadonlyClient()
```
2020-08-02 15:59:25 -04:00
## Examples
2020-08-29 02:48:22 -04:00
< details >
< summary > Configure the client from environment variables.< / summary >
```go
client, _ := reddit.NewClient(reddit.FromEnv)
```
< / details >
2020-08-02 15:59:25 -04:00
< details >
2020-08-26 23:13:34 -04:00
< summary > Submit a comment.< / summary >
2020-08-02 15:59:25 -04:00
```go
2020-08-26 23:13:34 -04:00
comment, _, err := client.Comment.Submit(context.Background(), "t3_postid", "comment body")
if err != nil {
return err
}
fmt.Printf("Comment permalink: %s\n", comment.Permalink)
2020-08-02 15:59:25 -04:00
```
< / details >
< details >
< summary > Upvote a post.< / summary >
```go
_, err := client.Post.Upvote(context.Background(), "t3_postid")
if err != nil {
return err
}
```
< / details >
< details >
2020-08-06 19:26:47 -04:00
< summary > Get r/golang's top 5 posts of all time.< / summary >
2020-08-02 15:59:25 -04:00
```go
2020-08-29 14:20:30 -04:00
posts, _, err := client.Subreddit.TopPosts(context.Background(), "golang", & reddit.ListPostOptions{
2020-08-06 19:26:47 -04:00
ListOptions: reddit.ListOptions{
Limit: 5,
},
Time: "all",
})
2020-08-02 15:59:25 -04:00
if err != nil {
return err
}
2020-08-29 14:20:30 -04:00
fmt.Printf("Received %d posts.\n", len(posts))
2020-08-02 15:59:25 -04:00
```
< / details >
More examples are available in the [examples ](examples ) folder.
## Design
2020-08-26 23:13:34 -04:00
The package design is heavily inspired from [Google's GitHub API client ](https://github.com/google/go-github ) and [DigitalOcean's API client ](https://github.com/digitalocean/godo ).
2020-08-02 15:59:25 -04:00
## License
This project is licensed under the MIT License - see the [LICENSE ](LICENSE ) file for details.