This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
e621-sdk-go/pkg/e621/builder/favorite.go
2023-11-06 15:56:36 +01:00

67 lines
2.1 KiB
Go

package builder
import (
"context"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"strconv"
)
// FavoritesBuilder represents a builder for constructing queries to retrieve user's favorite posts.
type FavoritesBuilder interface {
// SetUserID sets the user ID for the query.
SetUserID(userID model.UserID) FavoritesBuilder
// SetLimit sets the maximum number of favorite posts to retrieve.
SetLimit(limitFavorites int) FavoritesBuilder
// Page sets the page number for paginated results.
Page(pageNumber int) FavoritesBuilder
// Execute sends the constructed query and returns a slice of favorite posts and an error if any.
Execute() ([]model.Post, error)
}
// NewGetFavoritesBuilder creates a new FavoritesBuilder with the provided RequestContext.
func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder {
return &getFavorites{
requestContext: requestContext,
query: make(map[string]string),
}
}
type getFavorites struct {
query map[string]string
requestContext model.RequestContext
}
// SetUserID sets the user ID for the query.
func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder {
g.query["user_id"] = strconv.Itoa(int(userID))
return g
}
// SetLimit sets the maximum number of favorite posts to retrieve.
func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
g.query["limit"] = strconv.Itoa(limitFavorites)
return g
}
// Page sets the page number for paginated results.
func (g *getFavorites) Page(pageNumber int) FavoritesBuilder {
g.query["page"] = strconv.Itoa(pageNumber)
return g
}
// Execute sends the constructed query and returns a slice of favorite posts and an error if any.
func (g *getFavorites) Execute() ([]model.Post, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
favorites, err := endpoints.GetFavorites(g.requestContext, g.query)
if err != nil {
return nil, err
}
return favorites, nil
}