e621-sdk-go/pkg/e621/builder/user.go

47 lines
1.4 KiB
Go
Raw Permalink Normal View History

package builder
import (
"context"
"git.anthrove.art/Anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.anthrove.art/Anthrove/e621-sdk-go/pkg/e621/model"
)
// UserBuilder represents a builder for constructing queries to retrieve user information.
type UserBuilder interface {
// SetUsername sets the username for the query to retrieve user information.
SetUsername(username string) UserBuilder
// Execute sends the constructed query and returns the requested user information and an error, if any.
Execute() (model.User, error)
}
// NewGetUserBuilder creates a new UserBuilder with the provided RequestContext.
func NewGetUserBuilder(requestContext model.RequestContext) UserBuilder {
return &getUser{requestContext: requestContext}
}
type getUser struct {
requestContext model.RequestContext
username string
}
// SetUsername sets the username for the query to retrieve user information.
func (g *getUser) SetUsername(username string) UserBuilder {
g.username = username
return g
}
// Execute sends the constructed query and returns the requested user information and an error, if any.
func (g *getUser) Execute() (model.User, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return model.User{}, err
}
}
user, err := endpoints.GetUser(g.requestContext, g.username)
if err != nil {
return model.User{}, err
}
return user, nil
}