Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthorStatistic ¶
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 (*Client) ExchangeAuthCode ¶
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")