From be44a669d90c86857f3b668b2fdecfa9d3ae04fb Mon Sep 17 00:00:00 2001 From: soxx Date: Mon, 27 Nov 2023 08:12:58 +0100 Subject: [PATCH] refactor: start rework of the high level API Signed-off-by: soxx --- example/midlevel/dbexport.go | 6 +- example/midlevel/favorite.go | 4 +- example/midlevel/note.go | 6 +- example/midlevel/pool.go | 6 +- example/midlevel/post.go | 6 +- example/midlevel/tag.go | 6 +- example/midlevel/user.go | 6 +- .../builder/{ => basic}/db_export_file.go | 2 +- .../builder/{ => basic}/db_export_list.go | 2 +- pkg/e621/builder/basic/favorite.go | 95 +++++++++++ pkg/e621/builder/{ => basic}/favorite_test.go | 2 +- pkg/e621/builder/{ => basic}/note.go | 2 +- pkg/e621/builder/{ => basic}/note_test.go | 2 +- pkg/e621/builder/{ => basic}/notes.go | 2 +- pkg/e621/builder/{ => basic}/notes_test.go | 2 +- pkg/e621/builder/{ => basic}/pool.go | 2 +- pkg/e621/builder/{ => basic}/pool_test.go | 2 +- pkg/e621/builder/{ => basic}/pools.go | 2 +- pkg/e621/builder/{ => basic}/pools_test.go | 2 +- pkg/e621/builder/{ => basic}/post.go | 2 +- pkg/e621/builder/{ => basic}/post_test.go | 2 +- pkg/e621/builder/{ => basic}/posts.go | 2 +- pkg/e621/builder/{ => basic}/posts_test.go | 2 +- pkg/e621/builder/{ => basic}/tag.go | 2 +- pkg/e621/builder/{ => basic}/tag_test.go | 2 +- pkg/e621/builder/{ => basic}/tags.go | 2 +- pkg/e621/builder/{ => basic}/user.go | 2 +- pkg/e621/builder/{ => basic}/user_test.go | 2 +- pkg/e621/builder/{ => basic}/users.go | 2 +- pkg/e621/builder/{ => basic}/users_test.go | 2 +- pkg/e621/builder/favorite.go | 153 +++++++++++------- pkg/e621/client.go | 88 +++++----- 32 files changed, 275 insertions(+), 145 deletions(-) rename pkg/e621/builder/{ => basic}/db_export_file.go (99%) rename pkg/e621/builder/{ => basic}/db_export_list.go (98%) create mode 100644 pkg/e621/builder/basic/favorite.go rename pkg/e621/builder/{ => basic}/favorite_test.go (98%) rename pkg/e621/builder/{ => basic}/note.go (99%) rename pkg/e621/builder/{ => basic}/note_test.go (98%) rename pkg/e621/builder/{ => basic}/notes.go (99%) rename pkg/e621/builder/{ => basic}/notes_test.go (98%) rename pkg/e621/builder/{ => basic}/pool.go (99%) rename pkg/e621/builder/{ => basic}/pool_test.go (98%) rename pkg/e621/builder/{ => basic}/pools.go (99%) rename pkg/e621/builder/{ => basic}/pools_test.go (98%) rename pkg/e621/builder/{ => basic}/post.go (99%) rename pkg/e621/builder/{ => basic}/post_test.go (98%) rename pkg/e621/builder/{ => basic}/posts.go (99%) rename pkg/e621/builder/{ => basic}/posts_test.go (98%) rename pkg/e621/builder/{ => basic}/tag.go (99%) rename pkg/e621/builder/{ => basic}/tag_test.go (98%) rename pkg/e621/builder/{ => basic}/tags.go (99%) rename pkg/e621/builder/{ => basic}/user.go (99%) rename pkg/e621/builder/{ => basic}/user_test.go (98%) rename pkg/e621/builder/{ => basic}/users.go (99%) rename pkg/e621/builder/{ => basic}/users_test.go (98%) diff --git a/example/midlevel/dbexport.go b/example/midlevel/dbexport.go index 61f0222..db27f9c 100644 --- a/example/midlevel/dbexport.go +++ b/example/midlevel/dbexport.go @@ -1,7 +1,7 @@ package main import ( - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "log" "net/http" @@ -19,7 +19,7 @@ func main() { } log.Println("Getting a list of DB Exports: ") - getDBExportList := builder.NewGetDBExportListBuilder(requestContext) + getDBExportList := basic.NewGetDBExportListBuilder(requestContext) dbExportFiles, err := getDBExportList.Execute() if err != nil { panic(err) @@ -35,7 +35,7 @@ func main() { exportFileName := dbExportFiles[0] log.Println("Downloading DB export") log.Printf("File to download: %s", exportFileName) - getDBExportFile := builder.NewGetDBExportFileBuilder(requestContext) + getDBExportFile := basic.NewGetDBExportFileBuilder(requestContext) rawFile, err := getDBExportFile.SetFile(exportFileName).Execute() diff --git a/example/midlevel/favorite.go b/example/midlevel/favorite.go index 17d1fa3..5084e4e 100644 --- a/example/midlevel/favorite.go +++ b/example/midlevel/favorite.go @@ -1,7 +1,7 @@ package main import ( - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" @@ -23,7 +23,7 @@ func main() { log.Println("Getting favorites API user: ") // Call the GetFavorites function to retrieve favorite posts. - getFavorites := builder.NewGetFavoritesBuilder(requestContext) + getFavorites := basic.NewGetFavoritesBuilder(requestContext) posts, err := getFavorites.SetLimit(5).Execute() if err != nil { diff --git a/example/midlevel/note.go b/example/midlevel/note.go index a9fae34..c66847e 100644 --- a/example/midlevel/note.go +++ b/example/midlevel/note.go @@ -1,7 +1,7 @@ package main import ( - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" @@ -27,7 +27,7 @@ func main() { noteID := 36957 // Replace with the desired note's ID. // Call the GetNote function to retrieve the specified note. - getNote := builder.NewGetNoteBuilder(requestContext) + getNote := basic.NewGetNoteBuilder(requestContext) note, err := getNote.SetNoteID(noteID).Execute() if err != nil { @@ -42,7 +42,7 @@ func main() { log.Println("Getting a list of notes: ") // Call the GetNotes function to retrieve a list of notes based on the query parameters. - getNotes := builder.NewGetNotesBuilder(requestContext) + getNotes := basic.NewGetNotesBuilder(requestContext) notes, err := getNotes.SetLimit(5).Active(true).SearchInBody("furry").Execute() if err != nil { diff --git a/example/midlevel/pool.go b/example/midlevel/pool.go index d07345b..cc48b26 100644 --- a/example/midlevel/pool.go +++ b/example/midlevel/pool.go @@ -1,7 +1,7 @@ package main import ( - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" @@ -26,7 +26,7 @@ func main() { poolID := 36957 // Replace with the desired pool's ID. // Call the GetPool function to retrieve the specified pool. - getPool := builder.NewGetPoolBuilder(requestContext) + getPool := basic.NewGetPoolBuilder(requestContext) pool, err := getPool.ID(poolID).Execute() if err != nil { @@ -41,7 +41,7 @@ func main() { 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) + getPools := basic.NewGetPoolsBuilder(requestContext) pools, err := getPools.SetLimit(5).Active(true).SearchDescription("mass effect").Execute() if err != nil { diff --git a/example/midlevel/post.go b/example/midlevel/post.go index db2fc43..a89e620 100644 --- a/example/midlevel/post.go +++ b/example/midlevel/post.go @@ -1,7 +1,7 @@ package main import ( - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" @@ -26,7 +26,7 @@ func main() { var postID model.PostID = 4353480 // Replace with the desired post's ID. // Call the GetPost function to retrieve the specified post. - getPost := builder.NewGetPostBuilder(requestContext) + getPost := basic.NewGetPostBuilder(requestContext) post, err := getPost.SetPostID(postID).Execute() if err != nil { @@ -41,7 +41,7 @@ func main() { log.Println("Getting a list of posts: ") // Call the GetPosts function to retrieve a list of posts based on the query parameters. - getPosts := builder.NewGetPostsBuilder(requestContext) + getPosts := basic.NewGetPostsBuilder(requestContext) posts, err := getPosts.SetLimit(5).Execute() if err != nil { diff --git a/example/midlevel/tag.go b/example/midlevel/tag.go index 8afc3f1..e320aa4 100644 --- a/example/midlevel/tag.go +++ b/example/midlevel/tag.go @@ -1,7 +1,7 @@ package main import ( - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" @@ -26,7 +26,7 @@ func main() { tagID := 1530 // Replace with the desired tag's ID. // Call the GetTag function to retrieve the specified tag. - getTag := builder.NewGetTagBuilder(requestContext) + getTag := basic.NewGetTagBuilder(requestContext) tag, err := getTag.SetTagID(tagID).Execute() if err != nil { @@ -41,7 +41,7 @@ func main() { 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) + getTags := basic.NewGetTagsBuilder(requestContext) tagList, err := getTags.SetLimit(5).Artist(false).Execute() if err != nil { diff --git a/example/midlevel/user.go b/example/midlevel/user.go index 9528218..6fc56cf 100644 --- a/example/midlevel/user.go +++ b/example/midlevel/user.go @@ -1,7 +1,7 @@ package main import ( - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" @@ -26,7 +26,7 @@ func main() { username := "selloo" // Replace with the desired username. // Call the GetUser function to retrieve the specified user. - userBuilder := builder.NewGetUserBuilder(requestContext) + userBuilder := basic.NewGetUserBuilder(requestContext) user, err := userBuilder.SetUsername(username).Execute() if err != nil { @@ -41,7 +41,7 @@ func main() { log.Println("Getting a list of users: ") // Call the GetUsers function to retrieve a list of users based on the query parameters. - usersBuilder := builder.NewGetUsersBuilder(requestContext) + usersBuilder := basic.NewGetUsersBuilder(requestContext) userList, err := usersBuilder.SetLimit(5).Execute() if err != nil { diff --git a/pkg/e621/builder/db_export_file.go b/pkg/e621/builder/basic/db_export_file.go similarity index 99% rename from pkg/e621/builder/db_export_file.go rename to pkg/e621/builder/basic/db_export_file.go index 0746ced..c613d78 100644 --- a/pkg/e621/builder/db_export_file.go +++ b/pkg/e621/builder/basic/db_export_file.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" diff --git a/pkg/e621/builder/db_export_list.go b/pkg/e621/builder/basic/db_export_list.go similarity index 98% rename from pkg/e621/builder/db_export_list.go rename to pkg/e621/builder/basic/db_export_list.go index a660460..b172190 100644 --- a/pkg/e621/builder/db_export_list.go +++ b/pkg/e621/builder/basic/db_export_list.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" diff --git a/pkg/e621/builder/basic/favorite.go b/pkg/e621/builder/basic/favorite.go new file mode 100644 index 0000000..36bf737 --- /dev/null +++ b/pkg/e621/builder/basic/favorite.go @@ -0,0 +1,95 @@ +package basic + +import ( + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "strconv" +) + +// FavoritesBuilder represents a builder for constructing queries to retrieve user's favorite posts. +type FavoritesBuilder interface { + // SetUserID sets the user ID for the query. + SetUserID(userID model.UserID) FavoritesBuilder + // SetLimit sets the maximum number of favorite posts to retrieve. + SetLimit(limitFavorites int) FavoritesBuilder + // Page sets the page number for paginated results. + Page(pageNumber int) FavoritesBuilder + // Execute sends the constructed query and returns a slice of favorite posts and an error if any. + Execute() ([]model.Post, error) +} + +// NewGetFavoritesBuilder creates a new instance of FavoritesBuilder with the provided RequestContext. +// +// Parameters: +// - requestContext: The context for the API request, including the host, user agent, username, and API key. +// +// Returns: +// - FavoritesBuilder: An instance of the FavoritesBuilder interface. +func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder { + return &getFavorites{ + requestContext: requestContext, + query: make(map[string]string), + } +} + +// getFavorites is an implementation of the FavoritesBuilder interface. +type getFavorites struct { + query map[string]string + requestContext model.RequestContext +} + +// SetUserID sets the user ID for the query. +// +// Parameters: +// - userID: The ID of the user whose favorite posts are to be retrieved. +// +// Returns: +// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining. +func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder { + g.query["user_id"] = strconv.Itoa(int(userID)) + return g +} + +// SetLimit sets the maximum number of favorite posts to retrieve. +// +// Parameters: +// - limitFavorites: The maximum number of favorite posts to retrieve. +// +// Returns: +// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining. +func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder { + g.query["limit"] = strconv.Itoa(limitFavorites) + return g +} + +// Page sets the page number for paginated results. +// +// Parameters: +// - pageNumber: The page number for paginated results. +// +// Returns: +// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining. +func (g *getFavorites) Page(pageNumber int) FavoritesBuilder { + g.query["page"] = strconv.Itoa(pageNumber) + return g +} + +// Execute sends the constructed query and returns a slice of favorite posts and an error if any. +// +// Returns: +// - []model.Post: A slice of favorite posts. +// - error: An error, if any, encountered during the API request or response handling. +func (g *getFavorites) Execute() ([]model.Post, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } + favorites, err := endpoints.GetFavorites(g.requestContext, g.query) + if err != nil { + return nil, err + } + return favorites, nil +} diff --git a/pkg/e621/builder/favorite_test.go b/pkg/e621/builder/basic/favorite_test.go similarity index 98% rename from pkg/e621/builder/favorite_test.go rename to pkg/e621/builder/basic/favorite_test.go index f028643..aaca4aa 100644 --- a/pkg/e621/builder/favorite_test.go +++ b/pkg/e621/builder/basic/favorite_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/note.go b/pkg/e621/builder/basic/note.go similarity index 99% rename from pkg/e621/builder/note.go rename to pkg/e621/builder/basic/note.go index 5bb013e..fb253e4 100644 --- a/pkg/e621/builder/note.go +++ b/pkg/e621/builder/basic/note.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/note_test.go b/pkg/e621/builder/basic/note_test.go similarity index 98% rename from pkg/e621/builder/note_test.go rename to pkg/e621/builder/basic/note_test.go index e1de9b9..d876aad 100644 --- a/pkg/e621/builder/note_test.go +++ b/pkg/e621/builder/basic/note_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/notes.go b/pkg/e621/builder/basic/notes.go similarity index 99% rename from pkg/e621/builder/notes.go rename to pkg/e621/builder/basic/notes.go index 2df6476..f2e9a27 100644 --- a/pkg/e621/builder/notes.go +++ b/pkg/e621/builder/basic/notes.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/notes_test.go b/pkg/e621/builder/basic/notes_test.go similarity index 98% rename from pkg/e621/builder/notes_test.go rename to pkg/e621/builder/basic/notes_test.go index e7b61c0..c35d0ba 100644 --- a/pkg/e621/builder/notes_test.go +++ b/pkg/e621/builder/basic/notes_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/pool.go b/pkg/e621/builder/basic/pool.go similarity index 99% rename from pkg/e621/builder/pool.go rename to pkg/e621/builder/basic/pool.go index c66a210..0974da1 100644 --- a/pkg/e621/builder/pool.go +++ b/pkg/e621/builder/basic/pool.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/pool_test.go b/pkg/e621/builder/basic/pool_test.go similarity index 98% rename from pkg/e621/builder/pool_test.go rename to pkg/e621/builder/basic/pool_test.go index 157a424..a6dbc54 100644 --- a/pkg/e621/builder/pool_test.go +++ b/pkg/e621/builder/basic/pool_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/pools.go b/pkg/e621/builder/basic/pools.go similarity index 99% rename from pkg/e621/builder/pools.go rename to pkg/e621/builder/basic/pools.go index a146b10..bd3637b 100644 --- a/pkg/e621/builder/pools.go +++ b/pkg/e621/builder/basic/pools.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/pools_test.go b/pkg/e621/builder/basic/pools_test.go similarity index 98% rename from pkg/e621/builder/pools_test.go rename to pkg/e621/builder/basic/pools_test.go index 11ed09c..a84ae18 100644 --- a/pkg/e621/builder/pools_test.go +++ b/pkg/e621/builder/basic/pools_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/post.go b/pkg/e621/builder/basic/post.go similarity index 99% rename from pkg/e621/builder/post.go rename to pkg/e621/builder/basic/post.go index f8c16f7..5fcf086 100644 --- a/pkg/e621/builder/post.go +++ b/pkg/e621/builder/basic/post.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/post_test.go b/pkg/e621/builder/basic/post_test.go similarity index 98% rename from pkg/e621/builder/post_test.go rename to pkg/e621/builder/basic/post_test.go index c8f3340..7f9732e 100644 --- a/pkg/e621/builder/post_test.go +++ b/pkg/e621/builder/basic/post_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/posts.go b/pkg/e621/builder/basic/posts.go similarity index 99% rename from pkg/e621/builder/posts.go rename to pkg/e621/builder/basic/posts.go index 86787bc..209cd45 100644 --- a/pkg/e621/builder/posts.go +++ b/pkg/e621/builder/basic/posts.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/posts_test.go b/pkg/e621/builder/basic/posts_test.go similarity index 98% rename from pkg/e621/builder/posts_test.go rename to pkg/e621/builder/basic/posts_test.go index 8a912c0..d24c717 100644 --- a/pkg/e621/builder/posts_test.go +++ b/pkg/e621/builder/basic/posts_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/tag.go b/pkg/e621/builder/basic/tag.go similarity index 99% rename from pkg/e621/builder/tag.go rename to pkg/e621/builder/basic/tag.go index 402021b..5e41c2b 100644 --- a/pkg/e621/builder/tag.go +++ b/pkg/e621/builder/basic/tag.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/tag_test.go b/pkg/e621/builder/basic/tag_test.go similarity index 98% rename from pkg/e621/builder/tag_test.go rename to pkg/e621/builder/basic/tag_test.go index 49a3678..24b2f05 100644 --- a/pkg/e621/builder/tag_test.go +++ b/pkg/e621/builder/basic/tag_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/tags.go b/pkg/e621/builder/basic/tags.go similarity index 99% rename from pkg/e621/builder/tags.go rename to pkg/e621/builder/basic/tags.go index b61b617..55d0d53 100644 --- a/pkg/e621/builder/tags.go +++ b/pkg/e621/builder/basic/tags.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/user.go b/pkg/e621/builder/basic/user.go similarity index 99% rename from pkg/e621/builder/user.go rename to pkg/e621/builder/basic/user.go index 5b9e818..1211b3d 100644 --- a/pkg/e621/builder/user.go +++ b/pkg/e621/builder/basic/user.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/user_test.go b/pkg/e621/builder/basic/user_test.go similarity index 98% rename from pkg/e621/builder/user_test.go rename to pkg/e621/builder/basic/user_test.go index 1c0664d..345ac91 100644 --- a/pkg/e621/builder/user_test.go +++ b/pkg/e621/builder/basic/user_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/users.go b/pkg/e621/builder/basic/users.go similarity index 99% rename from pkg/e621/builder/users.go rename to pkg/e621/builder/basic/users.go index 41ce30b..54300cd 100644 --- a/pkg/e621/builder/users.go +++ b/pkg/e621/builder/basic/users.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "context" diff --git a/pkg/e621/builder/users_test.go b/pkg/e621/builder/basic/users_test.go similarity index 98% rename from pkg/e621/builder/users_test.go rename to pkg/e621/builder/basic/users_test.go index 4e84221..a1bd515 100644 --- a/pkg/e621/builder/users_test.go +++ b/pkg/e621/builder/basic/users_test.go @@ -1,4 +1,4 @@ -package builder +package basic import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" diff --git a/pkg/e621/builder/favorite.go b/pkg/e621/builder/favorite.go index 4240be1..6d9d38e 100644 --- a/pkg/e621/builder/favorite.go +++ b/pkg/e621/builder/favorite.go @@ -1,95 +1,130 @@ package builder import ( - "context" - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "fmt" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" - "strconv" + "strings" ) -// FavoritesBuilder represents a builder for constructing queries to retrieve user's favorite posts. type FavoritesBuilder interface { - // SetUserID sets the user ID for the query. - SetUserID(userID model.UserID) FavoritesBuilder - // SetLimit sets the maximum number of favorite posts to retrieve. - SetLimit(limitFavorites int) FavoritesBuilder - // Page sets the page number for paginated results. - Page(pageNumber int) FavoritesBuilder - // Execute sends the constructed query and returns a slice of favorite posts and an error if any. + FromUsername(username string) FavoritesBuilder + FromUserID(userID model.UserID) FavoritesBuilder + WithTags(tags []string) FavoritesBuilder + Limit(amount int) FavoritesBuilder Execute() ([]model.Post, error) } -// NewGetFavoritesBuilder creates a new instance of FavoritesBuilder with the provided RequestContext. -// -// Parameters: -// - requestContext: The context for the API request, including the host, user agent, username, and API key. -// -// Returns: -// - FavoritesBuilder: An instance of the FavoritesBuilder interface. func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder { return &getFavorites{ requestContext: requestContext, - query: make(map[string]string), } } -// getFavorites is an implementation of the FavoritesBuilder interface. type getFavorites struct { - query map[string]string requestContext model.RequestContext + user model.User + tags string + limit int + error error } -// SetUserID sets the user ID for the query. -// -// Parameters: -// - userID: The ID of the user whose favorite posts are to be retrieved. -// -// Returns: -// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining. -func (g *getFavorites) SetUserID(userID model.UserID) FavoritesBuilder { - g.query["user_id"] = strconv.Itoa(int(userID)) +func (g *getFavorites) FromUsername(username string) FavoritesBuilder { + user := model.User{Name: username} + g.user = user return g } -// SetLimit sets the maximum number of favorite posts to retrieve. -// -// Parameters: -// - limitFavorites: The maximum number of favorite posts to retrieve. -// -// Returns: -// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining. -func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder { - g.query["limit"] = strconv.Itoa(limitFavorites) +func (g *getFavorites) FromUserID(userID model.UserID) FavoritesBuilder { + user := model.User{ID: userID} + g.user = user return g } -// Page sets the page number for paginated results. -// -// Parameters: -// - pageNumber: The page number for paginated results. -// -// Returns: -// - FavoritesBuilder: The instance of the FavoritesBuilder for method chaining. -func (g *getFavorites) Page(pageNumber int) FavoritesBuilder { - g.query["page"] = strconv.Itoa(pageNumber) +func (g *getFavorites) WithTags(tagList []string) FavoritesBuilder { + tags := strings.Join(tagList, ",") + g.tags = tags return g } -// Execute sends the constructed query and returns a slice of favorite posts and an error if any. -// -// Returns: -// - []model.Post: A slice of favorite posts. -// - error: An error, if any, encountered during the API request or response handling. -func (g *getFavorites) Execute() ([]model.Post, error) { - if g.requestContext.RateLimiter != nil { - err := g.requestContext.RateLimiter.Wait(context.Background()) +func (g *getFavorites) Limit(amount int) FavoritesBuilder { + g.limit = amount + return g +} + +func (g getFavorites) Execute() ([]model.Post, error) { + var err error + basicFavoritesBuilder := basic.NewGetFavoritesBuilder(g.requestContext) + var userFavorites []model.Post + + if g.user.ID == 0 && g.user.Name == "" { + return nil, fmt.Errorf("no UserID or UserName given") + } + + if g.user.Name != "" { + g.user, err = basic.NewGetUserBuilder(g.requestContext).SetUsername(g.user.Name).Execute() if err != nil { return nil, err } } - favorites, err := endpoints.GetFavorites(g.requestContext, g.query) - if err != nil { - return nil, err + + if g.user.ID != 0 { + basicFavoritesBuilder.SetUserID(g.user.ID) } + + if g.limit != 0 { + basicFavoritesBuilder.SetLimit(g.limit) + } + + if g.tags == "" { + userFavorites, err = basicFavoritesBuilder.Execute() + } + + if g.tags != "" { + // TODO: implement with tags (channels) + } + + return userFavorites, err + +} + +/* +// GetNFavoritesForUser retrieves a specified number of favorites for a user. +func (c *CLient) GetNFavoritesForUser(n int, favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) { + // Retrieves a specified number of favorite posts for a user. + if n < utils.E621_MAX_POST_COUNT { + favoriteBuilder.SetLimit(n) + } + + var favorites []model.Post + var page = 1 + for len(favorites) < n { + favoriteBuilder.Page(page) + favoriteBuilder.SetLimit(n - len(favorites)) + newFavorites, err := favoriteBuilder.Execute() + if err != nil { + return nil, err + } + if len(newFavorites) == 0 { + break + } + favorites = append(favorites, newFavorites...) + page = page + 1 + } + return favorites, nil } + +// GetAllFavoritesForUser retrieves all favorites for a user. +func (c *Client) GetAllFavoritesForUser(favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) { + // Retrieves all favorite posts for a user. + return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder) +} + +// GetFavoritesForUserWithTags returns a posts builder for a user's favorites with specific tags. +func (c *Client) GetFavoritesForUserWithTags(username string, tags string) basic.PostsBuilder { + // Returns a posts builder for a user's favorites with specific tags. + favoritesBuilder := basic.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags)) + return favoritesBuilder +} +*/ diff --git a/pkg/e621/client.go b/pkg/e621/client.go index cf4b706..0660cd8 100644 --- a/pkg/e621/client.go +++ b/pkg/e621/client.go @@ -5,7 +5,7 @@ import ( "compress/gzip" "encoding/csv" "fmt" - "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder/basic" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "github.com/gocarina/gocsv" @@ -44,88 +44,88 @@ func NewClient(username string, apiKey string) Client { // // Returns: // - builder.UserBuilder: An instance of the UserBuilder. -func (c *Client) GetUserBuilder() builder.UserBuilder { - return builder.NewGetUserBuilder(c.RequestContext) +func (c *Client) GetUserBuilder() basic.UserBuilder { + return basic.NewGetUserBuilder(c.RequestContext) } // GetUsersBuilder returns a UsersBuilder instance for creating and executing requests to retrieve multiple users' information from the e621 API. // // Returns: // - builder.UsersBuilder: An instance of the UsersBuilder. -func (c *Client) GetUsersBuilder() builder.UsersBuilder { - return builder.NewGetUsersBuilder(c.RequestContext) +func (c *Client) GetUsersBuilder() basic.UsersBuilder { + return basic.NewGetUsersBuilder(c.RequestContext) } // GetFavoritesBuilder returns a FavoritesBuilder instance for creating and executing requests to retrieve a user's favorite posts from the e621 API. // // Returns: // - builder.FavoritesBuilder: An instance of the FavoritesBuilder. -func (c *Client) GetFavoritesBuilder() builder.FavoritesBuilder { - return builder.NewGetFavoritesBuilder(c.RequestContext) +func (c *Client) GetFavoritesBuilder() basic.FavoritesBuilder { + return basic.NewGetFavoritesBuilder(c.RequestContext) } // GetPostBuilder returns a PostBuilder instance for creating and executing requests to retrieve post information from the e621 API. // // Returns: // - builder.PostBuilder: An instance of the PostBuilder. -func (c *Client) GetPostBuilder() builder.PostBuilder { - return builder.NewGetPostBuilder(c.RequestContext) +func (c *Client) GetPostBuilder() basic.PostBuilder { + return basic.NewGetPostBuilder(c.RequestContext) } // GetPostsBuilder returns a PostsBuilder instance for creating and executing requests to retrieve multiple posts' information from the e621 API. // // Returns: // - builder.PostsBuilder: An instance of the PostsBuilder. -func (c *Client) GetPostsBuilder() builder.PostsBuilder { - return builder.NewGetPostsBuilder(c.RequestContext) +func (c *Client) GetPostsBuilder() basic.PostsBuilder { + return basic.NewGetPostsBuilder(c.RequestContext) } // GetNoteBuilder returns a NoteBuilder instance for creating and executing requests to retrieve note information from the e621 API. // // Returns: // - builder.NoteBuilder: An instance of the NoteBuilder. -func (c *Client) GetNoteBuilder() builder.NoteBuilder { - return builder.NewGetNoteBuilder(c.RequestContext) +func (c *Client) GetNoteBuilder() basic.NoteBuilder { + return basic.NewGetNoteBuilder(c.RequestContext) } // GetNotesBuilder returns a NotesBuilder instance for creating and executing requests to retrieve multiple notes' information from the e621 API. // // Returns: // - builder.NotesBuilder: An instance of the NotesBuilder. -func (c *Client) GetNotesBuilder() builder.NotesBuilder { - return builder.NewGetNotesBuilder(c.RequestContext) +func (c *Client) GetNotesBuilder() basic.NotesBuilder { + return basic.NewGetNotesBuilder(c.RequestContext) } // GetPoolBuilder returns a PoolBuilder instance for creating and executing requests to retrieve pool information from the e621 API. // // Returns: // - builder.PoolBuilder: An instance of the PoolBuilder. -func (c *Client) GetPoolBuilder() builder.PoolBuilder { - return builder.NewGetPoolBuilder(c.RequestContext) +func (c *Client) GetPoolBuilder() basic.PoolBuilder { + return basic.NewGetPoolBuilder(c.RequestContext) } // GetPoolsBuilder returns a PoolsBuilder instance for creating and executing requests to retrieve multiple pools' information from the e621 API. // // Returns: // - builder.PoolsBuilder: An instance of the PoolsBuilder. -func (c *Client) GetPoolsBuilder() builder.PoolsBuilder { - return builder.NewGetPoolsBuilder(c.RequestContext) +func (c *Client) GetPoolsBuilder() basic.PoolsBuilder { + return basic.NewGetPoolsBuilder(c.RequestContext) } // GetTagBuilder returns a TagBuilder instance for creating and executing requests to retrieve tag information from the e621 API. // // Returns: // - builder.TagBuilder: An instance of the TagBuilder. -func (c *Client) GetTagBuilder() builder.TagBuilder { - return builder.NewGetTagBuilder(c.RequestContext) +func (c *Client) GetTagBuilder() basic.TagBuilder { + return basic.NewGetTagBuilder(c.RequestContext) } // GetTagsBuilder returns a TagsBuilder instance for creating and executing requests to retrieve multiple tags' information from the e621 API. // // Returns: // - builder.TagsBuilder: An instance of the TagsBuilder. -func (c *Client) GetTagsBuilder() builder.TagsBuilder { - return builder.NewGetTagsBuilder(c.RequestContext) +func (c *Client) GetTagsBuilder() basic.TagsBuilder { + return basic.NewGetTagsBuilder(c.RequestContext) } // SetHost sets the API host for the client. @@ -143,30 +143,30 @@ func (c *Client) SetAgentName(userAgent string) *Client { } // GetUserByName returns a user builder for a given username. -func (c *Client) GetUserByName(username string) builder.UserBuilder { +func (c *Client) GetUserByName(username string) basic.UserBuilder { // Returns a user builder for the specified username. - return builder.NewGetUserBuilder(c.RequestContext).SetUsername(username) + return basic.NewGetUserBuilder(c.RequestContext).SetUsername(username) } // GetUserByID returns a user builder for a given user ID. -func (c *Client) GetUserByID(userID model.UserID) builder.UserBuilder { +func (c *Client) GetUserByID(userID model.UserID) basic.UserBuilder { // Returns a user builder for the specified user ID. - return builder.NewGetUserBuilder(c.RequestContext).SetUsername(strconv.FormatInt(int64(userID), 10)) + return basic.NewGetUserBuilder(c.RequestContext).SetUsername(strconv.FormatInt(int64(userID), 10)) } // GetFavoritesForUser returns a favorites builder for a given username. -func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder, error) { +func (c *Client) GetFavoritesForUser(username string) (basic.FavoritesBuilder, error) { // Returns a favorites builder for the specified username. - user, err := builder.NewGetUserBuilder(c.RequestContext).SetUsername(username).Execute() + user, err := basic.NewGetUserBuilder(c.RequestContext).SetUsername(username).Execute() if err != nil { return nil, err } - favoritesBuilder := builder.NewGetFavoritesBuilder(c.RequestContext).SetUserID(user.ID) + favoritesBuilder := basic.NewGetFavoritesBuilder(c.RequestContext).SetUserID(user.ID) return favoritesBuilder, nil } // GetNFavoritesForUser retrieves a specified number of favorites for a user. -func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) { +func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) { // Retrieves a specified number of favorite posts for a user. if n < utils.E621_MAX_POST_COUNT { favoriteBuilder.SetLimit(n) @@ -192,32 +192,32 @@ func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBu } // GetAllFavoritesForUser retrieves all favorites for a user. -func (c *Client) GetAllFavoritesForUser(favoriteBuilder builder.FavoritesBuilder) ([]model.Post, error) { +func (c *Client) GetAllFavoritesForUser(favoriteBuilder basic.FavoritesBuilder) ([]model.Post, error) { // Retrieves all favorite posts for a user. return c.GetNFavoritesForUser(math.MaxInt, favoriteBuilder) } // GetFavoritesForUserWithTags returns a posts builder for a user's favorites with specific tags. -func (c *Client) GetFavoritesForUserWithTags(username string, tags string) builder.PostsBuilder { +func (c *Client) GetFavoritesForUserWithTags(username string, tags string) basic.PostsBuilder { // Returns a posts builder for a user's favorites with specific tags. - favoritesBuilder := builder.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags)) + favoritesBuilder := basic.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags)) return favoritesBuilder } // GetPostByID returns a post builder for a specific post ID. -func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder { +func (c *Client) GetPostByID(id model.PostID) basic.PostBuilder { // Returns a post builder for a specific post ID. - return builder.NewGetPostBuilder(c.RequestContext).SetPostID(id) + return basic.NewGetPostBuilder(c.RequestContext).SetPostID(id) } // GetPosts returns a posts builder for general post queries. -func (c *Client) GetPosts() builder.PostsBuilder { +func (c *Client) GetPosts() basic.PostsBuilder { // Returns a posts builder for general post queries. - return builder.NewGetPostsBuilder(c.RequestContext) + return basic.NewGetPostsBuilder(c.RequestContext) } // GetNPosts retrieves a specified number of posts. -func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Post, error) { +func (c *Client) GetNPosts(n int, postBuilder basic.PostsBuilder) ([]model.Post, error) { // Retrieves a specified number of posts using the provided post builder. if n < utils.E621_MAX_POST_COUNT { postBuilder.SetLimit(n) @@ -246,18 +246,18 @@ func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Pos } // GetAllPosts retrieves all available posts using the provided post builder. -func (c *Client) GetAllPosts(postBuilder builder.PostsBuilder) ([]model.Post, error) { +func (c *Client) GetAllPosts(postBuilder basic.PostsBuilder) ([]model.Post, error) { // Retrieves all available posts using the provided post builder. return c.GetNPosts(math.MaxInt, postBuilder) } func (c *Client) GetLatestPoolsDBExportDataAsBytes() (string, []byte, error) { - dbExportFileNameList, err := builder.NewGetDBExportListBuilder(c.RequestContext).Execute() + dbExportFileNameList, err := basic.NewGetDBExportListBuilder(c.RequestContext).Execute() if err != nil { return "", nil, err } - getDBExportFile := builder.NewGetDBExportFileBuilder(c.RequestContext) + getDBExportFile := basic.NewGetDBExportFileBuilder(c.RequestContext) filter := func(s string) bool { return strings.HasPrefix(s, "pools") } filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter) @@ -323,12 +323,12 @@ func (c *Client) GetLatestPoolsDBExportDataAsStruct() ([]*model.Pool, error) { func (c *Client) GetLatestPostsDBExportDataAsBytes() (string, []byte, error) { log.Println("Please wait while the download is in progress for the post export... (file over 1GB) ") - dbExportFileNameList, err := builder.NewGetDBExportListBuilder(c.RequestContext).Execute() + dbExportFileNameList, err := basic.NewGetDBExportListBuilder(c.RequestContext).Execute() if err != nil { return "", nil, err } - getDBExportFile := builder.NewGetDBExportFileBuilder(c.RequestContext) + getDBExportFile := basic.NewGetDBExportFileBuilder(c.RequestContext) filter := func(s string) bool { return strings.HasPrefix(s, "posts") } filteredFileNameList := utils.SliceFilter(dbExportFileNameList, filter)