feat: added mit-level API for getUser

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 10:21:51 +02:00
parent 38cfa7c9cf
commit 08d584111e
6 changed files with 311 additions and 0 deletions

View File

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

52
example/midlevel/user.go Normal file
View File

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

35
pkg/e621/builder/user.go Normal file
View File

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

View File

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

42
pkg/e621/builder/users.go Normal file
View File

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

View File

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