feat: added mid-level API for favorites
Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
parent
93567a2d8b
commit
c5a1ea07e5
49
example/midlevel/favorite.go
Normal file
49
example/midlevel/favorite.go
Normal file
@ -0,0 +1,49 @@
|
||||
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 favorites from the API.
|
||||
log.Println("Getting favorites API user: ")
|
||||
|
||||
// Call the GetFavorites function to retrieve favorite posts.
|
||||
getFavorites := builder.NewGetFavoritesBuilder(requestContext)
|
||||
posts, err := getFavorites.SetLimit(5).Execute()
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
// Log the URL of the first favorite post.
|
||||
log.Println(posts[0].File.URL)
|
||||
}
|
||||
log.Println("----------")
|
||||
|
||||
// Log: Getting favorites for a specific user.
|
||||
log.Println("Getting favorites for user: ")
|
||||
|
||||
// Call the GetFavorites function to retrieve favorite posts for the specified user.
|
||||
posts, err = getFavorites.SetLimit(5).SetUserID(1337).Execute()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
// Log the URL of the first favorite post.
|
||||
log.Println(posts[0].File.URL)
|
||||
}
|
||||
log.Println("----------")
|
||||
}
|
45
pkg/e621/builder/favorite.go
Normal file
45
pkg/e621/builder/favorite.go
Normal file
@ -0,0 +1,45 @@
|
||||
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 FavoritesBuilder interface {
|
||||
SetUserID(userID int) FavoritesBuilder
|
||||
SetLimit(limitFavorites int) FavoritesBuilder
|
||||
Execute() ([]model.Post, error)
|
||||
}
|
||||
|
||||
func NewGetFavoritesBuilder(requestContext model.RequestContext) FavoritesBuilder {
|
||||
return &getFavorites{
|
||||
requestContext: requestContext,
|
||||
query: make(map[string]string),
|
||||
}
|
||||
}
|
||||
|
||||
type getFavorites struct {
|
||||
query map[string]string
|
||||
requestContext model.RequestContext
|
||||
}
|
||||
|
||||
func (g getFavorites) SetUserID(userID int) FavoritesBuilder {
|
||||
g.query["user_id"] = strconv.Itoa(userID)
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getFavorites) SetLimit(limitFavorites int) FavoritesBuilder {
|
||||
g.query["limit"] = strconv.Itoa(limitFavorites)
|
||||
return g
|
||||
}
|
||||
|
||||
func (g getFavorites) Execute() ([]model.Post, error) {
|
||||
favorites, err := endpoints.GetFavorites(g.requestContext, g.query)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return nil, err
|
||||
}
|
||||
return favorites, nil
|
||||
}
|
143
pkg/e621/builder/favorite_test.go
Normal file
143
pkg/e621/builder/favorite_test.go
Normal file
@ -0,0 +1,143 @@
|
||||
package builder
|
||||
|
||||
import (
|
||||
"git.dragse.it/anthrove/e621-to-graph/pkg/e621/model"
|
||||
"github.com/jarcoal/httpmock"
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetFavorites(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/favorites.json", jsonResponser)
|
||||
|
||||
requestContext := model.RequestContext{
|
||||
Client: http.Client{},
|
||||
Host: "https://e621.net",
|
||||
UserAgent: "Go-e621-SDK (@username)",
|
||||
Username: "memo",
|
||||
APIKey: "123456",
|
||||
}
|
||||
getFavorites := NewGetFavoritesBuilder(requestContext)
|
||||
posts, err := getFavorites.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)
|
||||
|
||||
}
|
Reference in New Issue
Block a user