From 989f853d72800d53ff50f640dbb4bcb35d2364a8 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 23 Oct 2023 14:27:20 +0200 Subject: [PATCH] feat: added mid-level API for pools Signed-off-by: SoXX --- README.md | 2 +- example/midlevel/pool.go | 57 ++++++++++++++++++++ pkg/e621/builder/pool.go | 36 +++++++++++++ pkg/e621/builder/pool_test.go | 56 ++++++++++++++++++++ pkg/e621/builder/pools.go | 87 ++++++++++++++++++++++++++++++ pkg/e621/builder/pools_test.go | 97 ++++++++++++++++++++++++++++++++++ pkg/e621/model/pool.go | 34 ++++++++---- 7 files changed, 357 insertions(+), 12 deletions(-) create mode 100644 example/midlevel/pool.go create mode 100644 pkg/e621/builder/pool.go create mode 100644 pkg/e621/builder/pool_test.go create mode 100644 pkg/e621/builder/pools.go create mode 100644 pkg/e621/builder/pools_test.go diff --git a/README.md b/README.md index 23948aa..551f50f 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ _Mid Level API_ | Tag aliases | :x: | | | Tag implications | :x: | | | Notes | :x: | | -| Pools | :x: | | +| Pools | :heavy_check_mark: | | | Users | :heavy_check_mark: | | | Favorites | :x: | | | DB export | :x: | | diff --git a/example/midlevel/pool.go b/example/midlevel/pool.go new file mode 100644 index 0000000..0ca5cb6 --- /dev/null +++ b/example/midlevel/pool.go @@ -0,0 +1,57 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + _ "github.com/joho/godotenv/autoload" + "log" + "net/http" + "os" +) + +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: os.Getenv("API_USER"), // Replace with your username + APIKey: os.Getenv("API_KEY"), // Replace with your API key + } + + // Log: Getting a single pool. + log.Println("Getting single pool: ") + + // Specify the pool ID for retrieval. + poolID := 36957 // Replace with the desired pool's ID. + + // Call the GetPool function to retrieve the specified pool. + getPool := builder.NewGetPoolBuilder(requestContext) + pool, err := getPool.ID(poolID).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the name of the retrieved pool. + log.Println(pool.Name) + } + log.Println("----------") + + // Log: Getting a list of pools. + log.Println("Getting a list of pools: ") + + // Call the GetPools function to retrieve a list of pools based on the query parameters. + getPools := builder.NewGetPoolsBuilder(requestContext) + pools, err := getPools.SetLimit(5).Active(true).SearchDescritpion("mass effect").Execute() + + if err != nil { + log.Println(err) + } else { + // Log the number of pools retrieved. + log.Println(len(pools)) + log.Println(pools[1].Name) + log.Println(pools[1].ID) + } + log.Println("----------") + +} diff --git a/pkg/e621/builder/pool.go b/pkg/e621/builder/pool.go new file mode 100644 index 0000000..c0fabd1 --- /dev/null +++ b/pkg/e621/builder/pool.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 PoolBuilder interface { + ID(poolID int) PoolBuilder + Execute() (model.Pool, error) +} + +func NewGetPoolBuilder(requestContext model.RequestContext) PoolBuilder { + return &getPool{requestContext: requestContext} +} + +type getPool struct { + requestContext model.RequestContext + id int +} + +func (g getPool) ID(poolID int) PoolBuilder { + g.id = poolID + return g +} + +func (g getPool) Execute() (model.Pool, error) { + pool, err := endpoints.GetPool(g.requestContext, strconv.Itoa(g.id)) + if err != nil { + log.Println(err) + return model.Pool{}, err + } + return pool, nil +} diff --git a/pkg/e621/builder/pool_test.go b/pkg/e621/builder/pool_test.go new file mode 100644 index 0000000..9ae1ff4 --- /dev/null +++ b/pkg/e621/builder/pool_test.go @@ -0,0 +1,56 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetPool(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + poolResponse := model.Pool{ + ID: 36957, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, poolResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/pools/36957.json", jsonResponser) + + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + getPool := NewGetPoolBuilder(requestContext) + pool, err := getPool.ID(36957).Execute() + if err != nil { + t.Error(err) + return + } + + if pool.ID == poolResponse.ID && pool.Name == poolResponse.Name { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pool, poolResponse) + +} diff --git a/pkg/e621/builder/pools.go b/pkg/e621/builder/pools.go new file mode 100644 index 0000000..a65c985 --- /dev/null +++ b/pkg/e621/builder/pools.go @@ -0,0 +1,87 @@ +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 PoolsBuilder interface { + SearchName(name string) PoolsBuilder + SearchID(poolIDs string) PoolsBuilder + SearchDescritpion(description string) PoolsBuilder + SearchCreatorID(creatorID int) PoolsBuilder + searchCreatorName(creatorName string) PoolsBuilder + Active(isActive bool) PoolsBuilder + SearchCategory(category model.PoolCategory) PoolsBuilder + Order(order model.PoolOrder) PoolsBuilder + SetLimit(limitPools int) PoolsBuilder + Execute() ([]model.Pool, error) +} + +func NewGetPoolsBuilder(requstContext model.RequestContext) PoolsBuilder { + return &getPools{ + requestContext: requstContext, + query: make(map[string]string), + } +} + +type getPools struct { + requestContext model.RequestContext + query map[string]string +} + +func (g *getPools) SearchName(name string) PoolsBuilder { + g.query["search[name_matches]"] = name + return g +} + +func (g *getPools) SearchID(poolIDs string) PoolsBuilder { + g.query["search[id]"] = poolIDs + return g +} + +func (g *getPools) SearchDescritpion(description string) PoolsBuilder { + g.query["search[description_matches]"] = description + return g +} + +func (g *getPools) SearchCreatorID(creatorID int) PoolsBuilder { + g.query["search[creator_id]"] = strconv.Itoa(creatorID) + return g +} + +func (g *getPools) searchCreatorName(creatorName string) PoolsBuilder { + g.query["search[creator_name]"] = creatorName + return g +} + +func (g *getPools) Active(isActive bool) PoolsBuilder { + g.query["search[is_active]"] = strconv.FormatBool(isActive) + return g +} + +func (g *getPools) SearchCategory(category model.PoolCategory) PoolsBuilder { + g.query["search[category]"] = string(category) + return g +} + +func (g *getPools) Order(order model.PoolOrder) PoolsBuilder { + g.query["search[order]"] = string(order) + return g +} + +func (g *getPools) SetLimit(limitUser int) PoolsBuilder { + g.query["limit"] = strconv.Itoa(limitUser) + return g +} + +func (g *getPools) Execute() ([]model.Pool, error) { + pools, err := endpoints.GetPools(g.requestContext, g.query) + if err != nil { + log.Println(err) + return nil, err + } + return pools, nil +} diff --git a/pkg/e621/builder/pools_test.go b/pkg/e621/builder/pools_test.go new file mode 100644 index 0000000..18ef709 --- /dev/null +++ b/pkg/e621/builder/pools_test.go @@ -0,0 +1,97 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetPools(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + poolResponse := []model.Pool{ + { + ID: 36957, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + { + ID: 5772, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + { + ID: 453687, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + { + ID: 3456876, + Name: "MLP:His_Nocturnal_Excellency", + CreatedAt: "2023-10-13T19:24:00.576-04:00", + UpdatedAt: "2023-10-19T07:46:00.267-04:00", + CreatorID: 16883, + Description: "Two (un)lucky royal guards who saw something they didn’t have to~", + IsActive: true, + Category: "series", + PostIDS: nil, + CreatorName: "2DUK", + PostCount: 11, + }, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, poolResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/pools.json", jsonResponser) + + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + getPools := NewGetPoolsBuilder(requestContext) + pools, err := getPools.SetLimit(4).Execute() + if err != nil { + t.Error(err) + return + } + + if len(pools) == 4 && pools[0].ID == poolResponse[0].ID && pools[1].Name == poolResponse[1].Name && pools[2].UpdatedAt == poolResponse[2].UpdatedAt { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", pools, poolResponse) + +} diff --git a/pkg/e621/model/pool.go b/pkg/e621/model/pool.go index 26879d1..3af933d 100644 --- a/pkg/e621/model/pool.go +++ b/pkg/e621/model/pool.go @@ -1,15 +1,27 @@ package model +type PoolCategory string +type PoolOrder string + +const ( + Series PoolCategory = "series" + Collection PoolCategory = "collection" + Name PoolOrder = "name" + CreatedAt PoolOrder = "created_at" + UpdatedAt PoolOrder = "updated_at" + PostCount PoolOrder = "post_count" +) + type Pool struct { - ID int64 `json:"id"` - Name string `json:"name"` - CreatedAt string `json:"created_at"` - UpdatedAt string `json:"updated_at"` - CreatorID int64 `json:"creator_id"` - Description string `json:"description"` - IsActive bool `json:"is_active"` - Category string `json:"category"` - PostIDS []int64 `json:"post_ids"` - CreatorName string `json:"creator_name"` - PostCount int64 `json:"post_count"` + ID int64 `json:"id"` + Name string `json:"name"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` + CreatorID int64 `json:"creator_id"` + Description string `json:"description"` + IsActive bool `json:"is_active"` + Category PoolCategory `json:"category"` + PostIDS []int64 `json:"post_ids"` + CreatorName string `json:"creator_name"` + PostCount int64 `json:"post_count"` }