feat: added mit-level API for posts

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-23 13:15:55 +02:00
parent 7acff845fd
commit e52735fe26
6 changed files with 385 additions and 2 deletions

View File

@ -32,8 +32,8 @@ _Mid Level API_
| Area | Get | Set | | Area | Get | Set |
|:-----------------|:------------------:|:----| |:-----------------|:------------------:|:----|
| Authentication | :x: | | | Authentication | :x: | |
| Posts | :x: | | | Posts | :heavy_check_mark: | |
| Tags | :x: | | | Tags | :heavy_check_mark: | |
| Tag aliases | :x: | | | Tag aliases | :x: | |
| Tag implications | :x: | | | Tag implications | :x: | |
| Notes | :x: | | | Notes | :x: | |

68
example/midlevel/post.go Normal file
View File

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

36
pkg/e621/builder/post.go Normal file
View File

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

View File

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

57
pkg/e621/builder/posts.go Normal file
View File

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

View File

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