socialmedia

package
v0.0.0-...-2781dfa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 15, 2024 License: MPL-2.0 Imports: 12 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthorStatistic

type AuthorStatistic struct {
	Author        string
	TotalPosts    int
	TotalUpvotes  int
	TotalComments int
}

type Client

type Client struct {
	OAuthConfig      *oauth2.Config
	AuthorizationURL string
	AuthCode         string
	RateLimiter      *rate.Limiter
	ServerErr        error
	Token            *oauth2.Token
	Port             int
	Throttle         <-chan time.Time
	HttpClient       *http.Client
	Context          context.Context
	Debug            bool
	ProgramStartTime time.Time
}

func NewClient

func NewClient(debugFlag bool) *Client

func NewClientWithToken

func NewClientWithToken(token *oauth2.Token, debugFlag bool) *Client

func (*Client) ExchangeAuthCode

func (c *Client) ExchangeAuthCode(ctx context.Context) (*oauth2.Token, error)

ExchangeAuthCode exchanges the authorization code for an OAuth2 token. It waits for the Throttle channel to receive a signal before proceeding, ensuring that the rate limit is respected. It uses the OAuthConfig to make the token exchange request. If successful, it stores the token in the Client's Token field and returns it. If there is an error, it returns nil for the token and the error.

Example usage:

c := &Client{}
c.StartServer(context.Background())
// User completes OAuth authorization flow and receives an authorization code
// The authorization code is automatically stored in c.AuthCode by the callbackHandler method
token, err := c.ExchangeAuthCode(context.Background())

func (*Client) FetchPosts

func (c *Client) FetchPosts(ctx context.Context, subreddit string, opts ...PaginationOptions) (RedditResponse, error)

FetchPosts retrieves the latest posts from a subreddit using the Reddit API. It makes a GET request to the subreddit's "new" endpoint and returns a RedditResponse object containing the posts. The method utilizes the client's Throttle channel to throttle requests. The method requires the subreddit name as the first argument and supports optional PaginationOptions. If provided, PaginationOptions determine the "before" and "after" query parameters in the request URL. The method sets the "Accept" and "Authorization" headers in the request and handles any errors that occur during the HTTP request. It also logs information about the rate limit headers received in the HTTP response. The method returns the RedditResponse and an error if one occurs. Example usage:

client := &Client{}
resp, err := client.FetchPosts(context.Background(), "golang")

func (*Client) StartServer

func (c *Client) StartServer(ctx context.Context) error

type PaginationOptions

type PaginationOptions struct {
	Before string
	After  string
}

type Post

type Post struct {
	PostID      string
	Title       string
	Body        string
	Author      string
	NumComments int
	UpVotes     int
	Created     time.Time
	SubReddit   string
}

type RedditResponse

type RedditResponse struct {
	Before    string
	After     string
	Subreddit string
	Posts     []Post
}

type SocialMedia

type SocialMedia interface {
	StartServer(ctx context.Context) error
	ExchangeAuthCode(ctx context.Context) (*oauth2.Token, error)
	FetchPosts(ctx context.Context, subreddit string) ([]Post, error)
}

type Transport

type Transport struct {
	UserAgent string
}

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL