86 lines
2.8 KiB
Go
86 lines
2.8 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"
|
|
"git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils"
|
|
"strconv"
|
|
)
|
|
|
|
// PostsBuilder represents a builder for constructing queries to retrieve posts.
|
|
type PostsBuilder interface {
|
|
// Tags sets the query to search for posts with specific tags.
|
|
Tags(tags string) PostsBuilder
|
|
// PageAfter sets the query to retrieve posts after a specific post ID.
|
|
PageAfter(postID model.PostID) PostsBuilder
|
|
// PageBefore sets the query to retrieve posts before a specific post ID.
|
|
PageBefore(postID model.PostID) PostsBuilder
|
|
// PageNumber sets the query to retrieve posts on a specific page number.
|
|
PageNumber(number int) PostsBuilder
|
|
// SetLimit sets the maximum number of posts to retrieve.
|
|
SetLimit(limitUser int) PostsBuilder
|
|
// Execute sends the constructed query and returns a slice of posts and an error, if any.
|
|
Execute() ([]model.Post, error)
|
|
}
|
|
|
|
// NewGetPostsBuilder creates a new PostsBuilder with the provided RequestContext.
|
|
func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder {
|
|
postBuilder := &getPosts{
|
|
requestContext: requestContext,
|
|
query: make(map[string]string),
|
|
}
|
|
|
|
return postBuilder.SetLimit(utils.E621_MAX_POST_COUNT)
|
|
}
|
|
|
|
type getPosts struct {
|
|
requestContext model.RequestContext
|
|
query map[string]string
|
|
}
|
|
|
|
// Tags sets the query to search for posts with specific tags.
|
|
func (g *getPosts) Tags(tags string) PostsBuilder {
|
|
g.query["tags"] = tags
|
|
return g
|
|
}
|
|
|
|
// PageAfter sets the query to retrieve posts after a specific post ID.
|
|
func (g *getPosts) PageAfter(postID model.PostID) PostsBuilder {
|
|
g.query["page"] = "a" + strconv.Itoa(int(postID))
|
|
return g
|
|
}
|
|
|
|
// PageBefore sets the query to retrieve posts before a specific post ID.
|
|
func (g *getPosts) PageBefore(postID model.PostID) PostsBuilder {
|
|
g.query["page"] = "b" + strconv.Itoa(int(postID))
|
|
return g
|
|
}
|
|
|
|
// PageNumber sets the query to retrieve posts on a specific page number.
|
|
func (g *getPosts) PageNumber(number int) PostsBuilder {
|
|
g.query["page"] = strconv.Itoa(number)
|
|
return g
|
|
}
|
|
|
|
// SetLimit sets the maximum number of posts to retrieve.
|
|
func (g *getPosts) SetLimit(limitUser int) PostsBuilder {
|
|
g.query["limit"] = strconv.Itoa(limitUser)
|
|
return g
|
|
}
|
|
|
|
// Execute sends the constructed query and returns a slice of posts and an error, if any.
|
|
func (g *getPosts) Execute() ([]model.Post, error) {
|
|
if g.requestContext.RateLimiter != nil {
|
|
err := g.requestContext.RateLimiter.Wait(context.Background())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
posts, err := endpoints.GetPosts(g.requestContext, g.query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return posts, err
|
|
}
|