e621-sdk-go/pkg/e621/builder/post.go
Alphyron 214aaceee0
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 35s
chore: update mod path and update dependencies
2024-08-20 08:07:46 +02:00

48 lines
1.3 KiB
Go

package builder
import (
"context"
"git.anthrove.art/Anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.anthrove.art/Anthrove/e621-sdk-go/pkg/e621/model"
"strconv"
)
// PostBuilder represents a builder for constructing queries to retrieve a specific post.
type PostBuilder interface {
// SetPostID sets the post ID for the query.
SetPostID(postID model.PostID) PostBuilder
// Execute sends the constructed query and returns the requested post and an error, if any.
Execute() (*model.Post, error)
}
// NewGetPostBuilder creates a new PostBuilder with the provided RequestContext.
func NewGetPostBuilder(requestContext model.RequestContext) PostBuilder {
return &getPost{requestContext: requestContext}
}
type getPost struct {
requestContext model.RequestContext
postID model.PostID
}
// SetPostID sets the post ID for the query.
func (g *getPost) SetPostID(postID model.PostID) PostBuilder {
g.postID = postID
return g
}
// Execute sends the constructed query and returns the requested post and an error, if any.
func (g *getPost) Execute() (*model.Post, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
post, err := endpoints.GetPost(g.requestContext, strconv.Itoa(int(g.postID)))
if err != nil {
return nil, err
}
return &post, nil
}