feat: added first implementations of the high level api

Signed-off-by: SoXX <soxx@fenpa.ws>
This commit is contained in:
SoXX 2023-10-24 15:25:50 +02:00
parent 3e80192bef
commit 7c0765bd3b
4 changed files with 151 additions and 0 deletions

View File

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

31
example/highlevel/post.go Normal file
View File

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

23
example/highlevel/user.go Normal file
View File

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

68
pkg/e621/client.go Normal file
View File

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