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/posts.go
SoXX f446823abd feat: added getting n posts
Signed-off-by: SoXX <soxx@fenpa.ws>
2023-11-06 12:42:40 +01:00

67 lines
1.5 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"
)
type PostsBuilder interface {
Tags(tags string) PostsBuilder
PageAfter(postID model.PostID) PostsBuilder
pageBefore(postID model.PostID) PostsBuilder
SetLimit(limitUser int) PostsBuilder
Execute() ([]model.Post, error)
}
func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder {
var postBuilder PostsBuilder = &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
}
func (g *getPosts) Tags(tags string) PostsBuilder {
g.query["tags"] = tags
return g
}
func (g *getPosts) PageAfter(postID model.PostID) PostsBuilder {
g.query["page"] = "a" + strconv.Itoa(int(postID))
return g
}
func (g *getPosts) pageBefore(postID model.PostID) PostsBuilder {
g.query["page"] = "b" + strconv.Itoa(int(postID))
return g
}
func (g *getPosts) SetLimit(limitUser int) PostsBuilder {
g.query["limit"] = strconv.Itoa(limitUser)
return g
}
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
}