From e52735fe268219e7e7284b37340eaba4c26c084f Mon Sep 17 00:00:00 2001 From: SoXX Date: Mon, 23 Oct 2023 13:15:55 +0200 Subject: [PATCH] feat: added mit-level API for posts Signed-off-by: SoXX --- README.md | 4 +- example/midlevel/post.go | 68 ++++++++++++++++ pkg/e621/builder/post.go | 36 +++++++++ pkg/e621/builder/post_test.go | 78 ++++++++++++++++++ pkg/e621/builder/posts.go | 57 +++++++++++++ pkg/e621/builder/posts_test.go | 144 +++++++++++++++++++++++++++++++++ 6 files changed, 385 insertions(+), 2 deletions(-) create mode 100644 example/midlevel/post.go create mode 100644 pkg/e621/builder/post.go create mode 100644 pkg/e621/builder/post_test.go create mode 100644 pkg/e621/builder/posts.go create mode 100644 pkg/e621/builder/posts_test.go diff --git a/README.md b/README.md index 2dcebef..23948aa 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,8 @@ _Mid Level API_ | Area | Get | Set | |:-----------------|:------------------:|:----| | Authentication | :x: | | -| Posts | :x: | | -| Tags | :x: | | +| Posts | :heavy_check_mark: | | +| Tags | :heavy_check_mark: | | | Tag aliases | :x: | | | Tag implications | :x: | | | Notes | :x: | | diff --git a/example/midlevel/post.go b/example/midlevel/post.go new file mode 100644 index 0000000..a72dd28 --- /dev/null +++ b/example/midlevel/post.go @@ -0,0 +1,68 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/builder" + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + _ "github.com/joho/godotenv/autoload" + "log" + "net/http" + "os" +) + +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: os.Getenv("API_USER"), // Replace with your username + APIKey: os.Getenv("API_KEY"), // Replace with your API key + } + + // Log: Getting a single post. + log.Println("Getting single post: ") + + // Specify the post ID for retrieval. + postID := 4353480 // Replace with the desired post's ID. + + // Call the GetPost function to retrieve the specified post. + getPost := builder.NewGetPostBuilder(requestContext) + post, err := getPost.SetPostID(postID).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the URL of the retrieved post. + log.Println(post.File.URL) + } + log.Println("----------") + + // Log: Getting a list of posts. + 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) + posts, err := getPosts.SetLimit(5).Execute() + + if err != nil { + log.Println(err) + } else { + // Log the number of posts retrieved. + log.Println(len(posts)) + } + log.Println("----------") + + // Log: Getting a list of posts with tags. + log.Println("Getting a list of posts: ") + + // Call the GetPosts function to retrieve a list of posts based on the query parameters. + posts, err = getPosts.SetLimit(5).Tags("fennec").Execute() + + if err != nil { + log.Println(err) + } else { + // Log the number of posts retrieved. + log.Println(len(posts)) + } + log.Println("----------") +} diff --git a/pkg/e621/builder/post.go b/pkg/e621/builder/post.go new file mode 100644 index 0000000..6c9dec6 --- /dev/null +++ b/pkg/e621/builder/post.go @@ -0,0 +1,36 @@ +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 PostBuilder interface { + SetPostID(postID int) PostBuilder + Execute() (*model.Post, error) +} + +func NewGetPostBuilder(requestContext model.RequestContext) PostBuilder { + return &getPost{requestContext: requestContext} +} + +type getPost struct { + requestContext model.RequestContext + postID int +} + +func (g *getPost) SetPostID(postID int) PostBuilder { + g.postID = postID + return g +} + +func (g *getPost) Execute() (*model.Post, error) { + post, err := endpoints.GetPost(g.requestContext, strconv.Itoa(g.postID)) + if err != nil { + log.Println(err) + return nil, err + } + return &post, nil +} diff --git a/pkg/e621/builder/post_test.go b/pkg/e621/builder/post_test.go new file mode 100644 index 0000000..d4b7a1a --- /dev/null +++ b/pkg/e621/builder/post_test.go @@ -0,0 +1,78 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetPost(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + postResponse := model.PostResponse{ + Post: model.Post{ + ID: 658415636580, + CreatedAt: "2023-10-15T03:58:27.272-04:00", + UpdatedAt: "2023-10-19T02:47:33.597-04:00", + File: model.File{ + Width: 759, + Height: 980, + EXT: "jpg", + Size: 640942, + Md5: "36e229e910638c7aebe65a500f16f3ee", + URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg", + }, + Preview: model.Preview{}, + Sample: model.Sample{}, + Score: model.Score{}, + Tags: model.Tags{}, + LockedTags: nil, + ChangeSeq: 0, + Flags: model.Flags{}, + Rating: "", + FavCount: 0, + Sources: nil, + Pools: nil, + Relationships: model.Relationships{}, + ApproverID: nil, + UploaderID: 0, + Description: "", + CommentCount: 0, + IsFavorited: false, + HasNotes: false, + Duration: nil, + }, + Posts: nil, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, postResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/posts/658415636580.json", jsonResponser) + + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + getPost := NewGetPostBuilder(requestContext) + post, err := getPost.SetPostID(658415636580).Execute() + if err != nil { + t.Error(err) + return + } + + if post.ID == postResponse.Post.ID && post.File.URL == postResponse.Post.File.URL { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", post, postResponse) + +} diff --git a/pkg/e621/builder/posts.go b/pkg/e621/builder/posts.go new file mode 100644 index 0000000..5171edc --- /dev/null +++ b/pkg/e621/builder/posts.go @@ -0,0 +1,57 @@ +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 PostsBuilder interface { + Tags(tags string) PostsBuilder + PageAfter(postID int) PostsBuilder + pageBefore(postID int) PostsBuilder + SetLimit(limitUser int) PostsBuilder + Execute() ([]model.Post, error) +} + +func NewGetPostsBuilder(requestContext model.RequestContext) PostsBuilder { + return &getPosts{ + requestContext: requestContext, + query: make(map[string]string), + } +} + +type getPosts struct { + requestContext model.RequestContext + query map[string]string +} + +func (g *getPosts) Tags(tags string) PostsBuilder { + g.query["tags"] = tags + return g +} + +func (g *getPosts) PageAfter(postID int) PostsBuilder { + g.query["page"] = "a" + strconv.Itoa(postID) + return g +} + +func (g *getPosts) pageBefore(postID int) PostsBuilder { + g.query["page"] = "b" + strconv.Itoa(postID) + return g +} + +func (g *getPosts) SetLimit(limitUser int) PostsBuilder { + g.query["limit"] = strconv.Itoa(limitUser) + return g +} + +func (g *getPosts) Execute() ([]model.Post, error) { + posts, err := endpoints.GetPosts(g.requestContext, g.query) + if err != nil { + log.Println(err) + return nil, err + } + return posts, err +} diff --git a/pkg/e621/builder/posts_test.go b/pkg/e621/builder/posts_test.go new file mode 100644 index 0000000..1337b4c --- /dev/null +++ b/pkg/e621/builder/posts_test.go @@ -0,0 +1,144 @@ +package builder + +import ( + "git.dragse.it/anthrove/e621-to-graph/pkg/e621/model" + "github.com/jarcoal/httpmock" + "net/http" + "testing" +) + +func TestGetPosts(t *testing.T) { + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + postResponse := model.PostResponse{ + Post: model.Post{}, + Posts: []model.Post{ + { + ID: 658415636580, + CreatedAt: "2023-10-15T03:58:27.272-04:00", + UpdatedAt: "2023-10-19T02:47:33.597-04:00", + File: model.File{ + Width: 759, + Height: 980, + EXT: "jpg", + Size: 640942, + Md5: "36e229e910638c7aebe65a500f16f3ee", + URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg", + }, + Preview: model.Preview{}, + Sample: model.Sample{}, + Score: model.Score{}, + Tags: model.Tags{}, + LockedTags: nil, + ChangeSeq: 0, + Flags: model.Flags{}, + Rating: "", + FavCount: 0, + Sources: nil, + Pools: nil, + Relationships: model.Relationships{}, + ApproverID: nil, + UploaderID: 0, + Description: "", + CommentCount: 0, + IsFavorited: false, + HasNotes: false, + Duration: nil, + }, + { + ID: 698418695, + CreatedAt: "2023-10-15T03:58:27.272-04:00", + UpdatedAt: "2023-10-19T02:47:33.597-04:00", + File: model.File{ + Width: 759, + Height: 980, + EXT: "jpg", + Size: 640942, + Md5: "36e229e910638c7aebe65a500f16f3ee", + URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg", + }, + Preview: model.Preview{}, + Sample: model.Sample{}, + Score: model.Score{}, + Tags: model.Tags{}, + LockedTags: nil, + ChangeSeq: 0, + Flags: model.Flags{}, + Rating: "", + FavCount: 0, + Sources: nil, + Pools: nil, + Relationships: model.Relationships{}, + ApproverID: nil, + UploaderID: 0, + Description: "", + CommentCount: 0, + IsFavorited: false, + HasNotes: false, + Duration: nil, + }, + { + ID: 48976516894, + CreatedAt: "2023-10-15T03:58:27.272-04:00", + UpdatedAt: "2023-10-19T02:47:33.597-04:00", + File: model.File{ + Width: 759, + Height: 980, + EXT: "jpg", + Size: 640942, + Md5: "36e229e910638c7aebe65a500f16f3ee", + URL: "https://static1.e621.net/data/36/e2/36e229e910638c7aebe65a500f16f3ee.jpg", + }, + Preview: model.Preview{}, + Sample: model.Sample{}, + Score: model.Score{}, + Tags: model.Tags{}, + LockedTags: nil, + ChangeSeq: 0, + Flags: model.Flags{}, + Rating: "", + FavCount: 0, + Sources: nil, + Pools: nil, + Relationships: model.Relationships{}, + ApproverID: nil, + UploaderID: 0, + Description: "", + CommentCount: 0, + IsFavorited: false, + HasNotes: false, + Duration: nil, + }, + }, + } + + jsonResponser, err := httpmock.NewJsonResponder(200, postResponse) + if err != nil { + t.Error(err) + return + } + httpmock.RegisterResponder("GET", "https://e621.net/posts.json", jsonResponser) + + requestContext := model.RequestContext{ + Client: http.Client{}, + Host: "https://e621.net", + UserAgent: "Go-e621-SDK (@username)", + Username: "memo", + APIKey: "123456", + } + + getPosts := NewGetPostsBuilder(requestContext) + posts, err := getPosts.SetLimit(3).Execute() + if err != nil { + t.Error(err) + return + } + + if len(posts) == 3 && posts[0].ID == postResponse.Posts[0].ID && posts[1].File.Md5 == postResponse.Posts[1].File.Md5 && posts[2].File.EXT == postResponse.Posts[2].File.EXT { + return + } + + t.Errorf("Respons did not match mock data:\nOriginal: %v\nMock: %v", posts, postResponse) + +}