feat(reddit): Fetcher interface and go-reddit wrapper
Adds internal/reddit package with a Fetcher interface and Client implementation backed by go-reddit/v2. Handles hot/top/rising/new sort variants with correct option types per the library API.
This commit is contained in:
6
go.mod
6
go.mod
@@ -4,6 +4,8 @@ go 1.26
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/golang/protobuf v1.2.0 // indirect
|
||||
github.com/google/go-querystring v1.0.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
@@ -12,7 +14,11 @@ require (
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/spf13/cobra v1.10.2 // indirect
|
||||
github.com/spf13/pflag v1.0.9 // indirect
|
||||
github.com/vartanbeno/go-reddit/v2 v2.0.1 // indirect
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e // indirect
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
|
||||
golang.org/x/sys v0.42.0 // indirect
|
||||
google.golang.org/appengine v1.4.0 // indirect
|
||||
modernc.org/libc v1.70.0 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
|
||||
21
go.sum
21
go.sum
@@ -1,6 +1,12 @@
|
||||
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
|
||||
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||
@@ -11,6 +17,7 @@ github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOF
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/pelletier/go-toml/v2 v2.3.0 h1:k59bC/lIZREW0/iVaQR8nDHxVq8OVlIzYCOJf421CaM=
|
||||
github.com/pelletier/go-toml/v2 v2.3.0/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
@@ -18,11 +25,25 @@ github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
||||
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
|
||||
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/vartanbeno/go-reddit/v2 v2.0.1 h1:P6ITpf5YHjdy7DHZIbUIDn/iNAoGcEoDQnMa+L4vutw=
|
||||
github.com/vartanbeno/go-reddit/v2 v2.0.1/go.mod h1:758/S10hwZSLm43NPtwoNQdZFSg3sjB5745Mwjb0ANI=
|
||||
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e h1:bRhVy7zSSasaqNksaRZiA5EEI+Ei4I1nO5Jh72wfHlg=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
|
||||
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
|
||||
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
modernc.org/libc v1.70.0 h1:U58NawXqXbgpZ/dcdS9kMshu08aiA6b7gusEusqzNkw=
|
||||
modernc.org/libc v1.70.0/go.mod h1:OVmxFGP1CI/Z4L3E0Q3Mf1PDE0BucwMkcXjjLntvHJo=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
|
||||
78
internal/reddit/reddit.go
Normal file
78
internal/reddit/reddit.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package reddit
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/vartanbeno/go-reddit/v2/reddit"
|
||||
|
||||
"somegit.dev/vikingowl/reddit-reader/internal/domain"
|
||||
)
|
||||
|
||||
// Fetcher is the interface for fetching posts from a subreddit.
|
||||
type Fetcher interface {
|
||||
FetchPosts(ctx context.Context, subreddit, sort string, limit int) ([]domain.Post, error)
|
||||
}
|
||||
|
||||
// Client wraps the go-reddit client behind the Fetcher interface.
|
||||
type Client struct {
|
||||
rc *reddit.Client
|
||||
}
|
||||
|
||||
// NewClient creates an authenticated Reddit API client.
|
||||
func NewClient(clientID, clientSecret, username, password string) (*Client, error) {
|
||||
rc, err := reddit.NewClient(reddit.Credentials{
|
||||
ID: clientID,
|
||||
Secret: clientSecret,
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create reddit client: %w", err)
|
||||
}
|
||||
return &Client{rc: rc}, nil
|
||||
}
|
||||
|
||||
// FetchPosts retrieves posts from the given subreddit using the specified sort order.
|
||||
// Supported sort values: "hot", "top", "rising", and anything else defaults to "new".
|
||||
func (c *Client) FetchPosts(ctx context.Context, subreddit, sort string, limit int) ([]domain.Post, error) {
|
||||
listOpts := &reddit.ListOptions{Limit: limit}
|
||||
listPostOpts := &reddit.ListPostOptions{ListOptions: *listOpts}
|
||||
|
||||
var posts []*reddit.Post
|
||||
var err error
|
||||
|
||||
switch sort {
|
||||
case "hot":
|
||||
posts, _, err = c.rc.Subreddit.HotPosts(ctx, subreddit, listOpts)
|
||||
case "top":
|
||||
posts, _, err = c.rc.Subreddit.TopPosts(ctx, subreddit, listPostOpts)
|
||||
case "rising":
|
||||
posts, _, err = c.rc.Subreddit.RisingPosts(ctx, subreddit, listOpts)
|
||||
default:
|
||||
posts, _, err = c.rc.Subreddit.NewPosts(ctx, subreddit, listOpts)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("fetch %s/%s: %w", subreddit, sort, err)
|
||||
}
|
||||
|
||||
result := make([]domain.Post, len(posts))
|
||||
for i, p := range posts {
|
||||
var created time.Time
|
||||
if p.Created != nil {
|
||||
created = p.Created.Time
|
||||
}
|
||||
result[i] = domain.Post{
|
||||
ID: p.FullID,
|
||||
Subreddit: p.SubredditName,
|
||||
Title: p.Title,
|
||||
Author: p.Author,
|
||||
URL: p.URL,
|
||||
SelfText: p.Body,
|
||||
Score: p.Score,
|
||||
CreatedUTC: created,
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
38
internal/reddit/reddit_test.go
Normal file
38
internal/reddit/reddit_test.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package reddit_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"somegit.dev/vikingowl/reddit-reader/internal/domain"
|
||||
redditpkg "somegit.dev/vikingowl/reddit-reader/internal/reddit"
|
||||
)
|
||||
|
||||
type mockFetcher struct {
|
||||
posts []domain.Post
|
||||
err error
|
||||
}
|
||||
|
||||
func (m *mockFetcher) FetchPosts(_ context.Context, _, _ string, _ int) ([]domain.Post, error) {
|
||||
return m.posts, m.err
|
||||
}
|
||||
|
||||
func TestFetcherInterface(t *testing.T) {
|
||||
mock := &mockFetcher{
|
||||
posts: []domain.Post{
|
||||
{ID: "t3_a", Title: "Test", Subreddit: "golang", CreatedUTC: time.Now()},
|
||||
},
|
||||
}
|
||||
var fetcher redditpkg.Fetcher = mock
|
||||
posts, err := fetcher.FetchPosts(context.Background(), "golang", "new", 25)
|
||||
if err != nil {
|
||||
t.Fatalf("FetchPosts: %v", err)
|
||||
}
|
||||
if len(posts) != 1 {
|
||||
t.Fatalf("len = %d, want 1", len(posts))
|
||||
}
|
||||
if posts[0].ID != "t3_a" {
|
||||
t.Errorf("ID = %q, want t3_a", posts[0].ID)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user