feat: added mid-level API for pools

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 14:27:20 +02:00
parent af69de28cf
commit 989f853d72
7 changed files with 357 additions and 12 deletions

View File

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

57
example/midlevel/pool.go Normal file
View File

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

36
pkg/e621/builder/pool.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 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
}

View File

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

87
pkg/e621/builder/pools.go Normal file
View File

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

View File

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

View File

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