From 9173ee8279753c73359b2c5c30d9540e24f348d8 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 6 Nov 2023 15:56:36 +0100 Subject: [PATCH] docs: added basic documentation (needs further reviewer) Signed-off-by: SoXX --- pkg/e621/builder/favorite.go | 13 +++++++-- pkg/e621/builder/note.go | 9 ++++--- pkg/e621/builder/notes.go | 37 +++++++++++++++++++------- pkg/e621/builder/pool.go | 11 +++++--- pkg/e621/builder/pools.go | 39 ++++++++++++++++++++------- pkg/e621/builder/post.go | 7 ++++- pkg/e621/builder/posts.go | 17 ++++++++++-- pkg/e621/builder/tag.go | 11 +++++--- pkg/e621/builder/tags.go | 25 +++++++++++++++--- pkg/e621/builder/user.go | 7 ++++- pkg/e621/builder/users.go | 51 +++++++++++++++++++++++++++--------- pkg/e621/client.go | 32 +++++++++++++++++++--- pkg/e621/utils/constants.go | 5 ++-- pkg/e621/utils/error.go | 49 +++++++++++++++++----------------- 14 files changed, 233 insertions(+), 80 deletions(-) diff --git a/pkg/e621/builder/favorite.go b/pkg/e621/builder/favorite.go index 0395129..5fb6de6 100644 --- a/pkg/e621/builder/favorite.go +++ b/pkg/e621/builder/favorite.go @@ -7,13 +7,19 @@ import ( "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(number 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 FavoritesBuilder with the provided RequestContext. func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder { return &getFavorites{ requestContext: requestContext, @@ -26,21 +32,25 @@ type getFavorites struct { requestContext model.RequestContext } +// SetUserID sets the user ID for the query. 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. func (g *getFavorites) SetLimit(limitFavorites int) FavoritesBuilder { g.query["limit"] = strconv.Itoa(limitFavorites) return g } +// Page sets the page number for paginated results. 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. func (g *getFavorites) Execute() ([]model.Post, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -50,7 +60,6 @@ func (g *getFavorites) Execute() ([]model.Post, error) { } favorites, err := endpoints.GetFavorites(g.requestContext, g.query) if err != nil { - return nil, err } return favorites, nil diff --git a/pkg/e621/builder/note.go b/pkg/e621/builder/note.go index 3c5bcc6..02e5a9b 100644 --- a/pkg/e621/builder/note.go +++ b/pkg/e621/builder/note.go @@ -7,14 +7,17 @@ import ( "strconv" ) +// NoteBuilder represents a builder for constructing queries to retrieve a specific note. type NoteBuilder interface { + // SetNoteID sets the note ID for the query. SetNoteID(noteID int) NoteBuilder + // Execute sends the constructed query and returns the requested note and an error, if any. Execute() (*model.Note, error) } +// NewGetNoteBuilder creates a new NoteBuilder with the provided RequestContext. func NewGetNoteBuilder(requestContext model.RequestContext) NoteBuilder { return &getNote{requestContext: requestContext} - } type getNote struct { @@ -22,11 +25,13 @@ type getNote struct { noteID int } +// SetNoteID sets the note ID for the query. func (g *getNote) SetNoteID(noteID int) NoteBuilder { g.noteID = noteID return g } +// Execute sends the constructed query and returns the requested note and an error, if any. func (g *getNote) Execute() (*model.Note, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -36,9 +41,7 @@ func (g *getNote) Execute() (*model.Note, error) { } note, err := endpoints.GetNote(g.requestContext, strconv.Itoa(g.noteID)) if err != nil { - return nil, err } return ¬e, nil - } diff --git a/pkg/e621/builder/notes.go b/pkg/e621/builder/notes.go index dd00185..f4d5545 100644 --- a/pkg/e621/builder/notes.go +++ b/pkg/e621/builder/notes.go @@ -7,17 +7,27 @@ import ( "strconv" ) +// NotesBuilder represents a builder for constructing queries to retrieve notes. type NotesBuilder interface { + // SearchInBody sets the query to search for notes containing a specific text in their body. SearchInBody(body string) NotesBuilder + // NoteID sets the query to search for a note with a specific note ID. NoteID(noteID int) NotesBuilder + // SearchTags sets the query to search for notes containing specific tags. SearchTags(tags string) NotesBuilder + // SearchCreatorID sets the query to search for notes created by a specific user (by their user ID). SearchCreatorID(creatorID int) NotesBuilder - searchCreatorName(creatorName string) NotesBuilder + // SearchCreatorName sets the query to search for notes created by a specific user (by their username). + SearchCreatorName(creatorName string) NotesBuilder + // Active sets whether to search for active or inactive notes. Active(isActive bool) NotesBuilder + // SetLimit sets the maximum number of notes to retrieve. SetLimit(limitNotes int) NotesBuilder + // Execute sends the constructed query and returns a slice of notes and an error, if any. Execute() ([]model.Note, error) } +// NewGetNotesBuilder creates a new NotesBuilder with the provided RequestContext. func NewGetNotesBuilder(requestContext model.RequestContext) NotesBuilder { return &getNotes{ requestContext: requestContext, @@ -30,42 +40,50 @@ type getNotes struct { query map[string]string } -func (g getNotes) SearchInBody(body string) NotesBuilder { +// SearchInBody sets the query to search for notes containing a specific text in their body. +func (g *getNotes) SearchInBody(body string) NotesBuilder { g.query["search[body_matches]"] = body return g } -func (g getNotes) NoteID(noteID int) NotesBuilder { +// NoteID sets the query to search for a note with a specific note ID. +func (g *getNotes) NoteID(noteID int) NotesBuilder { g.query["search[post_id]"] = strconv.Itoa(noteID) return g } -func (g getNotes) SearchTags(tags string) NotesBuilder { +// SearchTags sets the query to search for notes containing specific tags. +func (g *getNotes) SearchTags(tags string) NotesBuilder { g.query["search[post_tags_match]"] = tags return g } -func (g getNotes) SearchCreatorID(creatorID int) NotesBuilder { +// SearchCreatorID sets the query to search for notes created by a specific user (by their user ID). +func (g *getNotes) SearchCreatorID(creatorID int) NotesBuilder { g.query["search[creator_id]"] = strconv.Itoa(creatorID) return g } -func (g getNotes) searchCreatorName(creatorName string) NotesBuilder { +// SearchCreatorName sets the query to search for notes created by a specific user (by their username). +func (g *getNotes) SearchCreatorName(creatorName string) NotesBuilder { g.query["search[creator_name]"] = creatorName return g } -func (g getNotes) Active(isActive bool) NotesBuilder { +// Active sets whether to search for active or inactive notes. +func (g *getNotes) Active(isActive bool) NotesBuilder { g.query["search[is_active]"] = strconv.FormatBool(isActive) return g } -func (g getNotes) SetLimit(limitNotes int) NotesBuilder { +// SetLimit sets the maximum number of notes to retrieve. +func (g *getNotes) SetLimit(limitNotes int) NotesBuilder { g.query["limit"] = strconv.Itoa(limitNotes) return g } -func (g getNotes) Execute() ([]model.Note, error) { +// Execute sends the constructed query and returns a slice of notes and an error, if any. +func (g *getNotes) Execute() ([]model.Note, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) if err != nil { @@ -74,7 +92,6 @@ func (g getNotes) Execute() ([]model.Note, error) { } notes, err := endpoints.GetNotes(g.requestContext, g.query) if err != nil { - return nil, err } return notes, nil diff --git a/pkg/e621/builder/pool.go b/pkg/e621/builder/pool.go index 1559b9b..53f9530 100644 --- a/pkg/e621/builder/pool.go +++ b/pkg/e621/builder/pool.go @@ -7,11 +7,15 @@ import ( "strconv" ) +// PoolBuilder represents a builder for constructing queries to retrieve a specific pool. type PoolBuilder interface { + // ID sets the pool ID for the query. ID(poolID int) PoolBuilder + // Execute sends the constructed query and returns the requested pool and an error, if any. Execute() (model.Pool, error) } +// NewGetPoolBuilder creates a new PoolBuilder with the provided RequestContext. func NewGetPoolBuilder(requestContext model.RequestContext) PoolBuilder { return &getPool{requestContext: requestContext} } @@ -21,12 +25,14 @@ type getPool struct { id int } -func (g getPool) ID(poolID int) PoolBuilder { +// ID sets the pool ID for the query. +func (g *getPool) ID(poolID int) PoolBuilder { g.id = poolID return g } -func (g getPool) Execute() (model.Pool, error) { +// Execute sends the constructed query and returns the requested pool and an error, if any. +func (g *getPool) Execute() (model.Pool, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) if err != nil { @@ -35,7 +41,6 @@ func (g getPool) Execute() (model.Pool, error) { } pool, err := endpoints.GetPool(g.requestContext, strconv.Itoa(g.id)) if err != nil { - return model.Pool{}, err } return pool, nil diff --git a/pkg/e621/builder/pools.go b/pkg/e621/builder/pools.go index df52598..99ba6cc 100644 --- a/pkg/e621/builder/pools.go +++ b/pkg/e621/builder/pools.go @@ -7,22 +7,34 @@ import ( "strconv" ) +// PoolsBuilder represents a builder for constructing queries to retrieve pools. type PoolsBuilder interface { + // SearchName sets the query to search for pools with a specific name. SearchName(name string) PoolsBuilder + // SearchID sets the query to search for pools with specific pool IDs. SearchID(poolIDs string) PoolsBuilder - SearchDescritpion(description string) PoolsBuilder + // SearchDescription sets the query to search for pools with specific descriptions. + SearchDescription(description string) PoolsBuilder + // SearchCreatorID sets the query to search for pools created by a specific user (by their user ID). SearchCreatorID(creatorID int) PoolsBuilder - searchCreatorName(creatorName string) PoolsBuilder + // SearchCreatorName sets the query to search for pools created by a specific user (by their username). + SearchCreatorName(creatorName string) PoolsBuilder + // Active sets whether to search for active or inactive pools. Active(isActive bool) PoolsBuilder + // SearchCategory sets the query to search for pools in a specific category. SearchCategory(category model.PoolCategory) PoolsBuilder + // Order sets the ordering of the retrieved pools. Order(order model.PoolOrder) PoolsBuilder + // SetLimit sets the maximum number of pools to retrieve. SetLimit(limitPools int) PoolsBuilder + // Execute sends the constructed query and returns a slice of pools and an error, if any. Execute() ([]model.Pool, error) } -func NewGetPoolsBuilder(requstContext model.RequestContext) PoolsBuilder { +// NewGetPoolsBuilder creates a new PoolsBuilder with the provided RequestContext. +func NewGetPoolsBuilder(requestContext model.RequestContext) PoolsBuilder { return &getPools{ - requestContext: requstContext, + requestContext: requestContext, query: make(map[string]string), } } @@ -32,51 +44,61 @@ type getPools struct { query map[string]string } +// SearchName sets the query to search for pools with a specific name. func (g *getPools) SearchName(name string) PoolsBuilder { g.query["search[name_matches]"] = name return g } +// SearchID sets the query to search for pools with specific pool IDs. func (g *getPools) SearchID(poolIDs string) PoolsBuilder { g.query["search[id]"] = poolIDs return g } -func (g *getPools) SearchDescritpion(description string) PoolsBuilder { +// SearchDescription sets the query to search for pools with specific descriptions. +func (g *getPools) SearchDescription(description string) PoolsBuilder { g.query["search[description_matches]"] = description return g } +// SearchCreatorID sets the query to search for pools created by a specific user (by their user ID). func (g *getPools) SearchCreatorID(creatorID int) PoolsBuilder { g.query["search[creator_id]"] = strconv.Itoa(creatorID) return g } -func (g *getPools) searchCreatorName(creatorName string) PoolsBuilder { +// SearchCreatorName sets the query to search for pools created by a specific user (by their username). +func (g *getPools) SearchCreatorName(creatorName string) PoolsBuilder { g.query["search[creator_name]"] = creatorName return g } +// Active sets whether to search for active or inactive pools. func (g *getPools) Active(isActive bool) PoolsBuilder { g.query["search[is_active]"] = strconv.FormatBool(isActive) return g } +// SearchCategory sets the query to search for pools in a specific category. func (g *getPools) SearchCategory(category model.PoolCategory) PoolsBuilder { g.query["search[category]"] = string(category) return g } +// Order sets the ordering of the retrieved pools. 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) +// SetLimit sets the maximum number of pools to retrieve. +func (g *getPools) SetLimit(limitPools int) PoolsBuilder { + g.query["limit"] = strconv.Itoa(limitPools) return g } +// Execute sends the constructed query and returns a slice of pools and an error, if any. func (g *getPools) Execute() ([]model.Pool, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -86,7 +108,6 @@ func (g *getPools) Execute() ([]model.Pool, error) { } pools, err := endpoints.GetPools(g.requestContext, g.query) if err != nil { - return nil, err } return pools, nil diff --git a/pkg/e621/builder/post.go b/pkg/e621/builder/post.go index 4d63952..7b04454 100644 --- a/pkg/e621/builder/post.go +++ b/pkg/e621/builder/post.go @@ -7,11 +7,15 @@ import ( "strconv" ) +// PostBuilder represents a builder for constructing queries to retrieve a specific post. type PostBuilder interface { + // SetPostID sets the post ID for the query. SetPostID(postID model.PostID) PostBuilder + // Execute sends the constructed query and returns the requested post and an error, if any. Execute() (*model.Post, error) } +// NewGetPostBuilder creates a new PostBuilder with the provided RequestContext. func NewGetPostBuilder(requestContext model.RequestContext) PostBuilder { return &getPost{requestContext: requestContext} } @@ -21,11 +25,13 @@ type getPost struct { postID model.PostID } +// SetPostID sets the post ID for the query. func (g *getPost) SetPostID(postID model.PostID) PostBuilder { g.postID = postID return g } +// Execute sends the constructed query and returns the requested post and an error, if any. func (g *getPost) Execute() (*model.Post, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -35,7 +41,6 @@ func (g *getPost) Execute() (*model.Post, error) { } post, err := endpoints.GetPost(g.requestContext, strconv.Itoa(int(g.postID))) if err != nil { - return nil, err } return &post, nil diff --git a/pkg/e621/builder/posts.go b/pkg/e621/builder/posts.go index de739c6..45fa0cb 100644 --- a/pkg/e621/builder/posts.go +++ b/pkg/e621/builder/posts.go @@ -8,17 +8,25 @@ import ( "strconv" ) +// PostsBuilder represents a builder for constructing queries to retrieve posts. type PostsBuilder interface { + // Tags sets the query to search for posts with specific tags. Tags(tags string) PostsBuilder + // PageAfter sets the query to retrieve posts after a specific post ID. PageAfter(postID model.PostID) PostsBuilder + // PageBefore sets the query to retrieve posts before a specific post ID. PageBefore(postID model.PostID) PostsBuilder + // PageNumber sets the query to retrieve posts on a specific page number. PageNumber(number int) PostsBuilder + // SetLimit sets the maximum number of posts to retrieve. SetLimit(limitUser int) PostsBuilder + // Execute sends the constructed query and returns a slice of posts and an error, if any. Execute() ([]model.Post, error) } +// NewGetPostsBuilder creates a new PostsBuilder with the provided RequestContext. func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder { - var postBuilder PostsBuilder = &getPosts{ + postBuilder := &getPosts{ requestContext: requestContext, query: make(map[string]string), } @@ -31,31 +39,37 @@ type getPosts struct { query map[string]string } +// Tags sets the query to search for posts with specific tags. func (g *getPosts) Tags(tags string) PostsBuilder { g.query["tags"] = tags return g } +// PageAfter sets the query to retrieve posts after a specific post ID. func (g *getPosts) PageAfter(postID model.PostID) PostsBuilder { g.query["page"] = "a" + strconv.Itoa(int(postID)) return g } +// PageBefore sets the query to retrieve posts before a specific post ID. func (g *getPosts) PageBefore(postID model.PostID) PostsBuilder { g.query["page"] = "b" + strconv.Itoa(int(postID)) return g } +// PageNumber sets the query to retrieve posts on a specific page number. func (g *getPosts) PageNumber(number int) PostsBuilder { g.query["page"] = strconv.Itoa(number) return g } +// SetLimit sets the maximum number of posts to retrieve. func (g *getPosts) SetLimit(limitUser int) PostsBuilder { g.query["limit"] = strconv.Itoa(limitUser) return g } +// Execute sends the constructed query and returns a slice of posts and an error, if any. func (g *getPosts) Execute() ([]model.Post, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -65,7 +79,6 @@ func (g *getPosts) Execute() ([]model.Post, error) { } posts, err := endpoints.GetPosts(g.requestContext, g.query) if err != nil { - return nil, err } return posts, err diff --git a/pkg/e621/builder/tag.go b/pkg/e621/builder/tag.go index 1a035c4..d7200f7 100644 --- a/pkg/e621/builder/tag.go +++ b/pkg/e621/builder/tag.go @@ -7,13 +7,17 @@ import ( "strconv" ) +// TagBuilder represents a builder for constructing queries to retrieve a specific tag. type TagBuilder interface { + // SetTagID sets the tag ID for the query. SetTagID(tagID int) TagBuilder + // Execute sends the constructed query and returns the requested tag and an error, if any. Execute() (model.Tag, error) } -func NewGetTagBuilder(requstContext model.RequestContext) TagBuilder { - return &getTag{requestContext: requstContext} +// NewGetTagBuilder creates a new TagBuilder with the provided RequestContext. +func NewGetTagBuilder(requestContext model.RequestContext) TagBuilder { + return &getTag{requestContext: requestContext} } type getTag struct { @@ -21,11 +25,13 @@ type getTag struct { tagID int } +// SetTagID sets the tag ID for the query. func (g *getTag) SetTagID(tagID int) TagBuilder { g.tagID = tagID return g } +// Execute sends the constructed query and returns the requested tag and an error, if any. func (g *getTag) Execute() (model.Tag, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -35,7 +41,6 @@ func (g *getTag) Execute() (model.Tag, error) { } tag, err := endpoints.GetTag(g.requestContext, strconv.Itoa(g.tagID)) if err != nil { - return model.Tag{}, err } return tag, nil diff --git a/pkg/e621/builder/tags.go b/pkg/e621/builder/tags.go index c24f2b6..54b596f 100644 --- a/pkg/e621/builder/tags.go +++ b/pkg/e621/builder/tags.go @@ -7,20 +7,31 @@ import ( "strconv" ) +// TagsBuilder represents a builder for constructing queries to retrieve tags. type TagsBuilder interface { + // SearchName sets the query to search for tags with specific names. SearchName(name string) TagsBuilder + // SearchCategory sets the query to search for tags in a specific category. SearchCategory(category model.TagCategory) TagsBuilder + // Order sets the query to order tags by a specific criterion. Order(order string) TagsBuilder + // HideEmpty sets the query to filter out tags that are empty. HideEmpty(hideEmpty bool) TagsBuilder + // Wiki sets the query to filter tags that have a wiki. Wiki(hasWiki bool) TagsBuilder + // Artist sets the query to filter tags that have an artist page. Artist(hasArtistPage bool) TagsBuilder + // SetPage sets the query to retrieve tags from a specific page number. SetPage(pageNumber int) TagsBuilder + // SetLimit sets the maximum number of tags to retrieve. SetLimit(limitUser int) TagsBuilder + // Execute sends the constructed query and returns a slice of tags and an error, if any. Execute() ([]model.Tag, error) } -func NewGetTagsBuilder(requstContext model.RequestContext) TagsBuilder { - return &getTags{requestContext: requstContext, query: make(map[string]string)} +// NewGetTagsBuilder creates a new TagsBuilder with the provided RequestContext. +func NewGetTagsBuilder(requestContext model.RequestContext) TagsBuilder { + return &getTags{requestContext: requestContext, query: make(map[string]string)} } type getTags struct { @@ -28,46 +39,55 @@ type getTags struct { query map[string]string } +// SearchName sets the query to search for tags with specific names. func (g *getTags) SearchName(name string) TagsBuilder { g.query["search[name_matches]"] = name return g } +// SearchCategory sets the query to search for tags in a specific category. func (g *getTags) SearchCategory(category model.TagCategory) TagsBuilder { g.query["search[category]"] = strconv.Itoa(int(category)) return g } +// Order sets the query to order tags by a specific criterion. func (g *getTags) Order(order string) TagsBuilder { g.query["search[order]"] = order return g } +// HideEmpty sets the query to filter out tags that are empty. func (g *getTags) HideEmpty(hideEmpty bool) TagsBuilder { g.query["search[hide_empty]"] = strconv.FormatBool(hideEmpty) return g } +// Wiki sets the query to filter tags that have a wiki. func (g *getTags) Wiki(hasWiki bool) TagsBuilder { g.query["search[has_wiki]"] = strconv.FormatBool(hasWiki) return g } +// Artist sets the query to filter tags that have an artist page. func (g *getTags) Artist(hasArtistPage bool) TagsBuilder { g.query["search[has_artist]"] = strconv.FormatBool(hasArtistPage) return g } +// SetPage sets the query to retrieve tags from a specific page number. func (g *getTags) SetPage(pageNumber int) TagsBuilder { g.query["page"] = strconv.Itoa(pageNumber) return g } +// SetLimit sets the maximum number of tags to retrieve. func (g *getTags) SetLimit(limitUser int) TagsBuilder { g.query["limit"] = strconv.Itoa(limitUser) return g } +// Execute sends the constructed query and returns a slice of tags and an error, if any. func (g *getTags) Execute() ([]model.Tag, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -77,7 +97,6 @@ func (g *getTags) Execute() ([]model.Tag, error) { } tags, err := endpoints.GetTags(g.requestContext, g.query) if err != nil { - return nil, err } return tags, err diff --git a/pkg/e621/builder/user.go b/pkg/e621/builder/user.go index ee0f1af..102c9bd 100644 --- a/pkg/e621/builder/user.go +++ b/pkg/e621/builder/user.go @@ -6,11 +6,15 @@ import ( "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" ) +// UserBuilder represents a builder for constructing queries to retrieve user information. type UserBuilder interface { + // SetUsername sets the username for the query to retrieve user information. SetUsername(username string) UserBuilder + // Execute sends the constructed query and returns the requested user information and an error, if any. Execute() (model.User, error) } +// NewGetUserBuilder creates a new UserBuilder with the provided RequestContext. func NewGetUserBuilder(requestContext model.RequestContext) UserBuilder { return &getUser{requestContext: requestContext} } @@ -20,11 +24,13 @@ type getUser struct { username string } +// SetUsername sets the username for the query to retrieve user information. func (g *getUser) SetUsername(username string) UserBuilder { g.username = username return g } +// Execute sends the constructed query and returns the requested user information and an error, if any. func (g *getUser) Execute() (model.User, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -34,7 +40,6 @@ func (g *getUser) Execute() (model.User, error) { } user, err := endpoints.GetUser(g.requestContext, g.username) if err != nil { - return model.User{}, err } return user, nil diff --git a/pkg/e621/builder/users.go b/pkg/e621/builder/users.go index 371c9ef..a55443d 100644 --- a/pkg/e621/builder/users.go +++ b/pkg/e621/builder/users.go @@ -7,21 +7,35 @@ import ( "strconv" ) +// UsersBuilder represents a builder for constructing queries to retrieve user information. type UsersBuilder interface { + // SetPage sets the page number for paginated results. SetPage(pageNumber int) UsersBuilder + // SetLimit sets the maximum number of users to retrieve. SetLimit(limitUser int) UsersBuilder + // SearchByName specifies a username to search for. SearchByName(userName string) UsersBuilder + // SearchByAbout specifies a text to search in the 'about' field of user profiles. SearchByAbout(about string) UsersBuilder - SearchByAvatarID(postID model.PostID) UsersBuilder + // SearchByAvatarID specifies an avatar ID to search for. + SearchByAvatarID(avatarID model.PostID) UsersBuilder + // SearchByLevel specifies a user level to filter by. SearchByLevel(level model.UserLevel) UsersBuilder - SearchByMinLevel(level model.UserLevel) UsersBuilder - SearchByMaxLevellevel(model.UserLevel) UsersBuilder - searchByCanUpload(canUpload bool) UsersBuilder + // SearchByMinLevel specifies the minimum user level to filter by. + SearchByMinLevel(minLevel model.UserLevel) UsersBuilder + // SearchByMaxLevel specifies the maximum user level to filter by. + SearchByMaxLevel(maxLevel model.UserLevel) UsersBuilder + // SearchByCanUpload specifies whether users can upload free content. + SearchByCanUpload(canUpload bool) UsersBuilder + // SearchByIsApprover specifies whether users can approve posts. SearchByIsApprover(isApprover bool) UsersBuilder + // SearchByOrder specifies the order in which users are retrieved. SearchByOrder(order model.Order) UsersBuilder + // Execute sends the constructed query and returns the requested user information and an error, if any. Execute() ([]model.User, error) } +// NewGetUsersBuilder creates a new UsersBuilder with the provided RequestContext. func NewGetUsersBuilder(requestContext model.RequestContext) UsersBuilder { return &getUsers{requestContext: requestContext, query: make(map[string]string)} } @@ -31,61 +45,73 @@ type getUsers struct { query map[string]string } +// SearchByName specifies a username to search for. func (g *getUsers) SearchByName(userName string) UsersBuilder { g.query["search[name_matches]"] = userName return g } +// SearchByAbout specifies a text to search in the 'about' field of user profiles. func (g *getUsers) SearchByAbout(about string) UsersBuilder { - g.query["search[about_me"] = about + 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) +// SearchByAvatarID specifies an avatar ID to search for. +func (g *getUsers) SearchByAvatarID(avatarID model.PostID) UsersBuilder { + g.query["search[avatar_id]"] = strconv.FormatInt(int64(avatarID), 10) return g } +// SearchByLevel specifies a user level to filter by. 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)) +// SearchByMinLevel specifies the minimum user level to filter by. +func (g *getUsers) SearchByMinLevel(minLevel model.UserLevel) UsersBuilder { + g.query["search[min_level]"] = strconv.Itoa(int(minLevel)) return g } -func (g *getUsers) SearchByMaxLevellevel(level model.UserLevel) UsersBuilder { - g.query["search[max_level]"] = strconv.Itoa(int(level)) +// SearchByMaxLevel specifies the maximum user level to filter by. +func (g *getUsers) SearchByMaxLevel(maxLevel model.UserLevel) UsersBuilder { + g.query["search[max_level]"] = strconv.Itoa(int(maxLevel)) return g } -func (g *getUsers) searchByCanUpload(canUpload bool) UsersBuilder { +// SearchByCanUpload specifies whether users can upload free content. +func (g *getUsers) SearchByCanUpload(canUpload bool) UsersBuilder { g.query["search[can_upload_free]"] = strconv.FormatBool(canUpload) return g } +// SearchByIsApprover specifies whether users can approve posts. func (g *getUsers) SearchByIsApprover(isApprover bool) UsersBuilder { g.query["search[can_approve_posts]"] = strconv.FormatBool(isApprover) return g } +// SearchByOrder specifies the order in which users are retrieved. func (g *getUsers) SearchByOrder(order model.Order) UsersBuilder { g.query["search[order]"] = string(order) return g } +// SetPage sets the page number for paginated results. func (g *getUsers) SetPage(pageNumber int) UsersBuilder { g.query["page"] = strconv.Itoa(pageNumber) return g } +// SetLimit sets the maximum number of users to retrieve. func (g *getUsers) SetLimit(limitUser int) UsersBuilder { g.query["limit"] = strconv.Itoa(limitUser) return g } +// Execute sends the constructed query and returns the requested user information and an error, if any. func (g *getUsers) Execute() ([]model.User, error) { if g.requestContext.RateLimiter != nil { err := g.requestContext.RateLimiter.Wait(context.Background()) @@ -95,7 +121,6 @@ func (g *getUsers) Execute() ([]model.User, error) { } users, err := endpoints.GetUsers(g.requestContext, g.query) if err != nil { - return nil, err } return users, nil diff --git a/pkg/e621/client.go b/pkg/e621/client.go index ad86132..7028c5c 100644 --- a/pkg/e621/client.go +++ b/pkg/e621/client.go @@ -12,11 +12,14 @@ import ( "strconv" ) +// Client is the main client for interacting with the e621 API. type Client struct { RequestContext model.RequestContext } -func NewE621Client(username string, apikey string) Client { +// NewE621Client creates a new e621 client with the provided username and API key. +func NewE621Client(username string, apiKey string) Client { + // Create a new e621 client with the given username and API key. return Client{ RequestContext: model.RequestContext{ Client: http.Client{}, @@ -24,30 +27,40 @@ func NewE621Client(username string, apikey string) Client { Host: "https://e621.net", UserAgent: fmt.Sprintf("Go-e621-SDK used by %s | (made by the Anthrove Team)", username), Username: username, - APIKey: apikey, + APIKey: apiKey, }, } } +// SetHost sets the API host for the client. func (c *Client) SetHost(host string) *Client { + // Set the API host for the client. c.RequestContext.Host = host return c } +// SetAgentName sets the user agent name for the client. func (c *Client) SetAgentName(userAgent string) *Client { + // Set the user agent name for the client. c.RequestContext.UserAgent = userAgent return c } +// GetUserByName returns a user builder for a given username. func (c *Client) GetUserByName(username string) builder.UserBuilder { + // Returns a user builder for the specified username. return builder.NewGetUserBuilder(c.RequestContext).SetUsername(username) } +// GetUserByID returns a user builder for a given user ID. func (c *Client) GetUserByID(userID model.UserID) builder.UserBuilder { + // Returns a user builder for the specified user ID. return builder.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) { + // Returns a favorites builder for the specified username. user, err := builder.NewGetUserBuilder(c.RequestContext).SetUsername(username).Execute() if err != nil { return nil, err @@ -56,7 +69,9 @@ func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder, 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) { + // Retrieves a specified number of favorite posts for a user. if n < utils.E621_MAX_POST_COUNT { favoriteBuilder.SetLimit(n) } @@ -78,27 +93,36 @@ func (c *Client) GetNFavoritesForUser(n int, favoriteBuilder builder.FavoritesBu } return favorites, nil - } +// GetAllFavoritesForUser retrieves all favorites for a user. func (c *Client) GetAllFavoritesForUser(favoriteBuilder builder.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 { + // 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)) return favoritesBuilder } +// GetPostByID returns a post builder for a specific post ID. func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder { + // Returns a post builder for a specific post ID. return builder.NewGetPostBuilder(c.RequestContext).SetPostID(id) } +// GetPosts returns a posts builder for general post queries. func (c *Client) GetPosts() builder.PostsBuilder { + // Returns a posts builder for general post queries. return builder.NewGetPostsBuilder(c.RequestContext) } +// GetNPosts retrieves a specified number of posts. func (c *Client) GetNPosts(n int, postBuilder builder.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) } @@ -125,6 +149,8 @@ func (c *Client) GetNPosts(n int, postBuilder builder.PostsBuilder) ([]model.Pos return posts, nil } +// GetAllPosts retrieves all available posts using the provided post builder. func (c *Client) GetAllPosts(postBuilder builder.PostsBuilder) ([]model.Post, error) { + // Retrieves all available posts using the provided post builder. return c.GetNPosts(math.MaxInt, postBuilder) } diff --git a/pkg/e621/utils/constants.go b/pkg/e621/utils/constants.go index 8322e6b..d7df2e8 100644 --- a/pkg/e621/utils/constants.go +++ b/pkg/e621/utils/constants.go @@ -1,5 +1,4 @@ package utils -const ( - E621_MAX_POST_COUNT = 320 -) +// E621_MAX_POST_COUNT is the maximum allowable post count for E621. +const E621_MAX_POST_COUNT = 320 diff --git a/pkg/e621/utils/error.go b/pkg/e621/utils/error.go index 784f81c..d12b897 100644 --- a/pkg/e621/utils/error.go +++ b/pkg/e621/utils/error.go @@ -2,6 +2,7 @@ package utils import "fmt" +// StatusCodesToError maps HTTP status codes to corresponding error types. func StatusCodesToError(statusCode int) error { var err error switch statusCode { @@ -22,83 +23,83 @@ func StatusCodesToError(statusCode int) error { case 503: err = ServiceUnavailableError{} default: - err = fmt.Errorf("unhandels status code: %d", statusCode) + err = fmt.Errorf("unhandled status code: %d", statusCode) } return err } -type AccessDeniedError struct { -} +// AccessDeniedError represents an "Access Denied" error. +type AccessDeniedError struct{} func (_ AccessDeniedError) Error() string { return "access denied" } -type NotFoundError struct { -} +// NotFoundError represents a "Not Found" error. +type NotFoundError struct{} func (_ NotFoundError) Error() string { return "not found" } -type PreconditionFailedError struct { -} +// PreconditionFailedError represents a "Precondition Failed" error. +type PreconditionFailedError struct{} func (_ PreconditionFailedError) Error() string { return "precondition failed" } -type RateLimitReachedError struct { -} +// RateLimitReachedError represents a "Rate Limit Reached" error. +type RateLimitReachedError struct{} func (_ RateLimitReachedError) Error() string { return "rate limit reached" } -type InvalidParametersError struct { -} +// InvalidParametersError represents an "Invalid Parameters" error. +type InvalidParametersError struct{} func (_ InvalidParametersError) Error() string { - return "invalide parameters" + return "invalid parameters" } -type InternalServerError struct { -} +// InternalServerError represents an "Internal Server Error" error. +type InternalServerError struct{} func (_ InternalServerError) Error() string { return "internal server error" } -type BadGatewayError struct { -} +// BadGatewayError represents a "Bad Gateway" error. +type BadGatewayError struct{} func (_ BadGatewayError) Error() string { return "bad gateway" } -type ServiceUnavailableError struct { -} +// ServiceUnavailableError represents a "Service Unavailable" error. +type ServiceUnavailableError struct{} func (_ ServiceUnavailableError) Error() string { return "service unavailable" } -type UnknownError struct { -} +// UnknownError represents an "Unknown" error. +type UnknownError struct{} func (_ UnknownError) Error() string { return "unknown error" } -type OriginConnectionTimeOutError struct { -} +// OriginConnectionTimeOutError represents an "Origin Connection Time-Out" error. +type OriginConnectionTimeOutError struct{} func (_ OriginConnectionTimeOutError) Error() string { return "origin connection time-out" } -type SSLHandshakeFailedError struct { -} +// SSLHandshakeFailedError represents an "SSL Handshake Failed" error. +type SSLHandshakeFailedError struct{} func (_ SSLHandshakeFailedError) Error() string { return "ssl handshake failed"