From 08d584111e32d78489a67fc011f5e9eee954e026 Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 23 Oct 2023 10:21:51 +0200 Subject: [PATCH] feat: added mit-level API for getUser Signed-off-by: SoXX --- README.md | 15 ++++++ example/midlevel/user.go | 52 ++++++++++++++++++ pkg/e621/builder/user.go | 35 ++++++++++++ pkg/e621/builder/user_test.go | 69 ++++++++++++++++++++++++ pkg/e621/builder/users.go | 42 +++++++++++++++ pkg/e621/builder/users_test.go | 98 ++++++++++++++++++++++++++++++++++ 6 files changed, 311 insertions(+) create mode 100644 example/midlevel/user.go create mode 100644 pkg/e621/builder/user.go create mode 100644 pkg/e621/builder/user_test.go create mode 100644 pkg/e621/builder/users.go create mode 100644 pkg/e621/builder/users_test.go diff --git a/README.md b/README.md index 381f99f..2dcebef 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,21 @@ _High Level API_ | Favorites | :x: | | | DB export | :x: | | +_Mid Level API_ + +| Area | Get | Set | +|:-----------------|:------------------:|:----| +| Authentication | :x: | | +| Posts | :x: | | +| Tags | :x: | | +| Tag aliases | :x: | | +| Tag implications | :x: | | +| Notes | :x: | | +| Pools | :x: | | +| Users | :heavy_check_mark: | | +| Favorites | :x: | | +| DB export | :x: | | + _Low Level API_ | Area | Get | Set | diff --git a/example/midlevel/user.go b/example/midlevel/user.go new file mode 100644 index 0000000..0cbde8a --- /dev/null +++ b/example/midlevel/user.go @@ -0,0 +1,52 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" + "net/http" +) + +func main() { + // Define the request context with essential information. + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "", // Replace with your username + APIKey: "", // Replace with your API key + } + + // Log: Getting a single user. + log.Println("Getting a single user: ") + + // Specify the username for retrieval. + username := "selloo" // Replace with the desired username. + + // Call the GetUser function to retrieve the specified user. + userBuilder := builder.NewGetUserBuilder(requestContext) + user, err := userBuilder.SetUsername(username).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the ID of the retrieved user. + log.Println(user.ID) + } + log.Println("----------") + + // Log: Getting a list of users. + 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) + userList, err := usersBuilder.SetLimit(5).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the number of users retrieved. + log.Println(len(userList)) + } + log.Println("----------") +} diff --git a/pkg/e621/builder/user.go b/pkg/e621/builder/user.go new file mode 100644 index 0000000..fa973b2 --- /dev/null +++ b/pkg/e621/builder/user.go @@ -0,0 +1,35 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" +) + +type UserBuilder interface { + SetUsername(username string) UserBuilder + Execute() (*model.User, error) +} + +func NewGetUserBuilder(requestContext model.RequestContext) UserBuilder { + return &getUser{requestContext: requestContext} +} + +type getUser struct { + requestContext model.RequestContext + username string +} + +func (g *getUser) SetUsername(username string) UserBuilder { + g.username = username + return g +} + +func (g *getUser) Execute() (*model.User, error) { + user, err := endpoints.GetUser(g.requestContext, g.username) + if err != nil { + log.Println(err) + return nil, err + } + return &user, nil +} diff --git a/pkg/e621/builder/user_test.go b/pkg/e621/builder/user_test.go new file mode 100644 index 0000000..6a36ca0 --- /dev/null +++ b/pkg/e621/builder/user_test.go @@ -0,0 +1,69 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetUser(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + userResponse := model.User{ + WikiPageVersionCount: 0, + ArtistVersionCount: 0, + PoolVersionCount: 0, + ForumPostCount: 0, + CommentCount: 69, + FlagCount: 0, + FavoriteCount: 1337, + PositiveFeedbackCount: 0, + NeutralFeedbackCount: 0, + NegativeFeedbackCount: 0, + UploadLimit: 0, + ID: 1, + CreatedAt: "2020-04-07T07:16:40.286+02:00", + Name: "MaxMustermannDer69ste", + Level: 0, + BaseUploadLimit: 0, + PostUploadCount: 0, + PostUpdateCount: 0, + NoteUpdateCount: 0, + IsBanned: false, + CanApprovePosts: false, + CanUploadFree: false, + LevelString: "Member", + AvatarID: 7, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, userResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/users/MaxMustermannDer69ste.json", jsonResponser) + + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + GetUser := NewGetUserBuilder(requestContext) + user, err := GetUser.SetUsername("MaxMustermannDer69ste").Execute() + if err != nil { + t.Error(err) + return + } + + if user.ID == userResponse.ID && user.Name == userResponse.Name && user.CreatedAt == user.CreatedAt { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", user, userResponse) + +} diff --git a/pkg/e621/builder/users.go b/pkg/e621/builder/users.go new file mode 100644 index 0000000..6c974cf --- /dev/null +++ b/pkg/e621/builder/users.go @@ -0,0 +1,42 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/endpoints" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "log" + "strconv" +) + +type UsersBuilder interface { + SetPage(pageNumber int) UsersBuilder + SetLimit(limitUser int) UsersBuilder + Execute() ([]model.User, error) +} + +func NewGetUsersBuilder(requestContext model.RequestContext) UsersBuilder { + return &getUsers{requestContext: requestContext, query: make(map[string]string)} +} + +type getUsers struct { + requestContext model.RequestContext + query map[string]string +} + +func (g *getUsers) SetPage(pageNumber int) UsersBuilder { + g.query["page"] = strconv.Itoa(pageNumber) + return g +} + +func (g *getUsers) SetLimit(limitUser int) UsersBuilder { + g.query["limit"] = strconv.Itoa(limitUser) + return g +} + +func (g *getUsers) Execute() ([]model.User, error) { + users, err := endpoints.GetUsers(g.requestContext, g.query) + if err != nil { + log.Println(err) + return nil, err + } + return users, nil +} diff --git a/pkg/e621/builder/users_test.go b/pkg/e621/builder/users_test.go new file mode 100644 index 0000000..40b0621 --- /dev/null +++ b/pkg/e621/builder/users_test.go @@ -0,0 +1,98 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetUsers(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + userResponse := []model.User{ + { + + WikiPageVersionCount: 0, + ArtistVersionCount: 0, + PoolVersionCount: 0, + ForumPostCount: 0, + CommentCount: 69, + FlagCount: 0, + FavoriteCount: 1337, + PositiveFeedbackCount: 0, + NeutralFeedbackCount: 0, + NegativeFeedbackCount: 0, + UploadLimit: 0, + ID: 1, + CreatedAt: "2020-04-07T07:16:40.286+02:00", + Name: "MaxMustermannDer69ste", + Level: 0, + BaseUploadLimit: 0, + PostUploadCount: 0, + PostUpdateCount: 0, + NoteUpdateCount: 0, + IsBanned: false, + CanApprovePosts: false, + CanUploadFree: false, + LevelString: "Member", + AvatarID: 7, + }, + { + WikiPageVersionCount: 0, + ArtistVersionCount: 0, + PoolVersionCount: 0, + ForumPostCount: 0, + CommentCount: 1337, + FlagCount: 0, + FavoriteCount: 69, + PositiveFeedbackCount: 0, + NeutralFeedbackCount: 0, + NegativeFeedbackCount: 0, + UploadLimit: 0, + ID: 1, + CreatedAt: "2020-04-08T07:16:40.286+02:00", + Name: "HollaDieWaldfee", + Level: 0, + BaseUploadLimit: 0, + PostUploadCount: 0, + PostUpdateCount: 0, + NoteUpdateCount: 0, + IsBanned: false, + CanApprovePosts: false, + CanUploadFree: false, + LevelString: "Member", + AvatarID: 16, + }, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, userResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/users.json", jsonResponser) + + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + getUsers := NewGetUsersBuilder(requestContext) + user, err := getUsers.Execute() + if err != nil { + t.Error(err) + return + } + + if len(user) == 2 && user[0].ID == userResponse[0].ID && user[1].Name == userResponse[1].Name { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", user, userResponse) + +}