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/basic/pools.go
soxx be44a669d9 refactor: start rework of the high level API
Signed-off-by: soxx <soxx@fenpa.ws>
2023-11-27 08:12:58 +01:00

180 lines
5.9 KiB
Go

package basic
import (
"context"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model"
"strconv"
)
// PoolsBuilder represents a builder for constructing queries to retrieve pools.
type PoolsBuilder interface {
// SearchName sets the query to search for pools with a specific name.
SearchName(name string) PoolsBuilder
// SearchID sets the query to search for pools with specific pool IDs.
SearchID(poolIDs string) PoolsBuilder
// SearchDescription sets the query to search for pools with specific descriptions.
SearchDescription(description string) PoolsBuilder
// SearchCreatorID sets the query to search for pools created by a specific user (by their user ID).
SearchCreatorID(creatorID int) PoolsBuilder
// SearchCreatorName sets the query to search for pools created by a specific user (by their username).
SearchCreatorName(creatorName string) PoolsBuilder
// Active sets whether to search for active or inactive pools.
Active(isActive bool) PoolsBuilder
// SearchCategory sets the query to search for pools in a specific category.
SearchCategory(category model.PoolCategory) PoolsBuilder
// Order sets the ordering of the retrieved pools.
Order(order model.PoolOrder) PoolsBuilder
// SetLimit sets the maximum number of pools to retrieve.
SetLimit(limitPools int) PoolsBuilder
// Execute sends the constructed query and returns a slice of pools and an error, if any.
Execute() ([]model.Pool, error)
}
// NewGetPoolsBuilder creates a new instance of PoolsBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - PoolsBuilder: An instance of the PoolsBuilder interface.
func NewGetPoolsBuilder(requestContext model.RequestContext) PoolsBuilder {
return &getPools{
requestContext: requestContext,
query: make(map[string]string),
}
}
// getPools is an implementation of the PoolsBuilder interface.
type getPools struct {
requestContext model.RequestContext
query map[string]string
}
// SearchName sets the query to search for pools with a specific name.
//
// Parameters:
// - name: The name to search for in the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchName(name string) PoolsBuilder {
g.query["search[name_matches]"] = name
return g
}
// SearchID sets the query to search for pools with specific pool IDs.
//
// Parameters:
// - poolIDs: The pool IDs to search for, separated by commas.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchID(poolIDs string) PoolsBuilder {
g.query["search[id]"] = poolIDs
return g
}
// SearchDescription sets the query to search for pools with specific descriptions.
//
// Parameters:
// - description: The description to search for in the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchDescription(description string) PoolsBuilder {
g.query["search[description_matches]"] = description
return g
}
// SearchCreatorID sets the query to search for pools created by a specific user (by their user ID).
//
// Parameters:
// - creatorID: The ID of the user who created the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchCreatorID(creatorID int) PoolsBuilder {
g.query["search[creator_id]"] = strconv.Itoa(creatorID)
return g
}
// SearchCreatorName sets the query to search for pools created by a specific user (by their username).
//
// Parameters:
// - creatorName: The username of the user who created the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchCreatorName(creatorName string) PoolsBuilder {
g.query["search[creator_name]"] = creatorName
return g
}
// Active sets whether to search for active or inactive pools.
//
// Parameters:
// - isActive: A boolean indicating whether to search for active pools (true) or inactive pools (false).
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) Active(isActive bool) PoolsBuilder {
g.query["search[is_active]"] = strconv.FormatBool(isActive)
return g
}
// SearchCategory sets the query to search for pools in a specific category.
//
// Parameters:
// - category: The category to search for in the pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SearchCategory(category model.PoolCategory) PoolsBuilder {
g.query["search[category]"] = string(category)
return g
}
// Order sets the ordering of the retrieved pools.
//
// Parameters:
// - order: The order to apply to the retrieved pools.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) Order(order model.PoolOrder) PoolsBuilder {
g.query["search[order]"] = string(order)
return g
}
// SetLimit sets the maximum number of pools to retrieve.
//
// Parameters:
// - limitPools: The maximum number of pools to retrieve.
//
// Returns:
// - PoolsBuilder: The instance of the PoolsBuilder for method chaining.
func (g *getPools) SetLimit(limitPools int) PoolsBuilder {
g.query["limit"] = strconv.Itoa(limitPools)
return g
}
// Execute sends the constructed query and returns a slice of pools and an error, if any.
//
// Returns:
// - []model.Pool: A slice of pools.
// - error: An error, if any, encountered during the API request or response handling.
func (g *getPools) Execute() ([]model.Pool, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
pools, err := endpoints.GetPools(g.requestContext, g.query)
if err != nil {
return nil, err
}
return pools, nil
}