From a8a87b83d9bba6c50fa9229565a9813f9be3fed2 Mon Sep 17 00:00:00 2001 From: David Janowski Date: Thu, 11 Jul 2024 12:57:41 +0200 Subject: [PATCH] feat: partially added DMails into the Builder --- pkg/e621/builder/dmail.go | 79 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 pkg/e621/builder/dmail.go diff --git a/pkg/e621/builder/dmail.go b/pkg/e621/builder/dmail.go new file mode 100644 index 0000000..aae3714 --- /dev/null +++ b/pkg/e621/builder/dmail.go @@ -0,0 +1,79 @@ +package builder + +import ( + "context" + "strconv" + + "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" +) + +type DMailsBuilder interface { + ByTitle(title string) DMailsBuilder + ByBody(body string) DMailsBuilder + ByToName(toName string) DMailsBuilder + ByFromName(fromName string) DMailsBuilder + SetLimit(limit int) DMailsBuilder + PageNumber(number int) DMailsBuilder + Execute() ([]model.DMail, error) +} + +type getDMails struct { + requestContext model.RequestContext + query map[string]string + id int +} + +func NewGetDMailsBuilder(requestContext model.RequestContext) DMailsBuilder { + dMailsBuilder := &getDMails{ + requestContext: requestContext, + query: make(map[string]string), + } + + return dMailsBuilder.SetLimit(utils.E621_MAX_POST_COUNT) +} + +func (g getDMails) ByTitle(title string) DMailsBuilder { + g.query["title_matches"] = title + return g +} + +func (g getDMails) ByBody(body string) DMailsBuilder { + g.query["message_matches"] = body + return g +} + +func (g getDMails) ByToName(toName string) DMailsBuilder { + g.query["to_name"] = toName + return g +} + +func (g getDMails) ByFromName(fromName string) DMailsBuilder { + g.query["from_name"] = fromName + return g +} + +func (g getDMails) SetLimit(limit int) DMailsBuilder { + g.query["limit"] = strconv.Itoa(limit) + return g +} + +func (g getDMails) PageNumber(number int) DMailsBuilder { + g.query["page"] = strconv.Itoa(number) + return g +} + +func (g getDMails) Execute() ([]model.DMail, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } + favorites, err := endpoints.GetAllDmails(g.requestContext, g.query) + if err != nil { + return nil, err + } + return favorites, nil +}