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/post.go

65 lines
1.9 KiB
Go
Raw Normal View History

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"
)
// 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 instance of PostBuilder with the provided RequestContext.
//
// Parameters:
// - requestContext: The context for the API request, including the host, user agent, username, and API key.
//
// Returns:
// - PostBuilder: An instance of the PostBuilder interface.
func NewGetPostBuilder(requestContext model.RequestContext) PostBuilder {
return &getPost{requestContext: requestContext}
}
// getPost is an implementation of the PostBuilder interface.
type getPost struct {
requestContext model.RequestContext
postID model.PostID
}
// SetPostID sets the post ID for the query.
//
// Parameters:
// - postID: The ID of the post to be retrieved.
//
// Returns:
// - PostBuilder: The instance of the PostBuilder for method chaining.
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.
//
// Returns:
// - *model.Post: The requested post.
// - error: An error, if any, encountered during the API request or response handling.
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
}