From f26e5b223aa91dc4a32f7c93f5880e4d20bb5a60 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 23 Oct 2023 11:15:03 +0200 Subject: [PATCH] feat: added mit-level API for tags Signed-off-by: SoXX --- example/midlevel/tag.go | 79 ++++++++++++++++++++++++++++++++++++ pkg/e621/builder/tag.go | 36 ++++++++++++++++ pkg/e621/builder/tag_test.go | 54 ++++++++++++++++++++++++ pkg/e621/builder/tags.go | 78 +++++++++++++++++++++++++++++++++++ 4 files changed, 247 insertions(+) create mode 100644 example/midlevel/tag.go create mode 100644 pkg/e621/builder/tag.go create mode 100644 pkg/e621/builder/tag_test.go create mode 100644 pkg/e621/builder/tags.go diff --git a/example/midlevel/tag.go b/example/midlevel/tag.go new file mode 100644 index 0000000..ed45a85 --- /dev/null +++ b/example/midlevel/tag.go @@ -0,0 +1,79 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" + "net/http" +) + +func main() { + // Define the request context with essential information. + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "selloo", // Replace with your username + APIKey: "zt7XAaewLqP4AWvQi5WrLNwN", // Replace with your API key + } + + // Log: Getting a single tag. + log.Println("Getting a single tag: ") + + // Specify the tag ID for retrieval. + tagID := 1530 // Replace with the desired tag's ID. + + // Call the GetTag function to retrieve the specified tag. + getTag := builder.NewGetTagBuilder(requestContext) + tag, err := getTag.SetTagID(tagID).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the name of the retrieved tag. + log.Println(tag.Name) + } + log.Println("----------") + + // Log: Getting a list of tags. + log.Println("Getting a list of tags: ") + + // Call the GetTags function to retrieve a list of tags based on the query parameters. + getTags := builder.NewGetTagsBuilder(requestContext) + tagList, err := getTags.SetLimit(5).Artist(false).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the number of tags retrieved. + log.Println(len(tagList)) + } + log.Println("----------") + + // Log: Searching for tags containing "cat." + log.Println("Searching for tags containing 'cat': ") + + // Call the GetTags function with the search query to retrieve matching tags. + tagList, err = getTags.SetLimit(5).SearchName("cat*").Execute() + if err != nil { + log.Println(err) + } else { + // Log the retrieved tags. + log.Println(tagList) + } + log.Println("----------") + + // Log: Searching for tags with the category "artist." + log.Println("Searching for tags with the category 'artist': ") + + // Call the GetTags function with the category filter to retrieve artist tags. + tagList, err = getTags.SetLimit(5).SearchCategory(model.Artist).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the retrieved artist tags. + log.Println(tagList) + } + log.Println("----------") +} diff --git a/pkg/e621/builder/tag.go b/pkg/e621/builder/tag.go new file mode 100644 index 0000000..3a55cdb --- /dev/null +++ b/pkg/e621/builder/tag.go @@ -0,0 +1,36 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" + "strconv" +) + +type TagBuilder interface { + SetTagID(tagID int) TagBuilder + Execute() (model.Tag, error) +} + +func NewGetTagBuilder(requstContext model.RequestContext) TagBuilder { + return &getTag{requestContext: requstContext} +} + +type getTag struct { + requestContext model.RequestContext + tagID int +} + +func (g *getTag) SetTagID(tagID int) TagBuilder { + g.tagID = tagID + return g +} + +func (g *getTag) Execute() (model.Tag, error) { + tag, err := endpoints.GetTag(g.requestContext, strconv.Itoa(g.tagID)) + if err != nil { + log.Println(err) + return model.Tag{}, err + } + return tag, nil +} diff --git a/pkg/e621/builder/tag_test.go b/pkg/e621/builder/tag_test.go new file mode 100644 index 0000000..532642e --- /dev/null +++ b/pkg/e621/builder/tag_test.go @@ -0,0 +1,54 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetTag(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + tagResponse := model.Tag{ + ID: 165165, + Name: "35ad13", + PostCount: 59, + RelatedTags: "[]", + RelatedTagsUpdatedAt: "2023-10-16T07:58:15.897+02:00", + Category: 0, + IsLocked: false, + CreatedAt: "2023-10-16T07:58:15.917+02:00", + UpdatedAt: "2023-10-16T07:58:23.310+02:00", + } + + jsonResponser, err := httpmock.NewJsonResponder(200, tagResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/tags/165165.json", jsonResponser) + + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + getTag := NewGetTagBuilder(requestContext) + tag, err := getTag.SetTagID(165165).Execute() + if err != nil { + t.Error(err) + return + } + + if tag.ID == tagResponse.ID && tag.Name == tagResponse.Name && tag.CreatedAt == tag.CreatedAt { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", tag, tagResponse) + +} diff --git a/pkg/e621/builder/tags.go b/pkg/e621/builder/tags.go new file mode 100644 index 0000000..62cb8e5 --- /dev/null +++ b/pkg/e621/builder/tags.go @@ -0,0 +1,78 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" + "strconv" +) + +type TagsBuilder interface { + SearchName(name string) TagsBuilder + SearchCategory(category model.TagCategory) TagsBuilder + Order(order string) TagsBuilder + HideEmpty(hideEmpty bool) TagsBuilder + Wiki(hasWiki bool) TagsBuilder + Artist(hasArtistPage bool) TagsBuilder + SetPage(pageNumber int) TagsBuilder + SetLimit(limitUser int) TagsBuilder + Execute() ([]model.Tag, error) +} + +func NewGetTagsBuilder(requstContext model.RequestContext) TagsBuilder { + return &getTags{requestContext: requstContext, query: make(map[string]string)} +} + +type getTags struct { + requestContext model.RequestContext + query map[string]string +} + +func (g *getTags) SearchName(name string) TagsBuilder { + g.query["search[name_matches]"] = name + return g +} + +func (g *getTags) SearchCategory(category model.TagCategory) TagsBuilder { + g.query["search[category]"] = strconv.Itoa(int(category)) + return g +} + +func (g *getTags) Order(order string) TagsBuilder { + g.query["search[order]"] = order + return g +} + +func (g *getTags) HideEmpty(hideEmpty bool) TagsBuilder { + g.query["search[hide_empty]"] = strconv.FormatBool(hideEmpty) + return g +} + +func (g *getTags) Wiki(hasWiki bool) TagsBuilder { + g.query["search[has_wiki]"] = strconv.FormatBool(hasWiki) + return g +} + +func (g *getTags) Artist(hasArtistPage bool) TagsBuilder { + g.query["search[has_artist]"] = strconv.FormatBool(hasArtistPage) + return g +} + +func (g *getTags) SetPage(pageNumber int) TagsBuilder { + g.query["page"] = strconv.Itoa(pageNumber) + return g +} + +func (g *getTags) SetLimit(limitUser int) TagsBuilder { + g.query["limit"] = strconv.Itoa(limitUser) + return g +} + +func (g *getTags) Execute() ([]model.Tag, error) { + tags, err := endpoints.GetTags(g.requestContext, g.query) + if err != nil { + log.Println(err) + return nil, err + } + return tags, err +}