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 instance of PostsBuilder with the provided RequestContext. // // Parameters: // - requestContext: The context for the API request, including the host, user agent, username, and API key. // // Returns: // - PostsBuilder: An instance of the PostsBuilder interface. func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder { postBuilder := &getPosts{ requestContext: requestContext, query: make(map[string]string), } return postBuilder.SetLimit(utils.E621_MAX_POST_COUNT) } // getPosts is an implementation of the PostsBuilder interface. type getPosts struct { requestContext model.RequestContext query map[string]string } // Tags sets the query to search for posts with specific tags. // // Parameters: // - tags: The tags to search for in the posts. // // Returns: // - PostsBuilder: The instance of the PostsBuilder for method chaining. 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. // // Parameters: // - postID: The ID of the post after which to retrieve other posts. // // Returns: // - PostsBuilder: The instance of the PostsBuilder for method chaining. 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. // // Parameters: // - postID: The ID of the post before which to retrieve other posts. // // Returns: // - PostsBuilder: The instance of the PostsBuilder for method chaining. 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. // // Parameters: // - number: The page number to retrieve posts from. // // Returns: // - PostsBuilder: The instance of the PostsBuilder for method chaining. func (g *getPosts) PageNumber(number int) PostsBuilder { g.query["page"] = strconv.Itoa(number) return g } // SetLimit sets the maximum number of posts to retrieve. // // Parameters: // - limitUser: The maximum number of posts to retrieve. // // Returns: // - PostsBuilder: The instance of the PostsBuilder for method chaining. 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. // // Returns: // - []model.Post: A slice of posts. // - error: An error, if any, encountered during the API request or response handling. 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 }