From 7c0765bd3b249a4fbe905379af204eead59e5c6d Mon Sep 17 00:00:00 2001 From: SoXX Date: Tue, 24 Oct 2023 15:25:50 +0200 Subject: [PATCH] feat: added first implementations of the high level api Signed-off-by: SoXX --- example/highlevel/favorites.go | 29 +++++++++++++++ example/highlevel/post.go | 31 ++++++++++++++++ example/highlevel/user.go | 23 ++++++++++++ pkg/e621/client.go | 68 ++++++++++++++++++++++++++++++++++ 4 files changed, 151 insertions(+) create mode 100644 example/highlevel/favorites.go create mode 100644 example/highlevel/post.go create mode 100644 example/highlevel/user.go create mode 100644 pkg/e621/client.go diff --git a/example/highlevel/favorites.go b/example/highlevel/favorites.go new file mode 100644 index 0000000..c28edc8 --- /dev/null +++ b/example/highlevel/favorites.go @@ -0,0 +1,29 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621" + _ "github.com/joho/godotenv/autoload" + "log" + "os" +) + +func main() { + client := e621.NewE621Client(os.Getenv("API_USER"), os.Getenv("API_KEY")) + favorites, err := client.GetFavoritesForUser("selloo") + if err != nil { + log.Panic(err) + } + posts, err := favorites.Execute() + if err != nil { + log.Panic(err) + } + log.Printf("URL of favorite post 0 is: %s", posts[0].File.URL) + + favoritesWithTags := client.GetFavoritesForUserWithTags("selloo", "fennec male solo") + posts, err = favoritesWithTags.Execute() + if err != nil { + log.Panic(err) + } + log.Printf("URL of favorite post 0 with tags is: %s", posts[0].File.URL) + +} diff --git a/example/highlevel/post.go b/example/highlevel/post.go new file mode 100644 index 0000000..79839ad --- /dev/null +++ b/example/highlevel/post.go @@ -0,0 +1,31 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621" + _ "github.com/joho/godotenv/autoload" + "log" + "os" +) + +func main() { + client := e621.NewE621Client(os.Getenv("API_USER"), os.Getenv("API_KEY")) + + posts, err := client.GetPosts().Execute() + if err != nil { + log.Panic(err) + } + log.Printf("Post 0 has following tags: %s", posts[0].Tags) + + postWithTags, err := client.GetPosts().Tags("fennec male solo order:score").Execute() + if err != nil { + log.Panic(err) + } + log.Printf("Post 0 with tags has following ID: %d", postWithTags[0].ID) + + post, err := client.GetPostByID(1337).Execute() + if err != nil { + log.Panic(err) + } + log.Printf("Post 1337 has following tags: %s", post.Tags) + +} diff --git a/example/highlevel/user.go b/example/highlevel/user.go new file mode 100644 index 0000000..dab136e --- /dev/null +++ b/example/highlevel/user.go @@ -0,0 +1,23 @@ +package main + +import ( + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621" + _ "github.com/joho/godotenv/autoload" + "log" + "os" +) + +func main() { + client := e621.NewE621Client(os.Getenv("API_USER"), os.Getenv("API_KEY")) + user, err := client.GetUserByName("selloo").Execute() + if err != nil { + log.Panic(err) + } + log.Printf("User ID of user %s: %d ", user.Name, user.ID) + + user, err = client.GetUserByID(1337).Execute() + if err != nil { + log.Panic(err) + } + log.Printf("User Name of user with ID %d: %s ", user.ID, user.Name) +} diff --git a/pkg/e621/client.go b/pkg/e621/client.go new file mode 100644 index 0000000..afc6a47 --- /dev/null +++ b/pkg/e621/client.go @@ -0,0 +1,68 @@ +package e621 + +import ( + "fmt" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/builder" + "git.dragse.it/anthrove/e621-sdk-go/pkg/e621/model" + _ "github.com/joho/godotenv/autoload" + "golang.org/x/time/rate" + "net/http" + "strconv" +) + +type Client struct { + RequestContext model.RequestContext +} + +func NewE621Client(username string, apikey string) Client { + return Client{ + RequestContext: model.RequestContext{ + Client: http.Client{}, + RateLimiter: rate.NewLimiter(1, 2), + Host: "https://e621.net", + UserAgent: fmt.Sprintf("Go-e621-SDK used by %s | (made by the Anthrove Team)", username), + Username: username, + APIKey: apikey, + }, + } +} + +func (c *Client) SetHost(host string) *Client { + c.RequestContext.Host = host + return c +} + +func (c *Client) SetAgentName(userAgent string) *Client { + c.RequestContext.UserAgent = userAgent + return c +} + +func (c *Client) GetUserByName(username string) builder.UserBuilder { + return builder.NewGetUserBuilder(c.RequestContext).SetUsername(username) +} + +func (c *Client) GetUserByID(userID model.UserID) builder.UserBuilder { + return builder.NewGetUserBuilder(c.RequestContext).SetUsername(strconv.FormatInt(int64(userID), 10)) +} + +func (c *Client) GetFavoritesForUser(username string) (builder.FavoritesBuilder, error) { + user, err := builder.NewGetUserBuilder(c.RequestContext).SetUsername(username).Execute() + if err != nil { + return nil, err + } + favoritesBuilder := builder.NewGetFavoritesBuilder(c.RequestContext).SetUserID(user.ID) + return favoritesBuilder, nil +} + +func (c *Client) GetFavoritesForUserWithTags(username string, tags string) builder.PostsBuilder { + favoritesBuilder := builder.NewGetPostsBuilder(c.RequestContext).Tags(fmt.Sprintf("fav:%s %s", username, tags)) + return favoritesBuilder +} + +func (c *Client) GetPostByID(id model.PostID) builder.PostBuilder { + return builder.NewGetPostBuilder(c.RequestContext).SetPostID(id) +} + +func (c *Client) GetPosts() builder.PostsBuilder { + return builder.NewGetPostsBuilder(c.RequestContext) +}