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

48 lines
1.3 KiB
Go
Raw Normal View History

package builder
import (
"context"
2024-07-20 09:04:53 +00:00
"git.anthrove.art/anthrove/e621-sdk-go/pkg/e621/endpoints"
"git.anthrove.art/anthrove/e621-sdk-go/pkg/e621/model"
"strconv"
)
// NoteBuilder represents a builder for constructing queries to retrieve a specific note.
type NoteBuilder interface {
// SetNoteID sets the note ID for the query.
SetNoteID(noteID int) NoteBuilder
// Execute sends the constructed query and returns the requested note and an error, if any.
Execute() (*model.Note, error)
}
// NewGetNoteBuilder creates a new NoteBuilder with the provided RequestContext.
func NewGetNoteBuilder(requestContext model.RequestContext) NoteBuilder {
return &getNote{requestContext: requestContext}
}
type getNote struct {
requestContext model.RequestContext
noteID int
}
// SetNoteID sets the note ID for the query.
func (g *getNote) SetNoteID(noteID int) NoteBuilder {
g.noteID = noteID
return g
}
// Execute sends the constructed query and returns the requested note and an error, if any.
func (g *getNote) Execute() (*model.Note, error) {
if g.requestContext.RateLimiter != nil {
err := g.requestContext.RateLimiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
note, err := endpoints.GetNote(g.requestContext, strconv.Itoa(g.noteID))
if err != nil {
return nil, err
}
return &note, nil
}