feat: added mit-level API for tags

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 11:15:03 +02:00
parent 141a06a5a0
commit f26e5b223a
4 changed files with 247 additions and 0 deletions

79
example/midlevel/tag.go Normal file
View File

@ -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("----------")
}

36
pkg/e621/builder/tag.go Normal file
View File

@ -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
}

View File

@ -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)
}

78
pkg/e621/builder/tags.go Normal file
View File

@ -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
}