ReddiGo is a Go-based SDK for interacting with the Reddit API. It allows developers to easily integrate Reddit's functionalities into their Go applications, providing a simple interface for accessing various endpoints.
First, ensure you have Go installed and set up correctly on your machine. You can install the SDK using:
go get github.com/stationFortyTwo/ReddiGoImport the SDK into your Go project:
import "github.com/stationFortyTwo/ReddiGo"To initialize the SDK, you need to create a new instance of ReddiGoSDK by providing your Reddit API credentials.
package main
import (
"fmt"
"log"
"github.com/stationFortyTwo/ReddiGo"
)
func main() {
sdk := ReddiGo.NewReddiGoSDK("your-client-id", "your-client-secret", "your-redirect-uri", "your-refresh-token")
// Use the SDK to make API calls
user, err := sdk.GetMe()
if err != nil {
log.Fatalf("Error retrieving user info: %v", err)
}
fmt.Printf("User info: %+v\n", user)
}ReddiGo is generated by reddigo-generator based on the reddit dev page. To generate the latest version of ReddiGo, use the generator here and follow the instructions.
The SDK creates a default directory structure for organizing the generated files. Ensure the path you provide using the -o flag exists or has the necessary permissions.
If you wish to contribute to ReddiGo, fork the repository, make your changes, and submit a pull request.
For a full list of available functions see documentation.md
Description:
Fetches the authenticated user's information.
Parameters:
None.
Returns:
(GetMeResponse, error) - The response contains user details or an error if the request fails.
Example:
user, err := sdk.GetMe()
if err != nil {
log.Fatal(err)
}
fmt.Printf("User: %+v\n", user)Description:
Performs a generic API request to the specified Reddit endpoint.
Parameters:
method(string): The HTTP method (e.g., "GET", "POST").endpoint(string): The API endpoint path (e.g., "/api/v1/me").body(io.Reader): The request body (if any).
Returns:
(*http.Response, error) - The response from the Reddit API or an error.
Example:
resp, err := sdk.MakeRequest("GET", "/api/v1/me", nil)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
// Handle response...Description:
Refreshes the access token if it is expired or near expiration.
Parameters:
None.
Returns:
error - Returns an error if the token refresh fails.
Example:
err := sdk.RefreshTokenIfNeeded()
if err != nil {
log.Fatal("Token refresh failed:", err)
}