From 5f1cba1b716c7f7e4f5ed6edce3fc84340f580e9 Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 24 Oct 2023 15:10:39 +0200 Subject: [PATCH] feat: added first implementations of the high level api, reworked some stuff & more Signed-off-by: SoXX --- README.md | 7 +++ example/lowlevel/favorite.go | 4 +- example/lowlevel/note.go | 4 +- example/lowlevel/pool.go | 4 +- example/lowlevel/post.go | 4 +- example/lowlevel/tag.go | 4 +- example/lowlevel/user.go | 4 +- example/midlevel/favorite.go | 4 +- example/midlevel/note.go | 4 +- example/midlevel/pool.go | 4 +- example/midlevel/post.go | 6 +-- example/midlevel/tag.go | 4 +- example/midlevel/user.go | 4 +- go.mod | 7 ++- go.sum | 2 + internal/client.go | 1 - main.go | 5 +- pkg/e621/builder/favorite.go | 17 +++++-- pkg/e621/builder/favorite_test.go | 2 +- pkg/e621/builder/note.go | 11 ++++- pkg/e621/builder/note_test.go | 2 +- pkg/e621/builder/notes.go | 11 ++++- pkg/e621/builder/notes_test.go | 2 +- pkg/e621/builder/pool.go | 11 ++++- pkg/e621/builder/pool_test.go | 2 +- pkg/e621/builder/pools.go | 11 ++++- pkg/e621/builder/pools_test.go | 2 +- pkg/e621/builder/post.go | 19 ++++--- pkg/e621/builder/post_test.go | 2 +- pkg/e621/builder/posts.go | 11 ++++- pkg/e621/builder/posts_test.go | 2 +- pkg/e621/builder/tag.go | 11 ++++- pkg/e621/builder/tag_test.go | 2 +- pkg/e621/builder/tags.go | 11 ++++- pkg/e621/builder/user.go | 19 ++++--- pkg/e621/builder/user_test.go | 2 +- pkg/e621/builder/users.go | 65 +++++++++++++++++++++++- pkg/e621/builder/users_test.go | 2 +- pkg/e621/endpoints/favorite.go | 4 +- pkg/e621/endpoints/favorite_test.go | 2 +- pkg/e621/endpoints/note.go | 4 +- pkg/e621/endpoints/note_test.go | 2 +- pkg/e621/endpoints/pool.go | 4 +- pkg/e621/endpoints/pool_test.go | 2 +- pkg/e621/endpoints/post.go | 4 +- pkg/e621/endpoints/post_test.go | 2 +- pkg/e621/endpoints/tag.go | 4 +- pkg/e621/endpoints/tag_test.go | 2 +- pkg/e621/endpoints/user.go | 4 +- pkg/e621/endpoints/user_test.go | 2 +- pkg/e621/model/basic.go | 16 +++--- pkg/e621/model/pool.go | 11 +++-- pkg/e621/model/post.go | 4 +- pkg/e621/model/user.go | 71 ++++++++++++++++++--------- {internal => pkg/e621}/utils/error.go | 0 55 files changed, 303 insertions(+), 124 deletions(-) delete mode 100644 internal/client.go rename {internal => pkg/e621}/utils/error.go (100%) diff --git a/README.md b/README.md index 8cda725..f67d1fa 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,13 @@ An unofficial e621 SDK (Client) library to interact with **e621**, **e923** and **e6ai**. Maintained by the Anthrove Development Team. +- Caching? +- Auto pagination + - sync + - async +- connected/inelegant calls +- more docs + # Completeness _Legend_ diff --git a/example/lowlevel/favorite.go b/example/lowlevel/favorite.go index 0739f67..6a37b1a 100644 --- a/example/lowlevel/favorite.go +++ b/example/lowlevel/favorite.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/lowlevel/note.go b/example/lowlevel/note.go index e1cd333..6f1f2bb 100644 --- a/example/lowlevel/note.go +++ b/example/lowlevel/note.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/lowlevel/pool.go b/example/lowlevel/pool.go index da7db07..291d984 100644 --- a/example/lowlevel/pool.go +++ b/example/lowlevel/pool.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/lowlevel/post.go b/example/lowlevel/post.go index b8dfdfb..3f6bff7 100644 --- a/example/lowlevel/post.go +++ b/example/lowlevel/post.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/lowlevel/tag.go b/example/lowlevel/tag.go index 36110de..fcac66b 100644 --- a/example/lowlevel/tag.go +++ b/example/lowlevel/tag.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/lowlevel/user.go b/example/lowlevel/user.go index a7491b4..ed19bbb 100644 --- a/example/lowlevel/user.go +++ b/example/lowlevel/user.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/midlevel/favorite.go b/example/midlevel/favorite.go index 879f2f7..17d1fa3 100644 --- a/example/midlevel/favorite.go +++ b/example/midlevel/favorite.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/midlevel/note.go b/example/midlevel/note.go index 5e71803..a9fae34 100644 --- a/example/midlevel/note.go +++ b/example/midlevel/note.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/midlevel/pool.go b/example/midlevel/pool.go index 0ca5cb6..77c678a 100644 --- a/example/midlevel/pool.go +++ b/example/midlevel/pool.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/midlevel/post.go b/example/midlevel/post.go index a72dd28..db2fc43 100644 --- a/example/midlevel/post.go +++ b/example/midlevel/post.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" @@ -23,7 +23,7 @@ func main() { log.Println("Getting single post: ") // Specify the post ID for retrieval. - postID := 4353480 // Replace with the desired post's ID. + 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) diff --git a/example/midlevel/tag.go b/example/midlevel/tag.go index cdfee84..8afc3f1 100644 --- a/example/midlevel/tag.go +++ b/example/midlevel/tag.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/example/midlevel/user.go b/example/midlevel/user.go index a492006..9528218 100644 --- a/example/midlevel/user.go +++ b/example/midlevel/user.go @@ -1,8 +1,8 @@ package main import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" _ "github.com/joho/godotenv/autoload" "log" "net/http" diff --git a/go.mod b/go.mod index 7c5c99d..074481c 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module git.dragse.it/anthrove/e621-to-graph +module git.dragse.it/anthrove/e621-sdk-go go 1.21.3 @@ -6,3 +6,8 @@ require ( github.com/jarcoal/httpmock v1.3.1 github.com/joho/godotenv v1.5.1 ) + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + golang.org/x/time v0.3.0 // indirect +) diff --git a/go.sum b/go.sum index d380c0b..ce9b242 100644 --- a/go.sum +++ b/go.sum @@ -6,3 +6,5 @@ github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/maxatome/go-testdeep v1.12.0 h1:Ql7Go8Tg0C1D/uMMX59LAoYK7LffeJQ6X2T04nTH68g= github.com/maxatome/go-testdeep v1.12.0/go.mod h1:lPZc/HAcJMP92l7yI6TRz1aZN5URwUBUAfUNvrclaNM= +golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4= +golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/internal/client.go b/internal/client.go deleted file mode 100644 index 5bf0569..0000000 --- a/internal/client.go +++ /dev/null @@ -1 +0,0 @@ -package internal diff --git a/main.go b/main.go index 4ea84c9..da29a2c 100644 --- a/main.go +++ b/main.go @@ -1 +1,4 @@ -package e621_sdk_go +package main + +func main() { +} diff --git a/pkg/e621/builder/favorite.go b/pkg/e621/builder/favorite.go index 9500d5f..5802608 100644 --- a/pkg/e621/builder/favorite.go +++ b/pkg/e621/builder/favorite.go @@ -1,13 +1,14 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) type FavoritesBuilder interface { - SetUserID(userID int) FavoritesBuilder + SetUserID(userID model.UserID) FavoritesBuilder SetLimit(limitFavorites int) FavoritesBuilder Execute() ([]model.Post, error) } @@ -24,8 +25,8 @@ type getFavorites struct { requestContext model.RequestContext } -func (g getFavorites) SetUserID(userID int) FavoritesBuilder { - g.query["user_id"] = strconv.Itoa(userID) +func (g getFavorites) SetUserID(userID model.UserID) FavoritesBuilder { + g.query["user_id"] = strconv.Itoa(int(userID)) return g } @@ -35,6 +36,12 @@ func (g getFavorites) SetLimit(limitFavorites int) FavoritesBuilder { } 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 { diff --git a/pkg/e621/builder/favorite_test.go b/pkg/e621/builder/favorite_test.go index f56b897..1c2609f 100644 --- a/pkg/e621/builder/favorite_test.go +++ b/pkg/e621/builder/favorite_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/note.go b/pkg/e621/builder/note.go index 090c056..3c5bcc6 100644 --- a/pkg/e621/builder/note.go +++ b/pkg/e621/builder/note.go @@ -1,8 +1,9 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) @@ -27,6 +28,12 @@ func (g *getNote) SetNoteID(noteID int) NoteBuilder { } func (g *getNote) Execute() (*model.Note, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } note, err := endpoints.GetNote(g.requestContext, strconv.Itoa(g.noteID)) if err != nil { diff --git a/pkg/e621/builder/note_test.go b/pkg/e621/builder/note_test.go index 56cc98a..3040b21 100644 --- a/pkg/e621/builder/note_test.go +++ b/pkg/e621/builder/note_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/notes.go b/pkg/e621/builder/notes.go index 2d55dee..dd00185 100644 --- a/pkg/e621/builder/notes.go +++ b/pkg/e621/builder/notes.go @@ -1,8 +1,9 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) @@ -65,6 +66,12 @@ func (g getNotes) SetLimit(limitNotes int) NotesBuilder { } func (g getNotes) Execute() ([]model.Note, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } notes, err := endpoints.GetNotes(g.requestContext, g.query) if err != nil { diff --git a/pkg/e621/builder/notes_test.go b/pkg/e621/builder/notes_test.go index fc816e3..5e649be 100644 --- a/pkg/e621/builder/notes_test.go +++ b/pkg/e621/builder/notes_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/pool.go b/pkg/e621/builder/pool.go index b8a1e53..1559b9b 100644 --- a/pkg/e621/builder/pool.go +++ b/pkg/e621/builder/pool.go @@ -1,8 +1,9 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) @@ -26,6 +27,12 @@ func (g getPool) ID(poolID int) PoolBuilder { } func (g getPool) Execute() (model.Pool, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return model.Pool{}, err + } + } pool, err := endpoints.GetPool(g.requestContext, strconv.Itoa(g.id)) if err != nil { diff --git a/pkg/e621/builder/pool_test.go b/pkg/e621/builder/pool_test.go index 9ae1ff4..2b2eff6 100644 --- a/pkg/e621/builder/pool_test.go +++ b/pkg/e621/builder/pool_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/pools.go b/pkg/e621/builder/pools.go index ab8ff24..df52598 100644 --- a/pkg/e621/builder/pools.go +++ b/pkg/e621/builder/pools.go @@ -1,8 +1,9 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) @@ -77,6 +78,12 @@ func (g *getPools) SetLimit(limitUser int) PoolsBuilder { } func (g *getPools) Execute() ([]model.Pool, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } pools, err := endpoints.GetPools(g.requestContext, g.query) if err != nil { diff --git a/pkg/e621/builder/pools_test.go b/pkg/e621/builder/pools_test.go index 18ef709..4a971e9 100644 --- a/pkg/e621/builder/pools_test.go +++ b/pkg/e621/builder/pools_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/post.go b/pkg/e621/builder/post.go index 4213427..4d63952 100644 --- a/pkg/e621/builder/post.go +++ b/pkg/e621/builder/post.go @@ -1,13 +1,14 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) type PostBuilder interface { - SetPostID(postID int) PostBuilder + SetPostID(postID model.PostID) PostBuilder Execute() (*model.Post, error) } @@ -17,16 +18,22 @@ func NewGetPostBuilder(requestContext model.RequestContext) PostBuilder { type getPost struct { requestContext model.RequestContext - postID int + postID model.PostID } -func (g *getPost) SetPostID(postID int) PostBuilder { +func (g *getPost) SetPostID(postID model.PostID) PostBuilder { g.postID = postID return g } func (g *getPost) Execute() (*model.Post, error) { - post, err := endpoints.GetPost(g.requestContext, strconv.Itoa(g.postID)) + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } + post, err := endpoints.GetPost(g.requestContext, strconv.Itoa(int(g.postID))) if err != nil { return nil, err diff --git a/pkg/e621/builder/post_test.go b/pkg/e621/builder/post_test.go index d4b7a1a..2fbc770 100644 --- a/pkg/e621/builder/post_test.go +++ b/pkg/e621/builder/post_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/posts.go b/pkg/e621/builder/posts.go index b52a3cd..92ab64a 100644 --- a/pkg/e621/builder/posts.go +++ b/pkg/e621/builder/posts.go @@ -1,8 +1,9 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) @@ -47,6 +48,12 @@ func (g *getPosts) SetLimit(limitUser int) PostsBuilder { } func (g *getPosts) Execute() ([]model.Post, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } posts, err := endpoints.GetPosts(g.requestContext, g.query) if err != nil { diff --git a/pkg/e621/builder/posts_test.go b/pkg/e621/builder/posts_test.go index 1337b4c..83ea370 100644 --- a/pkg/e621/builder/posts_test.go +++ b/pkg/e621/builder/posts_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/tag.go b/pkg/e621/builder/tag.go index 055a17d..1a035c4 100644 --- a/pkg/e621/builder/tag.go +++ b/pkg/e621/builder/tag.go @@ -1,8 +1,9 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) @@ -26,6 +27,12 @@ func (g *getTag) SetTagID(tagID int) TagBuilder { } func (g *getTag) Execute() (model.Tag, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return model.Tag{}, err + } + } tag, err := endpoints.GetTag(g.requestContext, strconv.Itoa(g.tagID)) if err != nil { diff --git a/pkg/e621/builder/tag_test.go b/pkg/e621/builder/tag_test.go index 532642e..b5bc3b6 100644 --- a/pkg/e621/builder/tag_test.go +++ b/pkg/e621/builder/tag_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/tags.go b/pkg/e621/builder/tags.go index 1a78e39..c24f2b6 100644 --- a/pkg/e621/builder/tags.go +++ b/pkg/e621/builder/tags.go @@ -1,8 +1,9 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) @@ -68,6 +69,12 @@ func (g *getTags) SetLimit(limitUser int) TagsBuilder { } func (g *getTags) Execute() ([]model.Tag, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } tags, err := endpoints.GetTags(g.requestContext, g.query) if err != nil { diff --git a/pkg/e621/builder/user.go b/pkg/e621/builder/user.go index ba92bd3..ee0f1af 100644 --- a/pkg/e621/builder/user.go +++ b/pkg/e621/builder/user.go @@ -1,13 +1,14 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" ) type UserBuilder interface { SetUsername(username string) UserBuilder - Execute() (*model.User, error) + Execute() (model.User, error) } func NewGetUserBuilder(requestContext model.RequestContext) UserBuilder { @@ -24,11 +25,17 @@ func (g *getUser) SetUsername(username string) UserBuilder { return g } -func (g *getUser) Execute() (*model.User, error) { +func (g *getUser) Execute() (model.User, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return model.User{}, err + } + } user, err := endpoints.GetUser(g.requestContext, g.username) if err != nil { - return nil, err + return model.User{}, err } - return &user, nil + return user, nil } diff --git a/pkg/e621/builder/user_test.go b/pkg/e621/builder/user_test.go index 6a36ca0..59d67f7 100644 --- a/pkg/e621/builder/user_test.go +++ b/pkg/e621/builder/user_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/builder/users.go b/pkg/e621/builder/users.go index c083801..371c9ef 100644 --- a/pkg/e621/builder/users.go +++ b/pkg/e621/builder/users.go @@ -1,14 +1,24 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "context" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "strconv" ) type UsersBuilder interface { SetPage(pageNumber int) UsersBuilder SetLimit(limitUser int) UsersBuilder + SearchByName(userName string) UsersBuilder + SearchByAbout(about string) UsersBuilder + SearchByAvatarID(postID model.PostID) UsersBuilder + SearchByLevel(level model.UserLevel) UsersBuilder + SearchByMinLevel(level model.UserLevel) UsersBuilder + SearchByMaxLevellevel(model.UserLevel) UsersBuilder + searchByCanUpload(canUpload bool) UsersBuilder + SearchByIsApprover(isApprover bool) UsersBuilder + SearchByOrder(order model.Order) UsersBuilder Execute() ([]model.User, error) } @@ -21,6 +31,51 @@ type getUsers struct { query map[string]string } +func (g *getUsers) SearchByName(userName string) UsersBuilder { + g.query["search[name_matches]"] = userName + return g +} + +func (g *getUsers) SearchByAbout(about string) UsersBuilder { + g.query["search[about_me"] = about + return g +} + +func (g *getUsers) SearchByAvatarID(postID model.PostID) UsersBuilder { + g.query["search[avatar_id]"] = strconv.FormatInt(int64(postID), 10) + return g +} + +func (g *getUsers) SearchByLevel(level model.UserLevel) UsersBuilder { + g.query["search[level]"] = strconv.Itoa(int(level)) + return g +} + +func (g *getUsers) SearchByMinLevel(level model.UserLevel) UsersBuilder { + g.query["search[min_level"] = strconv.Itoa(int(level)) + return g +} + +func (g *getUsers) SearchByMaxLevellevel(level model.UserLevel) UsersBuilder { + g.query["search[max_level]"] = strconv.Itoa(int(level)) + return g +} + +func (g *getUsers) searchByCanUpload(canUpload bool) UsersBuilder { + g.query["search[can_upload_free]"] = strconv.FormatBool(canUpload) + return g +} + +func (g *getUsers) SearchByIsApprover(isApprover bool) UsersBuilder { + g.query["search[can_approve_posts]"] = strconv.FormatBool(isApprover) + return g +} + +func (g *getUsers) SearchByOrder(order model.Order) UsersBuilder { + g.query["search[order]"] = string(order) + return g +} + func (g *getUsers) SetPage(pageNumber int) UsersBuilder { g.query["page"] = strconv.Itoa(pageNumber) return g @@ -32,6 +87,12 @@ func (g *getUsers) SetLimit(limitUser int) UsersBuilder { } func (g *getUsers) Execute() ([]model.User, error) { + if g.requestContext.RateLimiter != nil { + err := g.requestContext.RateLimiter.Wait(context.Background()) + if err != nil { + return nil, err + } + } users, err := endpoints.GetUsers(g.requestContext, g.query) if err != nil { diff --git a/pkg/e621/builder/users_test.go b/pkg/e621/builder/users_test.go index 40b0621..a0ea4a8 100644 --- a/pkg/e621/builder/users_test.go +++ b/pkg/e621/builder/users_test.go @@ -1,7 +1,7 @@ package builder import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/endpoints/favorite.go b/pkg/e621/endpoints/favorite.go index a3dcdb4..6990a66 100644 --- a/pkg/e621/endpoints/favorite.go +++ b/pkg/e621/endpoints/favorite.go @@ -3,8 +3,8 @@ package endpoints import ( "encoding/json" "fmt" - "git.dragse.it/anthrove/e621-to-graph/internal/utils" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "log" "net/http" ) diff --git a/pkg/e621/endpoints/favorite_test.go b/pkg/e621/endpoints/favorite_test.go index 714d718..2b1a046 100644 --- a/pkg/e621/endpoints/favorite_test.go +++ b/pkg/e621/endpoints/favorite_test.go @@ -1,7 +1,7 @@ package endpoints import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/endpoints/note.go b/pkg/e621/endpoints/note.go index b30ba2e..0727ce1 100644 --- a/pkg/e621/endpoints/note.go +++ b/pkg/e621/endpoints/note.go @@ -3,8 +3,8 @@ package endpoints import ( "encoding/json" "fmt" - "git.dragse.it/anthrove/e621-to-graph/internal/utils" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "io" "log" "net/http" diff --git a/pkg/e621/endpoints/note_test.go b/pkg/e621/endpoints/note_test.go index 5b036e1..cfa4130 100644 --- a/pkg/e621/endpoints/note_test.go +++ b/pkg/e621/endpoints/note_test.go @@ -1,7 +1,7 @@ package endpoints import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/endpoints/pool.go b/pkg/e621/endpoints/pool.go index 17a2466..35c55b7 100644 --- a/pkg/e621/endpoints/pool.go +++ b/pkg/e621/endpoints/pool.go @@ -3,8 +3,8 @@ package endpoints import ( "encoding/json" "fmt" - "git.dragse.it/anthrove/e621-to-graph/internal/utils" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "log" "net/http" ) diff --git a/pkg/e621/endpoints/pool_test.go b/pkg/e621/endpoints/pool_test.go index fdefc36..b8ea570 100644 --- a/pkg/e621/endpoints/pool_test.go +++ b/pkg/e621/endpoints/pool_test.go @@ -1,7 +1,7 @@ package endpoints import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/endpoints/post.go b/pkg/e621/endpoints/post.go index ff182cc..61596f6 100644 --- a/pkg/e621/endpoints/post.go +++ b/pkg/e621/endpoints/post.go @@ -3,8 +3,8 @@ package endpoints import ( "encoding/json" "fmt" - "git.dragse.it/anthrove/e621-to-graph/internal/utils" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "log" "net/http" ) diff --git a/pkg/e621/endpoints/post_test.go b/pkg/e621/endpoints/post_test.go index eac3fcf..d97b381 100644 --- a/pkg/e621/endpoints/post_test.go +++ b/pkg/e621/endpoints/post_test.go @@ -1,7 +1,7 @@ package endpoints import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/endpoints/tag.go b/pkg/e621/endpoints/tag.go index a30c4da..00df072 100644 --- a/pkg/e621/endpoints/tag.go +++ b/pkg/e621/endpoints/tag.go @@ -3,8 +3,8 @@ package endpoints import ( "encoding/json" "fmt" - "git.dragse.it/anthrove/e621-to-graph/internal/utils" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "log" "net/http" ) diff --git a/pkg/e621/endpoints/tag_test.go b/pkg/e621/endpoints/tag_test.go index 9282e33..a49ca43 100644 --- a/pkg/e621/endpoints/tag_test.go +++ b/pkg/e621/endpoints/tag_test.go @@ -1,7 +1,7 @@ package endpoints import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/endpoints/user.go b/pkg/e621/endpoints/user.go index b34cbb8..a84bc36 100644 --- a/pkg/e621/endpoints/user.go +++ b/pkg/e621/endpoints/user.go @@ -3,8 +3,8 @@ package endpoints import ( "encoding/json" "fmt" - "git.dragse.it/anthrove/e621-to-graph/internal/utils" - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/utils" "log" "net/http" ) diff --git a/pkg/e621/endpoints/user_test.go b/pkg/e621/endpoints/user_test.go index 4e0a2a6..7eda544 100644 --- a/pkg/e621/endpoints/user_test.go +++ b/pkg/e621/endpoints/user_test.go @@ -1,7 +1,7 @@ package endpoints import ( - "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" "github.com/jarcoal/httpmock" "net/http" "testing" diff --git a/pkg/e621/model/basic.go b/pkg/e621/model/basic.go index 3e96e3a..1b01c9d 100644 --- a/pkg/e621/model/basic.go +++ b/pkg/e621/model/basic.go @@ -1,11 +1,15 @@ package model -import "net/http" +import ( + "golang.org/x/time/rate" + "net/http" +) type RequestContext struct { - Client http.Client - Host string - UserAgent string - Username string - APIKey string + Client http.Client + RateLimiter *rate.Limiter + Host string + UserAgent string + Username string + APIKey string } diff --git a/pkg/e621/model/pool.go b/pkg/e621/model/pool.go index 3af933d..0d8fff4 100644 --- a/pkg/e621/model/pool.go +++ b/pkg/e621/model/pool.go @@ -6,10 +6,13 @@ 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" +) + +const ( + PoolName PoolOrder = "name" + CreatedAt PoolOrder = "created_at" + UpdatedAt PoolOrder = "updated_at" + PostCount PoolOrder = "post_count" ) type Pool struct { diff --git a/pkg/e621/model/post.go b/pkg/e621/model/post.go index fd221fa..f6cea35 100644 --- a/pkg/e621/model/post.go +++ b/pkg/e621/model/post.go @@ -1,12 +1,14 @@ package model +type PostID int64 + type PostResponse struct { Post Post `json:"post"` Posts []Post `json:"posts"` } type Post struct { - ID int64 `json:"id"` + ID PostID `json:"id"` CreatedAt string `json:"created_at"` UpdatedAt string `json:"updated_at"` File File `json:"file"` diff --git a/pkg/e621/model/user.go b/pkg/e621/model/user.go index 7721db8..2a915cf 100644 --- a/pkg/e621/model/user.go +++ b/pkg/e621/model/user.go @@ -1,28 +1,51 @@ package model +type UserID int64 +type UserLevel int +type Order string + +const ( + Anonymus UserLevel = 0 + Blocked UserLevel = 10 + Member UserLevel = 20 + Privilaged UserLevel = 30 + FormerStaff UserLevel = 34 + Janitor UserLevel = 35 + Moderator UserLevel = 40 + Admin UserLevel = 50 +) + +const ( + JoinDate Order = "date" + UserName Order = "name" + PostUploadCount Order = "post_upload_count" + NoteCount Order = "note_count" + PostUpdateCount Order = "post_upload_count" +) + type User struct { - WikiPageVersionCount int64 `json:"wiki_page_version_count"` - ArtistVersionCount int64 `json:"artist_version_count"` - PoolVersionCount int64 `json:"pool_version_count"` - ForumPostCount int64 `json:"forum_post_count"` - CommentCount int64 `json:"comment_count"` - FlagCount int64 `json:"flag_count"` - FavoriteCount int64 `json:"favorite_count"` - PositiveFeedbackCount int64 `json:"positive_feedback_count"` - NeutralFeedbackCount int64 `json:"neutral_feedback_count"` - NegativeFeedbackCount int64 `json:"negative_feedback_count"` - UploadLimit int64 `json:"upload_limit"` - ID int64 `json:"id"` - CreatedAt string `json:"created_at"` - Name string `json:"name"` - Level int64 `json:"level"` - BaseUploadLimit int64 `json:"base_upload_limit"` - PostUploadCount int64 `json:"post_upload_count"` - PostUpdateCount int64 `json:"post_update_count"` - NoteUpdateCount int64 `json:"note_update_count"` - IsBanned bool `json:"is_banned"` - CanApprovePosts bool `json:"can_approve_posts"` - CanUploadFree bool `json:"can_upload_free"` - LevelString string `json:"level_string"` - AvatarID interface{} `json:"avatar_id"` + WikiPageVersionCount int64 `json:"wiki_page_version_count"` + ArtistVersionCount int64 `json:"artist_version_count"` + PoolVersionCount int64 `json:"pool_version_count"` + ForumPostCount int64 `json:"forum_post_count"` + CommentCount int64 `json:"comment_count"` + FlagCount int64 `json:"flag_count"` + FavoriteCount int64 `json:"favorite_count"` + PositiveFeedbackCount int64 `json:"positive_feedback_count"` + NeutralFeedbackCount int64 `json:"neutral_feedback_count"` + NegativeFeedbackCount int64 `json:"negative_feedback_count"` + UploadLimit int64 `json:"upload_limit"` + ID UserID `json:"id"` + CreatedAt string `json:"created_at"` + Name string `json:"name"` + Level UserLevel `json:"level"` + BaseUploadLimit int64 `json:"base_upload_limit"` + PostUploadCount int64 `json:"post_upload_count"` + PostUpdateCount int64 `json:"post_update_count"` + NoteUpdateCount int64 `json:"note_update_count"` + IsBanned bool `json:"is_banned"` + CanApprovePosts bool `json:"can_approve_posts"` + CanUploadFree bool `json:"can_upload_free"` + LevelString string `json:"level_string"` + AvatarID PostID `json:"avatar_id"` } diff --git a/internal/utils/error.go b/pkg/e621/utils/error.go similarity index 100% rename from internal/utils/error.go rename to pkg/e621/utils/error.go